relation() not working

Hi all,

For some reason my relation is not working. I have the following two tables:

ppl_details //table name

id

person_id //foreign key corresponding to people.id

test_type

score

people //table name

id

name

age

I would like to perform a relational query in my list action which lists all ppl_details in a way so I can get the name of a person from the people table (people.name).

In my ppl_details model I have the following relation:


return array(

     'ppls'=>array(self::BELONGS_TO,'People','id'),

);

In my controller I have the following code:


public function actionList()

	{

		$criteria=new CDbCriteria;

                $criteria->with = array('ppls');


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

		$pages->pageSize=self::PAGE_SIZE;

		$pages->applyLimit($criteria);


        $models=Ppl_details::model()->findAll($criteria);


		$this->render('list',array(

			'models'=>$models,

			'pages'=>$pages,

		));

	}



and the above relation is not working. When I try to access some of the people table data via $model->name it is not returning any value. I am using foreach in the view to iterate $models.

Can someone help me understand what I’ve got wrong in my code. I am using Yii 1.1 RC and one reacord from ‘people’ table corresponds to one record from ppl_details.

Thanks!

I’m not sure but maybe it has to do with this bug. It was fixed in revision r1596, so if you want to test it you have to download it from svn.

At least you’ll have to use the foreign key in the relationship declaration.




return array(

     'ppls'=>array(self::BELONGS_TO,'People','person_id'),

);



/Tommy

I updated my framework with the latest fixes but still no help. I also tried

$models=Ppl_details::model()->with(‘ppls’)->findAll($criteria); … not sure if I have to do anything additional here. Then when I try to access the data via $model->name and it is blank.

Tommy:

I have tried id and person_id but does not help.

Additionally, I also have other tables which I need to relate:

the below table represents the team work of students

Tbl_rank

id

team_id

games

played

points

the below table represents details about each team

Tbl_teams

id

team_name

In my Tbl_rank model I have related the above tables as below:


'team'=>array(self::BELONGS_TO,'Tbl_teams','team_id'),

I also tried to related them with HAS_ONE but still no success

Tbl_rankController:


public function actionList()

	{

		$criteria=new CDbCriteria;


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

		$pages->pageSize=self::PAGE_SIZE;

		$pages->applyLimit($criteria);


        $models=Tbl_rank::model()->with('team')->findAll($criteria);




		$this->render('list',array(

			'models'=>$models,

			'pages'=>$pages,

		));

	}

In my list view file:


foreach($models as $n=>$model): ?>

<div class="item">

<?php echo CHtml::encode('Team name'); ?>:

<?php echo CHtml::encode($model->team_name); ?>

<br />

<?php echo CHtml::encode($model->getAttributeLabel('played')); ?>:

<?php echo CHtml::encode($model->played); ?>

</div>

<?php endforeach; ?>

$model->team_name should be a record from the related table but Yii returns blank. $model->played [which is a record from the current model/table] gets populated correctly. So clearly the relation breaks down somewhere.

Thanks in advance for you help. I tried to relate the tables the PDO way but then I am stuck as it does not support $criteria therefore the pagination and sorting functions are unavailable so I must go with relations.

Any help is highly appreciated.

Regards,

B.




return array(     'ppls'=>array(self::BELONGS_TO,'People','id'),);



why not under people model?:




return array(     'ppl_details'=>array(self::BELONGS_TO,'person_id','id'),);