Yii Framework Forum: How can I get columns after relation query? - Yii Framework Forum

Jump to content

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

How can I get columns after relation query? Rate Topic: -----

#1 User is offline   andser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 12-February 10

Posted 14 May 2010 - 08:00 AM

Hi!

I have 3 tables :
Posted Image

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.
0

#2 User is offline   Jaz Manister 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 27-October 09
  • Location:Rural Western New York, USA

Posted 14 May 2010 - 08:16 AM

View Postandser, on 14 May 2010 - 08:00 AM, said:

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,
  );
}

0

#3 User is offline   andser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 12-February 10

Posted 14 May 2010 - 08:27 AM

View PostJaz Manister, on 14 May 2010 - 08:16 AM, said:

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!
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