我正在尝试使用 php activerecord 更新用户详细信息表,但每次我都会收到此错误
::save() cannot be invoked because this model is set to read only
我可以插入到同一个表,但我不能更新
详细信息.php
<?php
class Details extends ActiveRecord\Model
{
public static $table_name='details';
public static $primary_key='id_user_detail';
public function before_create()
{
$this->date_created = date("Y-m-d H:i:s");
$this->date_modified = date("Y-m-d H:i:s");
}
public function before_update()
{
$this->date_modified = date("Y-m-d H:i:s");
}
}?>
profile-edit.php
$sql = " select * from details where id_user_detail=1";
$user = Details::find_by_sql($sql);
$user->user_fname="test";
$user->save();
如何进行更新?
最佳答案
它是只读的这一事实是意料之中的。见the manual for custom-sql finders
This will render your model as readonly so that you cannot use any write methods on your returned model(s).
不用这么复杂的取景器,用AR模型就行了:
$user = Details::find(1);
$user->user_fname="test";
$user->save();
这是有效的,因为 id_user_detail
是您的主键。如果你想在另一个字段上查找,假设是 user_fname
,请执行以下操作:
$user = Details::find_by_user_fname('test');
$user->user_fname="test2";
$user->save();
阅读这些查找器以及如何使用它们,因为您应该避免使用 sql-finder。
关于php - 无法更新 php activerecord : model is read-only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30748907/