0

I have 3 tables:

  1. numbers (id, name)
  2. food (id, name)
  3. numbers_food (number_id, food_id, price_per_ad, price_per_ch)

How I can to get a price_per_ad and price_per_ch data for each food_id from numbers_food relationship table?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Footniko
  • 2,682
  • 2
  • 27
  • 36
  • Is `numbers_food` just a table or do you have the according model NumbersFood ? (The answer will depend on that) – darkheir Apr 22 '13 at 08:19
  • Thanks. I found the solution here: http://www.yiiframework.com/wiki/285/accessing-data-in-a-join-table-with-the-related-models/ – Footniko Apr 22 '13 at 17:23

1 Answers1

1

Just define relationships in models.

In NumberFoodModel:

'food'    => array(self::BELONGS_TO, 'Food',    'food_id'),

In FoodModel:

'number_food'    => array(self::HAS_MANY, 'NumberFood',    'food_id'),

Now in your code just use

Food::model()->with('number_food')->findByPk($id)
Preetam
  • 618
  • 6
  • 13