How can I get columns after relation query?

Hi!

I have 3 tables :

Relations in Products are :


public function relations()

        {

                return array(

                        'attribute_values' => array(self::HAS_MANY, 'AttributeValue', 'product_id'),

                );

        }

In AttributeValue :


        public function relations()

        {

                return array(

                        'attribute' => array(self::BELONGS_TO, 'Attribute', 'attribute_id'),

                        'product' => array(self::BELONGS_TO, 'Product', 'product_id'),

                );

        }

In Attribute :


        public function relations()

        {

                return array(

                        'attribute_values' => array(self::HAS_MANY, 'AttributeValue', 'attribute_id'),

                );

        }

I execute this code :


$product_id = 1;

$criteria = new CDbCriteria;

$criteria->condition = 't.id='.$product_id;

$criteria->with = array(

  'attribute_values.attribute'

);

$attributes=Product::model()->find($criteria);


foreach($attributes as $attribute)

      $arr[] = array(

   'id'=>// Here I need Product.id

   'name'=>// Here I need Attribute.name

   'value'=>// And here - AttributeValues.value

        );

I need all names from Attribute.name and their values from AttributeValues.value for product.

How can I get Product.id, Attribute.name, AttributeValues.value in this cycle?

Thank you.

Relationships can be referenced lazily. There’s no need to pull them in up front. Also, you don’t need to use criteria in this case…




$product = Product::model()->findByPk($product_id);

$attributes = array();

foreach($product->attribute_values as $attribute_value){

  $attributes[] = array(

    'id'=>$product->id,

    'name'=>$attribute_value->attribute->name,

    'value'=>$attribute_value->value,

  );

}



Wow! So simple! Thank you! It’s working perfect!