Relational Active Record

Hi,

I read that "Active Record Relation join several related database tables and bring back the joint data set."

but when i am running the below mentioned code:

Controller(Restaurant):

	 $restaurants = $model->findAll($condition);


	 $arr = array();


	 foreach($restaurants as $t)


	 {


		$arr[$t->id] = $t->attributes;


	 }


	 print_r($arr);

[u][b]

Relations are[/b][/u]

Model(Restaurant):

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(


	'merchants'=>array(self::BELONGS_TO,'Merchant','merchant_id'),


	);


}

Model(Merchant):

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(


		'restaurants'=>array(self::HAS_MANY,'Restaurant','merchant_id'),


	);


}

it is returning

Array

(

[3] => Array


    (


        [id] => 3


        [merchant_id] => 1


        [name] => BRestaurant One


        [address] => BTest Address One


        [website] => www.bwebsiteOne.com


        [created_date] => 2012-12-14 11:48:19


        [modified_date] => 2013-01-08 06:05:20


        [created_user] => 1


        [isblocked] => N


        [isdeleted] => N


    )

)

that means attributes like name in merchant table is not coming in result array which should be if relation means join of two tables.

[color="#006400"]/* Moved from "Yii-powered Applications" to "General Discussions for Yii 1.1.x" */[/color]

if you add "with" …

$restaurants = $model->with(‘merchants’)->findAll($condition);

than you will get and attributes from table for who relatios ‘merchants’ is.

but if you dont add with(‘merchants’), you can get attributes but there will be one more select from database

try this to see

$restaurants = $model->with(‘merchants’)->findAll($condition);

$arr = array();

foreach($restaurants as $t)

{

 $arr[$t->id] = $t->attributes;

}

print_r($arr);

and then try this

$restaurants = $mode->findAll($condition);

$arr = array();

foreach($restaurants as $t)

{

 $arr[$t->id] = $t->attributes;


 print_r($t->merchants);

}

print_r($arr);

thanks for your reply

but when i am writing this

$restaurants = $model->with(‘merchants’)->findAll($condition);

$arr = array();

foreach($restaurants as $t)

{

 $arr[$t->id] = $t->attributes;

}

print_r($arr);

it is returning :

Array ( [3] => Array ( [id] => 3 [merchant_id] => 1 [name] => BRestaurant One [address] => BTest Address One [website] => www.bwebsiteOne.com [created_date] => 2012-12-14 11:48:19 [modified_date] => 2013-01-08 06:05:20 [created_user] => 1 [isblocked] => N [isdeleted] => N ))

only attributes of restaurant table but not returning the matched attribute value of merchant table as join query returns.

truy this:

$restaurants = $model->with(‘merchants’)->findAll($condition);

CVarDumper::dump($restaurants,10,true);

yes i can see the related record with this.but how can i loop through this chunk of objects to get the array of main table value(this i know,my code is representing the data only of main table) and related table together.

Im not too sure what is your output. but to loop through array of objects you can do some thing like this…




 $model = Model::model()->findAllByAttributes(array('activity_id' => $id,'status'=>1,),array('order'=>'created DESC', 'limit'=>3));//it is returning array of objects here

//you can loop through like this:


        $reviewArray = array();

        foreach($model as $reviews)

        {

            $reviewArray[] = array(

                'id' => $reviews->id,

                //'post_id' => $reviews->post_id,

                'text' => $reviews->text,

                'created' => $reviews->created,

                'user' => array(

                    'first_name' =>$reviews->user->first_name,

                    'last_name' =>$reviews->user->last_name,

                  

                    ),

            );

        }

return $reviewArray;



hope it helps you :)