Note that there are some explanatory texts on larger screens.

plurals
  1. POapplying a default scope with reference to a relation in yii
    primarykey
    data
    text
    <p>I can't find too much documentation on applying a default scope to a model in yii, I was wondering if someone could explain or point me in the right direction.</p> <p><strong>The quick version of my question:</strong></p> <p>Is it possible to add a relation to a default scope, or to add a 'with' criteria by default to every AR search on a model?</p> <p><strong>The long version of my question:</strong></p> <p>A quick summary of my app:</p> <p>I have two models, <code>provider</code> and <code>item</code>. Which have a m:1 relationship where a provider can have many item's, but each item can only have one provider.</p> <p>So far I have these relations:</p> <pre><code>class Provider extends CActiveRecord { ... public function relations() { return array( 'items' =&gt; array(self::HAS_MANY, 'Item', 'id_provider', 'order'=&gt;'rank DESC'), ); } ... } class Item extends CActiveRecord { ... public function relations() { return array( 'provider' =&gt; array(self::BELONGS_TO, 'Provider', 'id_provider'), ); } ... } </code></pre> <p>Within my items model I've already got a defaultScope which filters out all offline items (i.e. only displays items which are set to <code>offline = false</code>):</p> <pre><code>public function defaultScope() { $alias = $this-&gt;getTableAlias(false,false); return array( 'condition'=&gt;"`$alias`.`offline` = false", ); } </code></pre> <p>What I want to do now, is also filter out items where their provider is set to offline (i.e. only show items where <code>provider.offline = false</code> alongside the current <code>item.offline = false</code>).</p> <p>I've tried joining the providers table in the defaultScope:</p> <pre><code>public function defaultScope() { $alias = $this-&gt;getTableAlias(false,false); return array( 'join'=&gt;"JOIN `provider` AS `provider` ON `provider`.`id` = `$alias`.`id_provider`", 'condition'=&gt;"`$alias`.`offline` = false AND `provider`.`offline` = false", ); } </code></pre> <p>However the JOIN applies after the ON statement, and causese an error (<code>CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'provider.offline' in 'on clause'</code>).</p> <p>I've also tried adding a with criteria to the defaultScope:</p> <pre><code>public function defaultScope() { $alias = $this-&gt;getTableAlias(false,false); return array( 'with'=&gt;"provider", 'condition'=&gt;"`$alias`.`offline` = false AND `provider`.`offline` = false", ); } </code></pre> <p>But I get the same error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'provider.offline' in 'on clause'`).</p> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload