I am having a strange problem, which I don’t know how to fix.
I am having this criteria to load trainings. I use named scopes to filter from relation.
$crit = New CDbCriteria;
$crit->with = array(
'customer' => array(
'select' => 'id, name, firstname, mail, junior, sex, bd, info',),
'customer.modul' => array(
'scopes' => array(
'season' => yii::app()->session['backend']->admin_saisonid,
),
),
'customer.filters' => array(
'scopes' => array(
'season' => yii::app()->session['backend']->admin_saisonid,
),
)
);
// $crit->together = true;
$crit->condition = 'customer.terms = 1 and customer.id=2589';
$crit->addInCondition('t.saison_id', array(yii::app()->session['backend']->admin_saisonid));
if (isset($c)) {
$crit->addCondition('customer.club_id=:clid');
$crit->params[':clid'] = $c;
}
$trainings = CustomerTraining::model()->findAll($crit);
the query runs correctly and returns the good rows, I checked in SQLyog
I have a query like:
SELECT
`t`.`id` AS `t0_c0`,
`t`.`customer_id` AS `t0_c1`,
`t`.`von` AS `t0_c2`,
`t`.`bis` AS `t0_c3`,
`t`.`flag` AS `t0_c4`,
`t`.`saison_id` AS `t0_c5`,
`t`.`client_id` AS `t0_c6`,
`t`.`wochentag` AS `t0_c7`,
`t`.`accepted` AS `t0_c8`,
`t`.`origin` AS `t0_c9`,
`t`.`special_module` AS `t0_c10`,
`t`.`customer_module_id` AS `t0_c11`,
`t`.`module_id` AS `t0_c12`,
`t`.`created` AS `t0_c13`,
`customer`.`id` AS `t1_c0`,
`customer`.`name` AS `t1_c1`,
`customer`.`firstname` AS `t1_c2`,
`customer`.`mail` AS `t1_c9`,
`customer`.`junior` AS `t1_c10`,
`customer`.`sex` AS `t1_c3`,
`customer`.`bd` AS `t1_c4`,
`customer`.`info` AS `t1_c12`,
`modul`.`id` AS `t2_c0`,
`modul`.`name` AS `t2_c1`,
`modul`.`preis` AS `t2_c2`,
`modul`.`inputs` AS `t2_c3`,
`modul`.`saison_id` AS `t2_c4`,
`modul`.`customer_id` AS `t2_c5`,
`modul`.`created` AS `t2_c6`,
`filters`.`id` AS `t3_c0`,
`filters`.`name` AS `t3_c1`,
`filters`.`customer_id` AS `t3_c2`,
`filters`.`saison_id` AS `t3_c3`,
`filters`.`label` AS `t3_c4`,
`filters`.`data` AS `t3_c5`,
`filters`.`created` AS `t3_c6`
FROM
`customer_training` `t`
LEFT OUTER JOIN `customer` `customer`
ON (
`t`.`customer_id` = `customer`.`id`
)
LEFT OUTER JOIN `customer_module` `modul`
ON (
`modul`.`customer_id` = `customer`.`id`
)
AND (modul.saison_id = 25)
LEFT OUTER JOIN `customer_filter` `filters`
ON (
`filters`.`customer_id` = `customer`.`id`
)
AND (filters.saison_id = 25)
WHERE (
(
(customer.terms = 1)
AND (t.saison_id = 25)
)
AND (customer.club_id = 25)
)
AND customer.id=2589
but after I start printing contents, the $training->customer->module->id is NOT from the rows the DB returns, it’s randomly one row (which may be the result of the relation as the “module” relation doesn’t have it’s scope defined)
How can I get so the correct module is being used from the query?