類別 yii\caching\DbQueryDependency
DbQueryDependency 代表一個基於 yii\db\QueryInterface 實例的查詢結果的依賴性。
如果查詢結果變更,則該依賴性會被視為已變更。 查詢是透過 $query 屬性指定的。
可以使用符合 yii\db\QueryInterface 的任何類別的物件,因此此依賴性不僅可以用於常規關聯式資料庫,還可以用於 MongoDB、Redis 等等。
有關快取的更多詳細資訊和使用資訊,請參閱快取概述指南文章。
另請參閱 yii\db\QueryInterface。
公開屬性
屬性 | 類型 | 描述 | 定義來源 |
---|---|---|---|
$data | mixed | 儲存在快取中,稍後與最新的依賴性資料比較的依賴性資料。 | yii\caching\Dependency |
$db | string|array|object | 資料庫連線的應用程式元件 ID、連線物件或其陣列配置。 | yii\caching\DbQueryDependency |
$method | string|callable|null | 應在 $query 物件上調用的方法。 | yii\caching\DbQueryDependency |
$query | yii\db\QueryInterface | 用於確定依賴性是否已變更的查詢。 | yii\caching\DbQueryDependency |
$reusable | boolean | 此依賴性是否可重複使用。 | yii\caching\Dependency |
公開方法
方法 | 描述 | 定義來源 |
---|---|---|
__call() | 調用不是類別方法的具名方法。 | yii\base\BaseObject |
__construct() | 建構子。 | yii\base\BaseObject |
__get() | 傳回物件屬性的值。 | yii\base\BaseObject |
__isset() | 檢查屬性是否已設定,即已定義且非 null。 | yii\base\BaseObject |
__set() | 設定物件屬性的值。 | yii\base\BaseObject |
__unset() | 將物件屬性設定為 null。 | yii\base\BaseObject |
canGetProperty() | 傳回一個值,指示屬性是否可以讀取。 | yii\base\BaseObject |
canSetProperty() | 傳回一個值,指示屬性是否可以設定。 | yii\base\BaseObject |
className() | 傳回此類別的完整限定名稱。 | yii\base\BaseObject |
evaluateDependency() | 通過產生和儲存與依賴性相關的資料來評估依賴性。 | yii\caching\Dependency |
getHasChanged() | 傳回一個值,指示依賴性是否已變更。 | yii\caching\Dependency |
hasMethod() | 傳回一個值,指示是否已定義方法。 | yii\base\BaseObject |
hasProperty() | 傳回一個值,指示是否已定義屬性。 | yii\base\BaseObject |
init() | 初始化物件。 | yii\base\BaseObject |
isChanged() | 檢查依賴性是否已變更。 | yii\caching\Dependency |
resetReusableData() | 重設可重複使用依賴性的所有快取資料。 | yii\caching\Dependency |
受保護的方法
方法 | 描述 | 定義來源 |
---|---|---|
generateDependencyData() | 產生確定依賴性是否已變更所需的資料。 | yii\caching\DbQueryDependency |
generateReusableHash() | 產生可用於檢索可重複使用依賴性資料的唯一雜湊值。 | yii\caching\Dependency |
屬性詳細資訊
應在 $query 物件上調用的方法。
如果指定為字串,將調用具有該名稱的自有查詢方法,並將 $db 值作為其第一個引數傳遞。 例如:exists
、all
。
此欄位可以指定為具有以下簽名的 PHP 回呼
function (QueryInterface $query, mixed $db) {
//return mixed;
}
如果未設定 - 將使用 yii\db\QueryInterface::one()。
方法詳細資訊
定義來源: yii\base\BaseObject::__call()
調用不是類別方法的具名方法。
不要直接調用此方法,因為它是 PHP magic method,當調用未知方法時,它會被隱式調用。
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法回傳值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當調用未知方法時 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定義來源: yii\base\BaseObject::__construct()
建構子。
預設實作執行兩件事
- 使用給定的配置
$config
初始化物件。 - 調用 init()。
如果此方法在子類別中被覆寫,建議
- 建構子的最後一個參數是一個配置陣列,例如此處的
$config
。 - 在建構子的結尾調用父類別實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的名稱-值對 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義來源: yii\base\BaseObject::__get()
傳回物件屬性的值。
不要直接調用此方法,因為它是 PHP magic method,當執行 $value = $object->property;
時,它會被隱式調用。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
throws | yii\base\InvalidCallException |
如果屬性是唯寫的 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義來源: yii\base\BaseObject::__isset()
檢查屬性是否已設定,即已定義且非 null。
不要直接調用此方法,因為它是 PHP magic method,當執行 isset($object->property)
時,它會被隱式調用。
請注意,如果未定義屬性,將傳回 false。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
具名屬性是否已設定 (非 null)。 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
定義來源: yii\base\BaseObject::__set()
設定物件屬性的值。
不要直接調用此方法,因為它是 PHP magic method,當執行 $object->property = $value;
時,它會被隱式調用。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | string |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性是唯讀的 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
定義來源: yii\base\BaseObject::__unset()
將物件屬性設定為 null。
不要直接調用此方法,因為它是 PHP magic method,當執行 unset($object->property)
時,它會被隱式調用。
請注意,如果未定義屬性,此方法將不執行任何操作。 如果屬性是唯讀的,它將拋出例外。
public void __unset ( $name ) | ||
$name | string |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性是唯讀的。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
定義來源: yii\base\BaseObject::canGetProperty()
傳回一個值,指示屬性是否可以讀取。
如果滿足以下條件,則屬性是可讀取的
- 類別具有與指定名稱相關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可以讀取 |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
定義來源: yii\base\BaseObject::canSetProperty()
傳回一個值,指示屬性是否可以設定。
如果滿足以下條件,則屬性是可寫入的
- 類別具有與指定名稱相關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可以寫入 |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class
。
定義來源: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
public void evaluateDependency ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此依賴性的快取元件 |
public function evaluateDependency($cache)
{
if ($this->reusable) {
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$_reusableData)) {
self::$_reusableData[$hash] = $this->generateDependencyData($cache);
}
$this->data = self::$_reusableData[$hash];
} else {
$this->data = $this->generateDependencyData($cache);
}
}
產生確定依賴性是否已變更所需的資料。
此方法傳回查詢結果。
protected mixed generateDependencyData ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此依賴性的快取元件 |
return | mixed |
確定依賴性是否已變更所需的資料。 |
---|---|---|
throws | yii\base\InvalidConfigException |
在無效配置時。 |
protected function generateDependencyData($cache)
{
$db = $this->db;
if ($db !== null) {
$db = Instance::ensure($db);
}
if (!$this->query instanceof QueryInterface) {
throw new InvalidConfigException('"' . get_class($this) . '::$query" should be an instance of "yii\db\QueryInterface".');
}
if (!empty($db->enableQueryCache)) {
// temporarily disable and re-enable query caching
$originEnableQueryCache = $db->enableQueryCache;
$db->enableQueryCache = false;
$result = $this->executeQuery($this->query, $db);
$db->enableQueryCache = $originEnableQueryCache;
} else {
$result = $this->executeQuery($this->query, $db);
}
return $result;
}
protected string generateReusableHash ( ) | ||
return | string |
此快取依賴性的唯一雜湊值。 |
---|
protected function generateReusableHash()
{
$clone = clone $this;
$clone->data = null; // https://github.com/yiisoft/yii2/issues/3052
try {
$serialized = serialize($clone);
} catch (\Exception $e) {
// unserializable properties are nulled
foreach ($clone as $name => $value) {
if (is_object($value) && $value instanceof \Closure) {
$clone->{$name} = null;
}
}
$serialized = serialize($clone);
}
return sha1($serialized);
}
定義來源: yii\caching\Dependency::getHasChanged()
傳回一個值,指示依賴性是否已變更。
public boolean getHasChanged ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此依賴性的快取元件 |
return | boolean |
依賴性是否已變更。 |
---|
public function getHasChanged($cache)
{
return $this->isChanged($cache);
}
定義來源: yii\base\BaseObject::hasMethod()
傳回一個值,指示是否已定義方法。
預設實作是對 php 函數 method_exists()
的調用。 當您實作 php magic method __call()
時,您可以覆寫此方法。
public boolean hasMethod ( $name ) | ||
$name | string |
方法名稱 |
return | boolean |
是否已定義方法 |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定義來源: yii\base\BaseObject::hasProperty()
傳回一個值,指示是否已定義屬性。
如果滿足以下條件,則定義了屬性
- 類別具有與指定名稱相關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
是否已定義屬性 |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
public void init ( ) |
public function init()
{
}
定義來源: yii\caching\Dependency::isChanged()
檢查依賴性是否已變更。
public boolean isChanged ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此依賴性的快取元件 |
return | boolean |
依賴性是否已變更。 |
---|
public function isChanged($cache)
{
if ($this->reusable) {
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$_reusableData)) {
self::$_reusableData[$hash] = $this->generateDependencyData($cache);
}
$data = self::$_reusableData[$hash];
} else {
$data = $this->generateDependencyData($cache);
}
return $data !== $this->data;
}
定義來源: yii\caching\Dependency::resetReusableData()
重設可重複使用依賴性的所有快取資料。
public static void resetReusableData ( ) |
public static function resetReusableData()
{
self::$_reusableData = [];
}
註冊 或 登入 以發表評論。