relations question

I have the following tables:

Campaigns

  • id

  • name

Build

  • id

  • campaign_id

  • name

BuildParams

  • id

  • build_id

  • name

One campaign has many builds and a build has many buildparams.

How can I get all the buildparams based on a campaign id?

Make sure to set up your relations in your objects, and then you can call

$criteria = new CDbCriteria();

$criteria->condition = ‘campaign_id = :campaign_id’

$criteria->params = array(’:campaign_id’ => $campaign_id);

then you can do either

Build::model()->with(‘Campaigns’, ‘BuildParams’)->findAll($criteria);

And you should get everything from all three tables. You can find more info on setting up relationships here:

http://www.yiiframework.com/doc/guide/database.arr

Please let me know if you still have trouble after reading these docs and I can try to help you with your relationships as well.

Corey

Hi Corey,

thanks for your answer.

Can you explain how to set up the relations in the objects.

It is not really clear to me how to do this.

Thanks!

hi heyhoo,

did you check this chapter from the guide ?

Note that if you use yiic (or even better : gii) to generate automatically your model classes, relations are automatically created. Then take a look at the relation method to understand better.

Hope this helps

8)

Thanks for the answers. I have figured it out :)