Yii Framework Forum: 2 Problems With Relational Active Records - Yii Framework Forum

Jump to content

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

2 Problems With Relational Active Records Rate Topic: -----

#1 User is offline   appinger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-November 12

Posted 22 November 2012 - 10:05 AM

Hello,

I'm new here and new with the Yii Framework.

Now I like to work with Relational Active Record.

Two Problems:

1.)

I'd like to count the number of trees in an area. A tree is in a city and a city is in a region. Now I would like to return the number of trees in the region with ID 1.

In region-model stands in the relations:

return array ('city' => array (self :: HAS_MANY, 'city', 'RegionlD'));

In the city-model stands in the relations:

return array ('tree' => array (self :: HAS_MANY, 'tree', 'StadtID'));

So how can I count the trees?


2.)

I like to select all trees in a Region with ID=1.

I tryed

Tree::model()->with('city')->with('region')->findAll('Region.ID=:id', array('id' => 1));

but the Tree Model does not know region.

Can anybody help me?

Thanks....
0

#2 User is offline   Keith 

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

Posted 22 November 2012 - 10:29 AM

For your second question, assuming you've already configured those relations, try:
Tree::model()->with('city.region')->findAllByAttributes(array('region.id'=>1)));


For your first question, you could retrieve all of the records as above and return the count of them, but that may be inefficient if your data set is large. Alternatively, look into using the STAT relation type or consider a custom query using PDO.

EDIT: Also, make sure you have a 'region' relation in your City model.
'region' => array(self::BELONGS_TO, 'Region', 'RegionID')

0

#3 User is offline   appinger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-November 12

Posted 22 November 2012 - 12:00 PM

Thanks for the answer.
In the city-model i also insert the relation
'region' => array(self::BELONGS_TO, 'Region', 'RegionID')


but there is the error:

table "Tree" has no column named "region.id"
0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 22 November 2012 - 12:12 PM

Dear Appinger

Would you please try this?

In Region Model.
return array (
       'city' => array (self :: HAS_MANY, 'city', 'RegionlD'),
       'tree' => array (self :: HAS_MANY, 'tree', array('id'=>'StadtID'),'through'=>'city'),
       
);


Then try doing following this.
$trees=Region::model()->findByPk(1)->tree;
echo count($trees);


Regards.
0

#5 User is offline   appinger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-November 12

Posted 23 November 2012 - 07:26 AM

Thanks!

This works fine.
But how can I do actions like order or limit the trees? For example order trees by size?
0

#6 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 23 November 2012 - 02:34 PM

Dear Appinger

We can do this way.
$trees=Region::model()->with(array('tree'=>array(
'select'=>'tree.id,site,owner,size',
'condition'=>'size<2',
'order'=>'size DESC'
)))->findByPk(1)->tree;


Regards
0

#7 User is offline   appinger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-November 12

Posted 27 November 2012 - 10:38 AM

Worked great!
Thanks....
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