Yii 1.1 中的默认作用域
Default scope in Yii 1.1
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 ...);
^^^ 没关系。将执行范围条件。但是...
在我的项目中,我有很多地方没有指定 Player 模型 的任何范围,在这种情况下,我需要使用此 scope-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".
有什么建议吗?我该如何解决这个问题?
为此创建一个新的 Class。
<?php
## e.g. protected/models/
class MyCoreAR extends CActiveRecord
{
/**
* Switch off the default scope
*/
private $_defaultScopeDisabled = false; // Flag - whether defaultScope is disabled or not
public function setDefaultScopeDisabled($bool)
{
$this->_defaultScopeDisabled = $bool;
}
public function getDefaultScopeDisabled()
{
return $this->_defaultScopeDisabled;
}
public function noScope()
{
$obj = clone $this;
$obj->setDefaultScopeDisabled(true);
return $obj;
}
// see http://www.yiiframework.com/wiki/462/yii-for-beginners-2/#hh16
public function resetScope($bool = true)
{
$this->setDefaultScopeDisabled(true);
return parent::resetScope($bool);
}
public function defaultScope()
{
if(!$this->getDefaultScopeDisabled()) {
return array(
'condition' => 'mode = "main"',
);
} else {
return array();
}
}
}
在您的代码中:
// no default scope
$model = Player::model()->noScope()->proleague();
// with default scope
$model = Player::model()->proleague();
您应该只使用 resetScope(true) 方法。它 "removes" defaultScope 过滤器。
$model = Player::model()->resetScope(true)->proleague();
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 ...);
^^^ 没关系。将执行范围条件。但是...
在我的项目中,我有很多地方没有指定 Player 模型 的任何范围,在这种情况下,我需要使用此 scope-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".
有什么建议吗?我该如何解决这个问题?
为此创建一个新的 Class。
<?php
## e.g. protected/models/
class MyCoreAR extends CActiveRecord
{
/**
* Switch off the default scope
*/
private $_defaultScopeDisabled = false; // Flag - whether defaultScope is disabled or not
public function setDefaultScopeDisabled($bool)
{
$this->_defaultScopeDisabled = $bool;
}
public function getDefaultScopeDisabled()
{
return $this->_defaultScopeDisabled;
}
public function noScope()
{
$obj = clone $this;
$obj->setDefaultScopeDisabled(true);
return $obj;
}
// see http://www.yiiframework.com/wiki/462/yii-for-beginners-2/#hh16
public function resetScope($bool = true)
{
$this->setDefaultScopeDisabled(true);
return parent::resetScope($bool);
}
public function defaultScope()
{
if(!$this->getDefaultScopeDisabled()) {
return array(
'condition' => 'mode = "main"',
);
} else {
return array();
}
}
}
在您的代码中:
// no default scope
$model = Player::model()->noScope()->proleague();
// with default scope
$model = Player::model()->proleague();
您应该只使用 resetScope(true) 方法。它 "removes" defaultScope 过滤器。
$model = Player::model()->resetScope(true)->proleague();