[英]Error when I save model in Laravel. 'Call to a member function getKey() on a non-object'
I try save my object in DB by this code: 我尝试通过以下代码将对象保存在DB中:
$preview = new ProjectProductPreview();
$preview->user_id = $user_id;
$preview->path = $preview_url;
$preview->type = $type;
$preview->project_product_id = $product_id;
$preview->save();
And I got strange error. 而且我得到了奇怪的错误。 This error occurs when call save() method.
调用save()方法时,会发生此错误。 I have checked my laravel logs, there is this error.
我检查了我的Laravel日志,出现此错误。 I can debug this precisely because in this case, error stack has only two positions
我可以精确地调试它,因为在这种情况下,错误堆栈只有两个位置
[2015-02-17 09:37:30] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function getKey() on a non-object' in /var/www/erp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:133
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []
My model looks like: 我的模型如下:
class ProjectProductPreview extends BaseModel {
protected $table = 'project_products_preview';
public function user() {
return $this->belongsTo('User');
}
public function project_product() {
return $this->belongsTo('ProjectProduct', 'project_product_id');
}
public function projectProduct()
{
return $this->belongsTo('ProjectProduct', 'project_product_id');
}
public function message() {
return $this->belongsToMany('Message');
}
public function getThumb() {
$path = $this->path;
$thumb_path = str_replace('.jpg', '_70.jpg', $path);
return $thumb_path;
}
public function fullPath() {
return public_path($this->path);
}
public function getPath()
{
return $this->path;
}
public function getUrlAttribute() {
$product_id = $this->project_product_id;
return '/product/' . $product_id . '#' . $this->id;
}
public function isAccepted()
{
if($this->accepted==1){
return true;
}
return false;
}
public function accept()
{
ProjectProductPreview::where('project_product_id',$this->project_product_id)->update(array('accepted'=>0));
$this->accepted=1;
$this->save();
Event::fire('project_product_preview.accepted', array($this));
}
public function acceptRemove()
{
$this->accepted=0;
$this->save();
Event::fire('project_product_preview.remove_accept', array($this));
}
}
I am not quite sure about this, but I think this line 我对此不太确定,但我认为这条线
$preview->project_product_id = $product_id;
causes the issue. 导致问题。 You have a relationship to
ProjectProduct
so you might want to do sth. 您与
ProjectProduct
有关系,因此您可能想做某事。 like that actually: 像这样:
$preview->projectProduct()->associate(ProjectProduct::find($product_id));
see the laravel docs on inserting models . 有关插入模型的信息,请参阅laravel文档 。 Please also note that you have two identical realtionships to
ProjectProduct
which might cause unexpected behaviour as well. 另请注意,您对
ProjectProduct
有两个相同的所有权,这也可能导致意外行为。 At least one is superflous. 至少一个是多余的。
// one can be removed
//public function project_product() {
// return $this->belongsTo('ProjectProduct', 'project_product_id');
//}
public function projectProduct()
{
return $this->belongsTo('ProjectProduct', 'project_product_id');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.