Добрый день возникла проблема с запросом связанных таблиц.
Структуры таблиц не привожу за ненадобностью.
Запрос выполняет тривиальную задачу - Выборку записи из таблицы А и связывание по атрибуту resource_id таблицы Б с условием что атрибут таблицы Б type = значению (в моем случае строка one_image)
в модели описываю правило
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'captionimage' => array(self::HAS_ONE, 'Files', 'resource_id',
'condition' => 'type = "one_image"',
)
);
}
В контроллере
$model = Theory::model()->with('captionimage')->find('user_id=:user_id AND id = :id', array(
':user_id' => Yii::app()->user->Id,
':id' => $id
));
[b]
Фреймворк генерирует запрос[/b]
SELECT `t`.`id` AS `t0_c0`, `t`.`category_id` AS `t0_c1`, `t`.`tp_id` AS `t0_c2`, `t`.`tp_sub_cat_id` AS `t0_c3`, `t`.`subject` AS `t0_c4`, `t`.`body` AS `t0_c5`, `t`.`user_id` AS `t0_c6`, `t`.`date_create` AS `t0_c7`, `captionimage`.`file_id` AS `t1_c0`, `captionimage`.`type` AS `t1_c1`, `captionimage`.`path` AS `t1_c2`, `captionimage`.`resource_id` AS `t1_c3`
FROM `theory` `t`
LEFT OUTER JOIN `files` `captionimage` ON (`captionimage`.`resource_id`=`t`.`id`)
WHERE (user_id=:user_id AND id = :id) AND (type = "one_image")
Все вроде бы нормально, но мне не нужно что бы то условие которое я добавил в правилах моей модели не относилось к запросу в целом.
Нужно что бы запрос выглядел так:
SELECT t
.id
AS t0_c0
, t
.category_id
AS t0_c1
, t
.tp_id
AS t0_c2
, t
.tp_sub_cat_id
AS t0_c3
, t
.subject
AS t0_c4
, t
.body
AS t0_c5
, t
.user_id
AS t0_c6
, t
.date_create
AS t0_c7
, captionimage
.file_id
AS t1_c0
, captionimage
.type
AS t1_c1
, captionimage
.path
AS t1_c2
, captionimage
.resource_id
AS t1_c3
FROM theory
t
LEFT OUTER JOIN files
captionimage
ON (captionimage
.resource_id
=t
.id
) AND (type = "one_image")
WHERE (user_id=:user_id AND id = :id)