Eloquent模型中的UUID主键存储为uuid,但返回0 [英] UUID primary key in Eloquent model is stored as uuid but returns as 0

查看:312
本文介绍了Eloquent模型中的UUID主键存储为uuid,但返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在使用UUID作为主键的mysql表。以下是创建迁移:

 模式:: create('people',function(Blueprint $ table){
$ table-> uuid('id');
$ table-> primary('id');
...
$ table-> timestamps();

其中生成以下MySQL模式:

  CREATE TABLE`people`(
`id` char(36)COLLATE utf8_unicode_ci NOT NULL,
...
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;

在我的演绎模型中,我有一个方法来创建一个调用生成UUID的方法的实例:

  class Person extends Model 
{
protected $ fillable = [
...
];

public function make(array $ personData){
$ person = new Person;
$人 - > setUUID();
collect($ personData) - > each(function($ value,$ columnName)use($ person){
if(in_array($ columnName,$ this-> fillable)){
$ person-> {$ columnName} = $ value;
}
});
$ person-> save();
return $ person;
}

protected function setUUID(){
$ this-> id = preg_replace('/ \。/','',uniqid('bpm',true ));
}

}

当我创建一个新的模型实例它存储在数据库中:





但是我尝试访问新的实例的ID:





返回0:





什么是我错过了这个?

解决方案

Nevermind,在搜索文档后找到答案: https:// lar avel.com/docs/5.2/eloquent#eloquent-model-conventions



在主键部分有一点点:


此外,Eloquent假定主键是一个递增的整数值。如果您希望使用非递增主键,则必须将模型上的$ incrementing属性设置为false。


如果您要使用UUID,您必须将此属性设置为false。一旦我在我的模型的顶部这样做,它的工作。



由于我的所有模型都将使用UUID,我将UUID逻辑提取到父类。这是它的样子:

  class UuidModel extends Model 
{

public $ incrementing = false;

/ **
*设置主键字段的UUID值。
* /
protected function setUUID()
{
$ this-> id = preg_replace('/ \。/','',uniqid('bpm'真正));
}
}


I have a mysql table in which I'm using a UUID as the primary key. Here's the creation migration:

Schema::create('people', function (Blueprint $table) {
    $table->uuid('id');
    $table->primary('id');
    ...
    $table->timestamps();
}

Which generates the following MySQL schema:

CREATE TABLE `people` (
  `id` char(36) COLLATE utf8_unicode_ci NOT NULL,
  ...
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

In my Eloquent model I have a method for creating an instance which calls a method for generating the UUID:

class Person extends Model
{
    protected $fillable = [
        ...
    ];

    public function make(array $personData){
        $person = new Person;
        $person->setUUID();
        collect($personData)->each(function ($value, $columnName) use($person){
            if(in_array($columnName, $this->fillable)){
                $person->{$columnName} = $value;
            }
        });
        $person->save();
        return $person;
    }

    protected function setUUID(){
        $this->id = preg_replace('/\./', '', uniqid('bpm', true));
    }

}

When I create a new model instance it stores it fine in the database:

But when I try to access the new instance's id:

It returns as 0:

What am I missing here?

解决方案

Nevermind, I found the answer after searching through the docs: https://laravel.com/docs/5.2/eloquent#eloquent-model-conventions

Under the section "Primary Keys" there's a little blurb:

In addition, Eloquent assumes that the primary key is an incrementing integer value. If you wish to use a non-incrementing primary key, you must set the $incrementing property on your model to false.

If you're going to use a UUID you have to set this property to false. Once I did that at the top of my model it worked.

Since all of my models are going to use UUIDs I extracted the UUID logic to a parent class. Here's what it looks like:

class UuidModel extends Model
{

    public $incrementing = false;

    /**
     * Sets the UUID value for the primary key field.
     */
    protected function setUUID()
    {
        $this->id = preg_replace('/\./', '', uniqid('bpm', true));
    }
}

这篇关于Eloquent模型中的UUID主键存储为uuid,但返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆