错误:模型要求列'id',但$ key设置为其他不同

时间:2013-10-03 05:39:54

标签: laravel laravel-4 eloquent

错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `genre_books` set `provider_id` = ?, `genre_id` = ? where `id` is null) (Bindings: array ( 0 => 3, 1 => 466284, ))

它正在成功添加一些数据,但在该表中,但在更新过程中仍会出现此错误。

型号:

class GenreBook extends Eloquent {

    public $table = 'genre_books';
    public $timestamps = false;
    public $publicKey = 'book_id';

    public function setRank($a) {
        $this->sort_order = $a;
    }

    public function setProviderId($a) {
        $this->provider_id = $a;
    }

    public function setBookId($a) {
        $this->book_id = $a;
    }

    public function setGenreId($a) {
        $this->genre_id = $a;
    }

}

控制器中的代码:

GenreBook::unguard();
foreach($bookList as $genreId => $books) {
     if(count($books) > 0) {
    foreach ($books as $book) {
         $genreBookRecord = GenreBook::where('book_id',$book['asin'])->where('provider_id',3)->first();


         $newGenreBook->setProviderId(3);
         $newGenreBook->setBookId($book['asin']);
         $newGenreBook->setRank($book['rank']);
         $newGenreBook->setGenreId($genreId);
         $newGenreBook->save();
      }
}

1 个答案:

答案 0 :(得分:9)

Eloquent假设primary keyid,所以你需要做的就是这样设置你的主键:

 protected $primaryKey = your primary key