php - 无法更新 php activerecord : model is read-only

标签 php mysql activerecord phpactiverecord

我正在尝试使用 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/

相关文章:

PHP: CSV 导入到 MYSQL 总是少于 CSV 文件中的实际行数?

php - 表单欺骗及解决方法

php - 是否可以将参数传递给关联数组?

c# - MySQL 中 INNER JOIN 的语法错误

ruby-on-rails - Rails 在没有收集/映射的情况下将嵌套关系作为 ActiveRecord 对象获取

ruby-on-rails - 在 Rails 3 中使用 group by 查询中以整数形式获取 COUNT(column)

php - 将mysql数据库与Android应用程序连接

mysql - 使用 CONCAT 和 NULL 值

mysql - SQL查询内连接得到2个项目

mysql - serchkick 找到正确结果后如何重新排序结果?