How to visualize joined items?

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()



		$criteria=new CDbCriteria;

		$pages=new CPagination(openings::model()->count());



		$sort=new CSort('openings');










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.

Posted Image

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>


      <?php echo CHtml::link('Update',array('update','id'=>$model->PK_ID)); ?>

      <?php echo CHtml::linkButton('Delete',array(



      	  'confirm'=>"Are you sure to delete #{$model->PK_ID}?")); ?>


The code generated by yiic is meant to be a starting point. You still need to customize it to fit for your needs.

Yes, Qiang, but why don't I have access to the property PROJECT. Is it possible not to have access?

$model->PROJECT gives the project object. I think you want to display one of the project's property.

PROJECT is not a property but an object, in particular an instance of "sentprojects".

In the view you have to write for example $model->PROJECT->description (imagining description a field of the table PROJECT) or any other field of the table PROJECT. When you do the with() in the query it returns an instance of the PROJECT that contains any info of the record.

Actually my two tables are:

CREATE TABLE `sentprojects` (

  `PROJECT_ID` int(11) NOT NULL auto_increment,

  `PROJECT` varchar(50) collate utf8_unicode_ci NOT NULL,




CREATE TABLE `openings` (

  `PK_ID` int(11) NOT NULL auto_increment,

  `PROJECT_ID` int(11) NOT NULL,

  `EMAIL_ID` int(11) NOT NULL,

  `KEY` varchar(16) collate utf8_unicode_ci NOT NULL,

  `OPENED` char(1) collate utf8_unicode_ci NOT NULL,




So, I have to join sentprojects, so that sentprojects.PROJECT_ID = openings.PROJECT_ID

and you say, that I should have access via $model->PROJECT->PROJECT(I think I have done wrong, by naming the relationship PROJECT, but I still don’t know everything very good :). What I wrote is based on my understanding of words of you both, so that

$model->PROJECT is the object. And it should contain any info of the record, as Ilzale said, so $model->PROJECT->PROJECT should return what I am looking for?

And actually it worked. Thank you both!