Complex relation table

Hey guys,

I’m new to yii, but have been dedicated on learning it properly for the last 2 weeks. I was wondering, how would be the proper way to handle relation tables which contains attributes. I’ve been searching around but couldn’t find much info about this situation specifically. I’ll give you an example.

I have three tables:



  • id

  • client_id


  • id

  • order_id

  • product_id

  • price

  • amount


  • id

  • name

  • price


So, [b]orders[/b] has a many to many relation to [b]products[/b] through [b]order_items[/b]. Now the question is:

When creating an Order the user will have to select Products, but, when I save the order, I want to create OrderItem and copy the value of price from the selected Product to the OrderItem. How would you do that?


Something like this?

$item = new OrderItem;

$product = CActiveRecord::model('Product')->findByPk($user_submitted_item_id);

$item->price = $product->price;

$item->amount = $user_submitted_item_amount;


I’m not sure why you’re calling this a complex relationship, it’s really pretty simple :).

I was actually concerned about how to display the relation in the create form. Check this topic: I believe it explains it better.

It sounds like you’re asking for help with UI design and it looks like you have your ideas down on the other thread. I don’t know of anything exactly like what you’re describing, but it should be doable with a combination of something like CGridView and CListView with some jQuery mixed in.

Also doesn’t sound that different from a shopping cart in terms of what’s going on in the database. You could check out yiishop: Doesn’t look like it’s maintained though.

BStep, yep! Not exactly the design, but a way to relate a model to another during creation of the first when there are columns to be filled in the relation table. I’ve seen widgets in Yii to deal with many to many relations, but none which deals with relation data.

I’ll have a look at yiishop. Thanks for the tip!

what if i want to use the same logic but for drop downs ? ive been asking this for weeks on one thread …

thanks to revert !

What exactly do you mean?

See this thread for more information, thanks to revert !


i have 3 tables: agent (id, user_id, agency_id) user (id, person_id) person (id, name, surname).

i want the user_id field in agent table to display the name and surname from the person table in a

drop down… any idea ?

cos this name, surname from my person table to display in a dropdown for the user’s field in the

create new agent is driving me nuts :(

is the relations functions for the agent model is ‘users’ => array(self::HAS MANY, ‘Person’, ‘user_id’) ?

if i could get a link on how to display the name,surname of the person table inside my dropdown field for choose user in my ‘create new agent’ form, i would be relieved