findAllBySql()
internally calls resetScopes()
so it cannot be done, only if you clone and roll your own findAllBySql
Use default scope ordering with a model::findAllBySql() call
https://stackoverflow.com/questions/22150265
- |
Question
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.
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow