yii afterfind传递数组来查看

时间:2012-08-18 10:19:18

标签: php yii

在我的模型中我有这段代码,这是部分代码

protected function afterFind ()
{
    $car= Car::model()->findAll("car_id = '{$this->model_id}'");
    foreach($car as $cars) {
        $val['car_name'][] = $cars->car_name;
        $val['car_value'][] = $cars->car_value;
    }
    $this->car_arr = $val;

    parent::afterFind();
}

如何传递数组进行查看?当我做这样的事情时,YII说错误输出 htmlspecialchars() expects parameter 1 to be string, array given

1 个答案:

答案 0 :(得分:1)

Yii afterfind()方法被覆盖到后处理 AR属性。

From the docs:

  

在查找实例化每条记录后调用此方法   方法。默认实现会引发onAfterFind事件。您   可以覆盖此方法以在每次新找到后进行后处理   记录被实例化。确保调用父实现   以便正确地提出事件。

所以它通常像:

protected function afterFind()
{

  $this->attribute_a = customize($this->attribute_a); //return customized attribute_a
  $this->attribute_b = customize($this->attribute_b); //return customized attribute_b
  ....
  parent::afterFind(); //To raise the event
}

我不确定你想做什么?但是你可能希望在每次查找后自动完成一些任务(填充一些数组)!!

在这种情况下,您可以在模型中定义公共数组:

$public car_arr = array();

然后在afterFind()中填充它,并且可以在视图中访问它。

相关问题