Yii Framework Forum: relation() not working - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

relation() not working Rate Topic: -----

#1 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 02 January 2010 - 05:22 AM

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!
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 02 January 2010 - 05:40 AM

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.
0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 02 January 2010 - 07:28 AM

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#4 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 03 January 2010 - 04:28 AM

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.
0

#5 User is offline   megabr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 23-March 09
  • Location:Brazil

Posted 08 January 2010 - 08:33 AM

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'),);

trying build code for Generate complete CRUD source code with YII... any help?
post comments here:
EXTENDING CRUD GENERATION (scaffolding)

old tentative released (module generator):
http://www.yiiframew...sion/modulegen/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users