3
<?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値が失われる理由を誰か説明できますか?

4

3 に答える 3

4

あなたの状況でこれを解決したかどうかはわかりませんが、Laravel 5.1 ではこれが私に起こりました.0 または 1 対 1 の関係があるため、あるテーブルの主キーは別のテーブルの主キーと同じです。

何が起こっているかというと、Eloquent は主キーを挿入の最後の挿入 ID に割り当てていますが、主キーは自動インクリメント値ではないため、ゼロに割り当てています。データベースに正しく保存されますが、そのキーを使用する必要がある場合、保存後のモデルは役に立ちません。解決策は、外部主キーを持つモデルの insertAndSetId 関数をオーバーライドして、主キー属性の設定を防止することです。もちろん、自動インクリメント キーを持つモデルではこれを実行したくありません。主キーを手動で割り当てているモデルだけです上で述べたように、データベースには正しい情報が含まれているため、モデルを作成した直後にモデルを使用する必要がない場合も必要ありません。

protected function insertAndSetId(Builder $query, $attributes)
{
    $id = $query->insertGetId($attributes, $keyName = $this->getKeyName());

    // $this->setAttribute($keyName, $id);
}
于 2015-07-10T21:47:07.270 に答える
2

これは、データベースの id 列に自動インクリメントが設定されていない可能性があるためです。

自動インクリメントなしのテスト モデルでこれを試したところ、0 が返されましたが、id 列を自動インクリメントに変更すると、ID が正しく返されました。

laravel/Framework/Src/Illuminate/Database/Eloquent/Model.php でこの関数を確認してください

自動インクリメントがある場合は、id を挿入して設定すると言われています。

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;
    }
于 2013-10-03T14:55:38.897 に答える
0

私にとっては、問題を解決するために、モデルの主キーの列名に protect $primaryKey を設定する必要がありました。(skill_id は列名だったので、Skill モデルでは protected $primaryKey = 'skill_id' を設定しました。デフォルトは 'id' です。)

于 2015-08-11T19:57:00.863 に答える