Yiii 后查找传递数组以查看


yii afterfind pass array to view

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

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

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

从文档中:

此方法在查找实例化每条记录后调用 方法。默认实现引发 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()中,它将在视图中访问。