yii检测作用域是否正在使用
我想知道是否有一种方法可以检测yii中AR搜索是否使用了范围 例如,模型可能包含两个作用域:yii检测作用域是否正在使用,yii,scopes,named-scopes,Yii,Scopes,Named Scopes,我想知道是否有一种方法可以检测yii中AR搜索是否使用了范围 例如,模型可能包含两个作用域: class MyModel extends CActiveRecord { ... function myScope1() { $this->getDbCriteria()->mergeWith(array( 'join'=>'etc...', 'condition'=>'foo = bar'
class MyModel extends CActiveRecord
{
...
function myScope1()
{
$this->getDbCriteria()->mergeWith(array(
'join'=>'etc...',
'condition'=>'foo = bar',
));
return $this;
}
function myScope2()
{
$this->getDbCriteria()->mergeWith(array(
'join'=>'etc...',
'condition'=>'foo2 = bar2',
));
return $this;
}
....
}
我这样给AR打电话:
$results = MyModel::model()->myScope1()->myScope2()->findAll();
这是一个非常动态的站点,有两个以上的作用域,一些已使用,一些未使用。如果正在使用另一个作用域,则有几个作用域不应应用。为了避免数百条if-else
语句,我可以这样做:
class MyModel extends CActiveRecord
{
...
function myScope1()
{
$this->getDbCriteria()->mergeWith(array(
'condition'=>'foo = bar',
));
return $this;
}
function myScope2()
{
if($this->appliedScopes('myScope1')==false)
{
// scope myScope1 isn't applied, so apply this scope:
$this->getDbCriteria()->mergeWith(array(
'condition'=>'foo2 = bar2',
));
}
return $this;
}
....
}
$results = MyModel::model()->myScope1()->myScope2(false)->findAll();
典型的情况是,几乎在发布后立即想到解决方法 在我的例子中,在两个范围之间应用“或”将起作用,因此
class MyModel extends CActiveRecord
{
...
function myScope1()
{
$this->getDbCriteria()->mergeWith(array(
'join'=>'etc...',
'condition'=>'foo = bar',
));
return $this;
}
function myScope2($useAnd=true)
{
$this->getDbCriteria()->mergeWith(array(
'join'=>'etc...',
'condition'=>'foo2 = bar2',
),$useAnd);
return $this;
}
....
}
这样称呼:
class MyModel extends CActiveRecord
{
...
function myScope1()
{
$this->getDbCriteria()->mergeWith(array(
'condition'=>'foo = bar',
));
return $this;
}
function myScope2()
{
if($this->appliedScopes('myScope1')==false)
{
// scope myScope1 isn't applied, so apply this scope:
$this->getDbCriteria()->mergeWith(array(
'condition'=>'foo2 = bar2',
));
}
return $this;
}
....
}
$results = MyModel::model()->myScope1()->myScope2(false)->findAll();