首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >插入后Laravel4模型主键值丢失

插入后Laravel4模型主键值丢失
EN

Stack Overflow用户
提问于 2013-10-03 21:31:55
回答 3查看 2.5K关注 0票数 3
代码语言:javascript
复制
<?php
// Model
class ProfileDelivery extends \Eloquent {
    protected $table = 'profile_delivery';
    protected $guarded = array();
    public $timestamps = FALSE;
}

// Somewhere
$deliveryGuy->id = 1;
print $deliveryGuy->id; // Prints 1
if (!$deliveryGuy->save()) {
    throw new \Exception('Cant save .');
}
print $deliveryGuy->id; // Prints 0

有人能解释一下为什么ID值丢失了吗?

EN

回答 3

Stack Overflow用户

发布于 2015-07-11 05:47:07

我不确定你是否针对你的情况解决了这个问题,但在Laravel 5.1中,这种情况刚刚发生在我身上--一个表的主键与另一个表的主键相同,因为它们之间存在0或1对1的关系。

发生的情况是,Eloquent将主键分配给插入的最后一个插入id,但因为主键不是自动增量值,所以它将其分配为零。它被正确地存储在数据库中,但如果您需要使用该键,则保存后的模型将不再有用。解决方案是覆盖具有外主键的模型的insertAndSetId函数,以防止其设置主键属性。当然,您不希望对任何具有自动增量键的模型执行此操作,只希望对您手动分配主键的模型执行此操作。如果您在创建模型后不需要立即使用它,也没有必要这样做,因为正如我上面所说的,数据库中有正确的信息。

代码语言:javascript
复制
protected function insertAndSetId(Builder $query, $attributes)
{
    $id = $query->insertGetId($attributes, $keyName = $this->getKeyName());

    // $this->setAttribute($keyName, $id);
}
票数 4
EN

Stack Overflow用户

发布于 2013-10-03 22:55:39

这是因为数据库中的id列可能没有设置自动增量。

我在不带自动增量的测试模型中尝试了这一点,它返回0,但是当我将id列更改为自动增量时,它正确地返回了id。

在laravel/Framework/Src/Illuminate/Database/Eloquent/Model.php中检查此函数

它说,如果它有自动增量,它将插入并设置id。

代码语言:javascript
复制
protected function performInsert($query)
    {
        if ($this->fireModelEvent('creating') === false) return false;

        // First we'll need to create a fresh query instance and touch the creation and
        // update timestamps on this model, which are maintained by us for developer
        // convenience. After, we will just continue saving these model instances.
        if ($this->timestamps)
        {
            $this->updateTimestamps();
        }

        // If the model has an incrementing key, we can use the "insertGetId" method on
        // the query builder, which will give us back the final inserted ID for this
        // table from the database. Not all tables have to be incrementing though.
        $attributes = $this->attributes;

        if ($this->incrementing)
        {
            $this->insertAndSetId($query, $attributes);
        }

        // If the table is not incrementing we'll simply insert this attributes as they
        // are, as this attributes arrays must contain an "id" column already placed
        // there by the developer as the manually determined key for these models.
        else
        {
            $query->insert($attributes);
        }

        // We will go ahead and set the exists property to true, so that it is set when
        // the created event is fired, just in case the developer tries to update it
        // during the event. This will allow them to do so and run an update here.
        $this->exists = true;

        $this->fireModelEvent('created', false);

        return true;
    }
票数 2
EN

Stack Overflow用户

发布于 2015-08-12 03:57:01

对我来说,为了解决这个问题,我必须将protect $primaryKey设置为模型中主键的列名。(skill_id是列名,因此在技能模型中,我设置了protected $primaryKey = ' Skill _id',缺省值为‘id’。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19160589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档