I have Page
model which HAS_MANY
Attachment
In Page model:
public function relations()
{
return array(
'attachments'=>array(self::HAS_MANY, 'Attachment', 'parent_id'),
)
}
and I am looking for a way to do some scoping on these attachments.
In the PageController
I have:
$model = Page::model()->with(array('attachments'))->findByAttributes(array('slug' => $slug))
For example in the Page view I would like to:
$model->attachments
(this works fine), but also I need:status
= 1)promoted
= 1)mime_type
in_array 'image/jpeg', 'image/gif', ...
)and any combination of them. Ex: the first promoted and published image
I guess that the best option is to do it without any extra queries and just filter the $model->attachments
, but is it possible?
Edit:
there is one pages
table and another attachments
table
in the attachments table I have: id
, parent_id
, file_name
, mime_type
, status
, promoted
you can do like this:
$posts=Post::model()->with(array(
'comments'=>array(
'scopes'=>array('recently','approved')
),
))->findAll();
// or since 1.1.7
$posts=Post::model()->findAll(array(
'with'=>array(
'comments'=>array(
'scopes'=>array('recently','approved')
),
),
));
http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-named-scopes