我的目的:
1. 从用户输入中获取行的 id,
2. 更改所选行的第一行值。
我有一个这样的模型:
myModel.php
class myModel extends Eloquent{
protected $table = 'myTable';
protected $primaryKey = 'id';
public $timestamps = false;
public incrementing = false;
protected fillable = array(
'id', //yes it needs to be fillable...
'name',
'surname'
);
}
myController.php
class myController extends BaseController{
public function someFunction(){
$modelInstance = myModel::where('id', Session::get('id'))
->where('otherID', Input::get('someHTMLInput'))
->first();
$modelInstance->id = Session::get('id');
$modelInstance->name = Input::get('someNameInput');
$modelInstance->surname = Input::get('someSurnameInput');
$modelInstance->save();
}
}
发生的情况:
1.我可以很好地发布从输入中获得的值
2. 当涉及到 Controller 的 save 方法时,查询会更新每一行的值,包括后面的 ID 号。
即在数据库上
id=1; otherID=1; name=aaa; surname=bbb;
id=1; otherID=2; name=xxx; surname=yyy;
id=1; otherID=3; name=ccc; surname=ddd;
当我尝试更改名称 aaa->rrr 时,会发生以下情况:
id=1; otherID=1; name=rrr; surname=bbb;
id=1; otherID=2; name=rrr; surname=yyy;
id=1; otherID=3; name=rrr; surname=ddd;
提前致谢。
最佳答案
Eloquent 目前不支持复合主键。正在进行讨论 over at github如果您想表达您对该问题的支持。
<小时/>你能做的是:
myModel::where('id', Session::get('id'))
->where('otherID', Input::get('someHTMLInput'))
->update([
'name' => Input::get('someNameInput'),
'surname' => Input::get('someSurnameInput')
]);
但是,这样您就可以使用几乎所有模型功能。它基本上与:
DB::table('my_model')->where('id', Session::get('id'))
->where('otherID', Input::get('someHTMLInput'))
->update([
'name' => Input::get('someNameInput'),
'surname' => Input::get('someSurnameInput')
]);
关于php - Laravel Eloquent 错误地更新每一行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31247953/