Yii Framework Forum: Read All Child Records - Yii Framework Forum

Jump to content

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

Read All Child Records Rate Topic: -----

#1 User is offline   .D3 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 19-August 11

Posted 21 November 2012 - 11:38 PM

I am stuck into very simple but annoying problem.
If we have db something like this:

User -> Projects -> Tasks

Now if we follow the YII Active Record relations, we'll have following relations:

User -> 'hasMany' => Projects
Project -> 'belongsTo' => User
Project -> 'hasMany' => Tasks
Task -> 'belongsTo' => Project

So this is a common Project and task relationship.
Currently, I am doing:

$Criteria = new CDbCriteria;
$Criteria->with = array(
					'projects'=>array(
							'with'=>array(
								'tasks'
							)
						)
					);

$User::model()->find($Criteria);



foreach( User->Project as project ){
	foreach( project->Tasks as task){
		**** list tasks ****
	}
}


But want to list all the tasks of all the projects, order by TASK DATE, without looping on Project. How can I do that ? I have tried the following but it did not work:

foreach( User->Project->Task as task ){
	*** list tasks ***
}


Thanks
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 22 November 2012 - 04:17 AM

I've not tried it, but you may be able to set up a HAS_MANY relation to Tasks directly from User, going through your Project relation. You'd be able to set the order in the new relation.

I have no idea if that will work; I've never needed to use 'through', so it might work differently than I expect.
0

#3 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 22 November 2012 - 05:01 AM

Hi D3

why didn't use directly the Task model to get all the Tasks?
In addition if You want to get the related User or Project then load Tasks with Project or User...
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#4 User is offline   .D3 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 19-August 11

Posted 22 November 2012 - 06:27 AM

View PostKonApaz, on 22 November 2012 - 05:01 AM, said:

Hi D3

why didn't use directly the Task model to get all the Tasks?
In addition if You want to get the related User or Project then load Tasks with Project or User...



Thanks for the reply.
The reason why I want to go through user is that I want to get all the tasks of "that particular" user. So each user has his/her own projects and tasks.

Thanks
0

#5 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 22 November 2012 - 06:34 AM

I think what KonApaz was suggesting is that you do something like this:

$criteria = new CDbCriteria;
$criteria->with = 'Project.User';
$criteria->together = true;
$criteria->order = 't.date ASC';

$tasks = Tasks::model()->findAllByAttributes(array('User.id'=>$userId), $criteria);


That's pretty much pseudo-code, but it should give you an idea.
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