yii关联

来源:互联网 发布:sleepyhead知乎 编辑:程序博客网 时间:2024/05/12 06:30
CDetailView中使用方法
<?php $this->widget('zii.widgets.CDetailView', array('data'=>$model,'attributes'=>array('id',array('name'=>'pid','value'=>CHtml::encode($model->p->name)),'name',),)); ?>
'zii.widgets.grid.CGridView使用
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'a-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns'=>array(  'id',  'pid',  'name',  array(   'class'=>'CButtonColumn',  ), ),)); ?>
$model->search()中使用with
$criteria->with = 'p';
模型中添加方法
protected function afterFind() {  if (isset($this->p) || !is_object($this->p))  {   $this->p = B::model()->findByPk($this->pid);  }  if (is_object($this->p) && isset($this->p->name))  {   $this->pid = $this->p->name;  } }
原创粉丝点击