There are 4 tables:
results: ResID | ResCodeConc | ResName | ResDate
marks (points): MarkID | MarkCodeResult | MarkExp
conclusions (conclusion): ConcID | Concname
conc_fields (fields): FieldID | FieldCodeNameConc | Fieldname

One result has many points, one-to-many relationship.
The result is of type (ResCodeConс) from the table of conclusions, one to one relation.
In turn, the conclusions (conclusions) has many fields, one to many.

I did it in the Results model:

'marks' => array(self::HAS_MANY, 'Marks', 'MarkCodeResult'), 'conclusions' => array(self::BELONGS_TO, 'Conclusions',array('ResCodeConc '=>'ConcID ')), 'conc_fields' => array(self::HAS_MANY, 'ConcFields', array('ConcID '=>'FieldCodeNameConc'),'through'=>'conclusions') 

Separately, everything works, i.e .: separate conclusion fields:

 foreach($results->conc_fields as $field) echo $field->FieldName; 

separate points:

 foreach($results->marks as $mark) echo $mark->MarkExp; 

But you can't do it like this, the name of the field is the FieldName score | MarkExp

  • The final query in the database looks like? - andrew68
  • Did not understand what request? A request that is already formed on the relationship? Separate queries: SELECT f.FieldName FROM conc_fields INNER JOIN conclusions ON ON f.FieldCodeNameConc = n.ConcID SELECT m.MarkExp FROM marks m INNER JOIN results

1 answer 1

The request itself should be like this.

 SELECT conc_fields.FieldName, marks.MarkExp FROM conc_fields, results, marks WHERE (results.ResCodeConc=conc_fields.FieldCodeNameConc) and (results.ResID=marks.MarkCodeResult) 

in general, it should be rolled up like this

 $results=Results::model()->with(array('marks','conc_fields'))->findAll(); foreach($results as $rows){ print $rows->marks->MarkExp; print $rows->conc_fields->FieldName; } 

it was difficult for me to understand without a scheme alt text