Yii Framework Forum: Effeciant way to iterate through large CActiveRecord - Yii Framework Forum

Jump to content

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

Effeciant way to iterate through large CActiveRecord Rate Topic: ***** 1 Votes

#1 User is offline   backwardselvis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 24-February 10
  • Location:Bend, OR

Posted 01 March 2010 - 07:05 PM

Yeah, another MANY to MANY question.

I have the following tables

children
guardians_children
guardians
carriers


The logical ownership could also be illustrated like this.

children->guardians_children->guardians->carriers

Here are the relations;

children:
public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'guardians_children' => array(self::HAS_MANY,'Guardians_Children','childrenId'),
		);
	}


guardians_children:
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'children' => array(self::BELONGS_TO,'Children','id'),
			'guardians' => array(self::HAS_MANY,'Guardians','id'),
		);
	}


guardians:
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'guardians_children' => array(self::BELONGS_TO, 'Guardians_Children', 'id'),
                        'carriers' => array(self::HAS_ONE,'Carriers','id'),
		);
	}


From the ChildrenController, I am calling a method in the Children model that returns all the related information about the child. And it works just fine, as when I DUMP the object, I can see that it is all there. But I am not understanding the most efficient way to iterate over the object to say, get the guardian's names, or the carrierId for example.
0

#2 User is offline   backwardselvis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 24-February 10
  • Location:Bend, OR

Posted 02 March 2010 - 05:26 PM

TIP: TURN ON LOGGING!

I wasn't joining the relations of the models correctly. Therefore the result wasn't really giving me what I thought it was. It was only by evaluating the underlying SQL, that I was able to correct the problem.

Here is the configuration for logging that I have enabled on my TEST instance.

../protected/config/main.php

		'log'=>array(
			'class'=>'CLogRouter',
			'routes'=>array(
				array(
					'class'=>'CFileLogRoute',
					'levels'=>'error, warning',
				),
                                array(
                                    'class'=>'CDbLogRoute',
                                    'levels'=>'error,warning',
                                    'autoCreateLogTable'=>true, //<------ be careful, this auto creates the table.
                                    'enabled'=>true,
                                    'connectionID'=>'db', //<------- pointer to the Yii::app()->db instance i believe.
                                ),
				// uncomment the following to show log messages on web pages
				array(
					'class'=>'CWebLogRoute',
				),
			),
		),

1

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