Relation help

Hi, I’m new to Yii and having a hard time figuring out how to get fields from the join table…

My tables are as follows:

group

  • group_id

  • name

group_field

  • group_id

  • field_id

  • field_order

field

  • field_id

  • field_name

  • type

  • desc

The relation is:


return array(

	'fields' => array(self::MANY_MANY, 'Field', 'group_field(group_id, field_id)',

);

This works fine and return all the associated fields for a group.

My problem is - how do I get the field_order column for each Field object in the ‘fields’ array that’s returned?

It’s probably a simple thing…I just can’t seem to figure it out :unsure:

can someone help please…? really stuck on this one…

The table group_field acts as a go-between, so the Many->Many relationship works. As such, you’re not directly given access to it. You have a few options:

[list=1]

[*]Move field_order to the "fields" table

[*]Perform another query to get the field order for each field:

[/list]


foreach($fields as $individual_field) {


  $field_order = GroupField::model()->findByPk($individual_field->field_id)->field_order;


}

It’s not particularly clean, but it’s a starting point.

I had a similar case like yours, and I ended up with a combination of 2 relations.

  1. a group HAS_MANY group_fields

  2. a group_field BELONGS_TO a field

I don’t know for sure whether it has to be this way or not, but I couldn’t think of a smart way. :(

Hi to both, and thanks for the response!

@georgebuckingham: I agree, gets the job done! One main concern with this approach is the performance hit for running a query for each field. Suppose I could get all the field_ids and run a "IN" query along with the group_id. Pseudo SQL - Sel field_id from group_field where group_id = n and field_id IN ( field_ids… ).

I was hoping of a straight-forward way of accessing the field_order via the matched Field Models.

@softark: when I look at that relationship, my head keeps saying “noo, that’s not right!” ha ha, no offence! But I might give it a try also…good to try out variations in my n00b state! :P

Thanks again, really appreciate the help!

Never! I don’t mind. :)

I myself would like to have some advice from experienced people regarding this.

This is a decent way to build it, as you’re representing the fact that the join table has data in its own right. I.e. it’s an association class (http://www.agilemodeling.com/style/classDiagram.htm). I would now take a look at the new through ability of relations though (http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through).

Hi Say_Ten, the ‘through’ functionality works like a charm! Thanks for pointing that out. I did have a look at it previously but didn’t really comprehend it fully. But had another look, at your direction and it all fell in to place. :D

So my relations now are:




'groupFieldMap' => array(self::HAS_MANY, 'GroupField', 'group_id', 'order'=>'field_order'),

'fields'=> array(self::HAS_MANY, 'Field', 'field_id', 'through' => 'groupFieldMap'),



Beauty!!

Thanks, Say_Ten and netmek.

I will try it later. :)