Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
yii检测作用域是否正在使用_Yii_Scopes_Named Scopes - Fatal编程技术网

yii检测作用域是否正在使用

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'

我想知道是否有一种方法可以检测yii中AR搜索是否使用了范围

例如,模型可能包含两个作用域:

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();