php Laravel Model 在没有主键的情况下保存数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25433220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Model saving data without primary key
提问by jl.
I have 2 models: Profile and Student. The profile contains a primary key, id. Student model doesn't have any primary key, but one foreign key, profile_id, which links to profile.id. I have a function within my Student controller that activates the Student, setting the status from 0 to 1, when called. I am trying to use save() for the update of the value, but unable to do it, prompting me a message as such:
我有 2 个模型:Profile 和 Student。配置文件包含一个主键id。学生模型没有任何主键,只有一个外键profile_id,它链接到 profile.id。我的 Student 控制器中有一个函数可以激活 Student,在调用时将状态从 0 设置为 1。我正在尝试使用 save() 来更新值,但无法做到,提示我一条消息:
Column not found: 1054 Unknown column 'id' in 'where clause'
Can please advise? Thank you very much.
可以请指教吗?非常感谢。
Student Model:
学生模型:
class Student extends Eloquent {
protected $primary_key = null;
public $incrementing = false;
protected $fillable = array('username', 'password', 'status', 'profile_id');
public function profile() {
return $this->belongsTo('Profile');
}
}
Profile Model:
型材型号:
class Profile extends Eloquent {
protected $fillable = array('firstname', 'lastname', 'email', 'phone');
public function student()
{
return $this->hasOne('Student', 'profile_id', 'id');
}
}
StudentController:
学生控制器:
public function activate($id) {
$student = Student::where('profile_id', '=', $id)->first();
$student->status = 1;
$student->save();
return Redirect::to('students');
}
回答by c-griffin
You might want to consider adding an id
field. It is pretty important to Laravel and packages (for aggregation, specifically)
您可能需要考虑添加一个id
字段。这对 Laravel 和包非常重要(特别是对于聚合)
but if you must...
但如果你必须...
Student Model:(camel case primaryKey
property)
学生模型:(骆驼壳primaryKey
属性)
class Student extends Eloquent {
protected $primaryKey = null;
public $incrementing = false;
protected $fillable = array('username', 'password', 'status', 'profile_id');
public function profile() {
return $this->belongsTo('Profile');
}
}
回答by ivoroto
I had the same problem and couldn't use a primary key field for this table.
我遇到了同样的问题,无法为此表使用主键字段。
Since Eloquent cannot update tables like this, I solved it simply deciding updating the row in a different way:
由于 Eloquent 无法像这样更新表,我解决了它,只需决定以不同的方式更新行:
NameOfMyTable::where('entity_type', 1)->update(['last_import' => Carbon::now()]);
回答by mushood badulla
If anyone has the same issue. I used Nishchit Dhanani answer (here) and it worked pretty well which was:
Write this line into your Model
public $primaryKey = '_id';
Where _id is your manual primary key
如果有人有同样的问题。我使用了 Nishchit Dhanani 的答案(这里)并且效果很好,它是:
将此行写入您的模型
public $primaryKey = '_id';
其中 _id 是您的手动主键
For my case. I had a user table and part of my users are students. So the primary key in my students table was "user_id".
Adding this line in my Student model: public $primaryKey = 'user_id';
对于我的情况。我有一个用户表,我的部分用户是学生。所以我的学生表中的主键是“user_id”。
在我的学生模型中添加这一行: public $primaryKey = 'user_id';
I was able to use the save() method when updating my student object.
更新我的学生对象时,我能够使用 save() 方法。
In your case, you would just need to add in your student model
public $primaryKey = 'profile_id'
在你的情况下,你只需要在你的学生模型中添加
public $primaryKey = 'profile_id'
Hope this helps
希望这可以帮助