Hey, guys.
Say, I have a model, called "Openings",
There is the relationships method:
/** * @return array relational rules. */ public function relations() { return array( 'PROJECT'=>array(self::HAS_ONE, 'sentprojects', 'PROJECT_ID'), ); }
Dunno, if I have specified the relation correctly.
In openings controller, actionAdmin.
public function actionAdmin() { $this->processAdminCommand(); $criteria=new CDbCriteria; $pages=new CPagination(openings::model()->count()); $pages->pageSize=self::PAGE_SIZE; $pages->applyLimit($criteria); $sort=new CSort('openings'); $sort->applyOrder($criteria); $openingsList=openings::model()->with('PROJECT')->findAll($criteria); $this->render('admin',array( 'openingsList'=>$openingsList, 'pages'=>$pages, 'sort'=>$sort, )); }
As you see, I've joined the tables, but what is the way to make Yii visualize PROJECT on the table, which it shows for Admin action? I mean, when you normally JOIN tables, you have access to all fields. Thus, I summarized that Yii should add all fields from the result and moreover, I read in the documentation, that it handles the result as single AR, created by joined tables.
My current table looks like this below. I don't want to have PROJECT_ID and EMAIL_ID available, I want to have their joined values, instead. At the moment I am doing it for PROJECT only, but I suppose it would be the same when I add EMAIL.
I modified view "admin", so that I add a row, displaying $model->PROJECT
Unfortunately, I am getting this error: htmlspecialchars() expects parameter 1 to be string, object given
It now looks like:
<?php foreach($openingsList as $n=>$model): ?> <tr class="<?php echo $n%2?'even':'odd';?>"> <td><?php echo CHtml::link($model->PK_ID,array('show','id'=>$model->PK_ID)); ?></td> <td><?php echo CHtml::encode($model->PROJECT_ID); ?></td> <td><?php echo CHtml::encode($model->EMAIL_ID); ?></td> <td><?php echo CHtml::encode($model->KEY); ?></td> <td><?php echo CHtml::encode($model->OPENED); ?></td> <td><?php echo CHtml::encode($model->DATE_OPENED); ?></td> <td><?php echo CHtml::encode($model->PROJECT); ?></td> <td> <?php echo CHtml::link('Update',array('update','id'=>$model->PK_ID)); ?> <?php echo CHtml::linkButton('Delete',array( 'submit'=>'', 'params'=>array('command'=>'delete','id'=>$model->PK_ID), 'confirm'=>"Are you sure to delete #{$model->PK_ID}?")); ?> </td>