无法更新php activerecord:model是只读的

时间:2015-06-10 06:42:50

标签: php mysql activerecord phpactiverecord

我正在尝试使用php activerecord更新用户详细信息表,但每次出现此错误

  无法调用

:: save(),因为此模型设置为只读

我可以插入同一张表,但我无法更新

Details.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");
}

}?>

轮廓edit.php

 $sql   = " select * from details where id_user_detail=1";
 $user  = Details::find_by_sql($sql);
 $user->user_fname="test";
 $user->save();

如何进行更新?

3 个答案:

答案 0 :(得分:1)

预计它是只读的这一事实。见the manual for custom-sql finders

  

这会将您的模型呈现为只读,这样您就无法在返回的模型上使用任何写入方法。

没有必要使用这个复杂的查找器,只需使用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。

答案 1 :(得分:0)

这是因为$ user包含一个数组,save()方法仅用于对象。您可以使用其他查找器,但如果它是一个复杂的查询,您可以使用foreach,然后默认使用查找器。

答案 2 :(得分:-1)

您可能在实例化类之前尝试更改类属性。

Details :: find_by_sql()应该返回一个新的Details实例。 例如:

public function find_by_sql($sql) {
   //execute sql
   if( some thing is returned ) {
      $details = new Details();
      $details->setByResult($sql_result)

      return $details;
   }

   return null;
}