문제

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