I have defaultScope() defined in my model class X to

return (array('order'=>'title ASC'));

When I run a findAllBySql() as follows:

 X::model()->findAllBySql(
   'SELECT *
      FROM x
     WHERE x.id NOT IN (
        SELECT y.x_id
          FROM y
         WHERE y.id = :y_id
        )',
   array(
    'mf_list_id'=>$y->id,
   )
 );

I was hoping to see the returned members of X in order ascending title. Unfortunately this does not appear to be the case.

Is there any way to use the default scope with this query? I am dynamically constructing the sql, so simply adding an order clause or criteria to the findAllBySql() call is undesirable.

Thanks in advance.

有帮助吗?

解决方案

findAllBySql() internally calls resetScopes() so it cannot be done, only if you clone and roll your own findAllBySql

其他提示

For anyone stumbling across this, I settled on this solution:

Instead of running findAllBySql() I run the following:

$criteria=new CDbCriteria();
$criteria->condition='
  id NOT IN (
    SELECT y.x_id
      FROM y
      WHERE y.id = :y_id
  )';
$criteria->params=(array('y_id'=>$y->id));
X::model()->findAll($criteria);

This uses the default scope from X and orders by title as desired.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top