Laravel:模型使用自动生成的uuid保存但该模型的实例将此uuid清空

时间:2016-02-19 08:21:23

标签: laravel model persistence laravel-5.1

我在模型上使用特征生成uuid。这是有用的,因为在我的数据库中uuid值不为空。但是我模型的当前实例没有新值。

这是特征:

trait UuidModel
{
    public static function bootUuidModel()
    {
        static::creating(function ($model) {
            // Don't let people provide their own UUIDs, we will generate a proper one.
            $model->uuid = Uuid::generate(4);
            return true;
        });

        static::saving(function ($model) {
            // What's that, trying to change the UUID huh?  Nope, not gonna happen.
            $original_uuid = $model->getOriginal('uuid');

            if ($original_uuid !== $model->uuid) {
                $model->uuid = $original_uuid;
            }
            return true;
        });
    }

    public function scopeUuid($query, $uuid, $first = true)
    {
        if (!is_string($uuid) || (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid) !== 1)) {
            throw (new ModelNotFoundException)->setModel(get_class($this));
        }

        $search = $query->where('uuid', $uuid);

        return $first ? $search->firstOrFail() : $search;
    }

    public function scopeIdOrUuId($query, $id_or_uuid, $first = true)
    {
        if (!is_string($id_or_uuid) && !is_numeric($id_or_uuid)) {
            throw (new ModelNotFoundException)->setModel(get_class($this));
        }

        if (preg_match('/^([0-9]+|[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})$/', $id_or_uuid) !== 1) {
            throw (new ModelNotFoundException)->setModel(get_class($this));
        }

        $search = $query->where(function ($query) use ($id_or_uuid) {
            $query->where('id', $id_or_uuid)
                ->orWhere('uuid', $id_or_uuid);
        });

        return $first ? $search->firstOrFail() : $search;
    }
}

这是我的模特:

class Image extends BaseModel
{
    use UuidModel;

    protected $table = 'images';
    public $incrementing = false;
    public $timestamps = true;
    protected $guarded = array('id', 'timestamps', 'path');
    protected $visible = array('timestamps', 'uuid');
    protected $hidden = array('id', 'path');

    public function Item()
    {
        return $this->belongsTo('App\Models\Item', 'id', 'item_id');
    }

}

这是我的控制器:

class ImageController extends Controller
{
    ...

    public function store(CreateImageRequest $request, $itemSlug)
    {
        $item = $this->getItem($itemSlug);

        $image = new Image();
        $image->path = "";

        $image = $item->Images()->save($image);

        return Response::json($image->uuid, 201);
    }

    ...

}

响应总是:{}但我的数据库中的uuid不为空。

更新

此测试有效:

public function store(CreateImageRequest $request, $collectionSlug, $itemSlug)
{
    $item = $this->getParentItem($collectionSlug, $itemSlug);

    $image = new Image();
    $image->path = $filePath;

    $image = $item->Images()->save($image);
    $newImage = Image::find($image->id);

    return Response::json($newImage->uuid, 201);
}

2 个答案:

答案 0 :(得分:0)

我昨天遇到同样的问题,这是工作特质

trait UuidTrait
{

  public static function bootUuidTrait(){
    static::creating(function($model){
      $model->incrementing = false;
      $model->{$model->getKeyName()} = Uuid::generate()->string;
    });
  }

}

所以也许这就是你问题的答案

static::creating(function ($model) {
        // Don't let people provide their own UUIDs, we will generate a proper one.
        $model->uuid = Uuid::generate(4)->string;
        return true;
});

我希望我会帮助别人:D

答案 1 :(得分:0)

你正在和#34;创建"并且"保存"事件,但你应该做的是挂钩"创造"和"更新"事件

如果您选中official documentation,则表示"正在保存"在创建新模型和更新现有模型时触发事件 - 因此在您创建新模型时会调用它,从而离开" uuid"前一个的价值(没什么);并且在更新它时也是如此。

如果切换到"创建"和"更新"事件然后一切都会按你的意愿运作 - 基本上只是替换"保存"用"更新"那就是它。