【发布时间】:2014-04-08 15:01:37
【问题描述】:
我在使用 Laravel Eloquent ORM 时遇到了一个问题: 在数据库中插入新的 Eloquent 模型时,数据已损坏。 具体来说:
$newItem = new NotificationNewItem;
$newItem->item_id = $item->id; // item_id is the primary key (returned by getKeyName())
$newItem->save();
return NotificationNewItem::find($item->id);
此代码返回的结果与
不同$newItem = new NotificationNewItem;
$newItem->item_id = $item->id;
$newItem->save();
return $newItem;
而这两个项目应该是相同的,不是吗? 奇怪的是,第一种情况下返回的 JSON 对象(我直接在浏览器中显示)正是插入到数据库中的内容,而在第二种情况下,JSON 对象的主键(这里 item_id)甚至等于 0如果在数据库中相应条目的主键等于 3(或不同的值)。
如果您想再次看到该错误,这里是 laravel 代码:http://pastebin.com/9wcsnvSq 模型函数 insertAndGetElement() 中有两个“返回”,它们返回具有不同主键的项目(该 pastebin 中的第一个返回的主键等于 0)。
我们将不胜感激。 提前致谢, 罗宾。
【问题讨论】:
标签: orm insert laravel-4 eloquent