title: Yii scope related models
body: <p>I have <code>Page</code> model which <code>HAS_MANY</code> <code>Attachment</code>
  In Page model:</p>
  
  <pre><code>public function relations()
  {
      return array(
          'attachments'=&gt;array(self::HAS_MANY, 'Attachment', 'parent_id'),
      )
  }
  </code></pre>
  
  <p>and I am looking for a way to do some scoping on these attachments.</p>
  
  <p>In the <code>PageController</code> I have:</p>
  
  <pre><code>    $model = Page::model()-&gt;with(array('attachments'))-&gt;findByAttributes(array('slug' =&gt; $slug))
  </code></pre>
  
  <p>For example in the Page view I would like to:</p>
  
  <ul>
  <li>get all attachments: <code>$model-&gt;attachments</code> (this works fine), but also I need:</li>
  <li>get all published attachments (all with <code>status</code> = 1)</li>
  <li>get first promoted attachment (the first one with <code>promoted</code> = 1)</li>
  <li>get only the images (<code>mime_type</code> in_array <code>'image/jpeg', 'image/gif', ...</code>)</li>
  <li>get all other files (everything that is not an image)</li>
  </ul>
  
  <p>and any combination of them. Ex: the first promoted and published image</p>
  
  <p>I guess that the best option is to do it without any extra queries and just filter the <code>$model-&gt;attachments</code> , but is it possible?</p>
  
  <p>Edit:</p>
  
  <p>there is one <code>pages</code> table and another <code>attachments</code> table</p>
  
  <p>in the attachments table I have: <code>id</code>, <code>parent_id</code>, <code>file_name</code>, <code>mime_type</code>, <code>status</code>, <code>promoted</code></p>