title: How to I fetch rows from one table via linked tables in Yii?
body: <p>The programmer who was working for the company I used has quit, so I'm stuck with learning Yii myself. I think the solution for this is easy if you know Yii, but for me it's not so obvious.</p>
  
  <p>I have three tables:</p>
  
  <pre><code>| Store | Brand | link_store_brand |
  |-------|-------|------------------|
  | id    | id    | storeID          |
  |       |       | brandID          |
  </code></pre>
  
  <p>Now I need to fetch all brands that is associated with store</p>
  
  <p>I have this code in my Store model: </p>
  
  <pre><code>public function relations()
  {
       return array(
          'brands' =&gt; array(CActiveRecord::MANY_MANY,
              'Brand',
              'link_store_brand(storeID, brandID)',
              'order' =&gt; 'brands.rank DESC, brands.name ASC',
              'with' =&gt; 'logo'),
       );
  }
  </code></pre>
  
  <p>So in my controller I do this:</p>
  
  <pre><code>    $store          = Store::model()-&gt;findByPk($requestData-&gt;id);
      $brands         = $store-&gt;getBrands();
  </code></pre>
  
  <p>I know I can do <code>Store::model()-&gt;with('brands')-&gt;findByPk()</code>. But a field naming issue makes the SQL query fail. So I'm first retrieving the store data, then fetching the brands.</p>
  
  <p>But <code>$store-&gt;getBrands()</code> is not working (SQL fails) so I think I have to use query builder and make my own query to fetch brands. But how do I do that?</p>