php - Yii 1.1 中的默认范围

标签 php activerecord yii

AR模型玩家:

public function scopes()
{
    return array(
        'proleague' => array(
            'condition' => 'mode = "proleague"',
        ),
        'main' => array(
            'condition' => 'mode = "main"',
        ),
    );
}

使用模型播放器:

Player::model()->
proleague()->
with('startposition')->
findAllByAttributes(... here some condition ...);

^^^ 没关系。范围条件将被执行。但是...


在我的项目中,有很多地方没有指定玩家模型的范围,在这种情况下,我需要使用这个范围条件作为默认值:

        'main' => array(
            'condition' => 'mode = "main"',
        )

如果我像这样将 defaultScope() 方法添加到 Player 模型

public function defaultScope()
{
    return array(
        'condition' => 'mode = "main"',
    );
}

下一个代码

Player::model()->
proleague()->
with('startposition')->
findAllByAttributes(... here some condition ...);

无法正确运行。我不会得到 mode = "proleague" 条件,因为我将使用 defaultScope()mode = "main"。 p>


有什么建议吗?我该如何解决这个问题?

最佳答案

您应该只使用 resetScope(true)方法。它“删除”defaultScope 过滤器。

$model = Player::model()->resetScope(true)->proleague();

关于php - Yii 1.1 中的默认范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29817435/

相关文章:

ruby-on-rails - 无需第二次查询即可获取 Rails 记录数

activerecord - yii事件记录联接模型

php - yii 使用的 ORM 是什么?

php - Sql 字符串查询不起作用

php - 防止重复显示 mysql 数据库中的类别

PHP 图片标题和 JavaScript 加载

php - 每次使用变量时都会调用 PHP 函数吗?

ruby-on-rails - 在 Rails 中访问只读数据库的最佳方式

php - Yii 事务不回滚

php - 如何在 Laravel 中通过 Eloquent 向模型添加字段?