0 追蹤者

類別 yii\behaviors\BlameableBehavior

繼承關係yii\behaviors\BlameableBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\BaseObject
實作yii\base\Configurable
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/behaviors/BlameableBehavior.php

BlameableBehavior 會自動使用當前使用者 ID 填充指定的屬性。

若要使用 BlameableBehavior,請將以下程式碼插入您的 ActiveRecord 類別

use yii\behaviors\BlameableBehavior;

public function behaviors()
{
    return [
        BlameableBehavior::class,
    ];
}

預設情況下,當相關聯的 AR 物件正在插入時,BlameableBehavior 將使用當前使用者 ID 填充 created_byupdated_by 屬性;當 AR 物件正在更新時,它將使用當前使用者 ID 填充 updated_by 屬性。

由於屬性值將由此行為自動設定,因此它們通常不是使用者輸入,因此不應進行驗證,即 created_byupdated_by 不應出現在模型的 rules() 方法中。

如果您的屬性名稱不同,您可以像下面這樣配置 $createdByAttribute$updatedByAttribute 屬性

public function behaviors()
{
    return [
        [
            'class' => BlameableBehavior::class,
            'createdByAttribute' => 'author_id',
            'updatedByAttribute' => 'updater_id',
        ],
    ];
}

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$attributes array 要自動填入透過 $value 指定之值的屬性列表。 yii\behaviors\AttributeBehavior
$createdByAttribute string (字串) 將接收目前使用者 ID 值的屬性。如果您不想記錄建立者 ID,請將此屬性設定為 false。 yii\behaviors\BlameableBehavior
$defaultValue mixed (混合) 當使用者為訪客時的預設值 yii\behaviors\BlameableBehavior
$owner yii\base\Component|null 此行為的擁有者 yii\base\Behavior
$preserveNonEmptyValues boolean (布林值) 是否保留非空的屬性值。 yii\behaviors\AttributeBehavior
$skipUpdateOnClean boolean (布林值) $owner 未被修改時,是否跳過此行為 yii\behaviors\AttributeBehavior
$updatedByAttribute string (字串) 將接收目前使用者 ID 值的屬性。如果您不想記錄更新者 ID,請將此屬性設定為 false。 yii\behaviors\BlameableBehavior
$value mixed (混合) 將被指派給目前屬性的值。 yii\behaviors\BlameableBehavior

Public Methods (公用方法)

Hide inherited methods (隱藏繼承的方法)

Method (方法) 描述 定義於
__call() 呼叫非類別方法的具名方法。 yii\base\BaseObject
__construct() 建構子 (Constructor)。 yii\base\BaseObject
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
attach() 將行為物件附加到元件。 yii\base\Behavior
canGetProperty() 傳回一個值,指示屬性是否可讀取。 yii\base\BaseObject
canSetProperty() 傳回一個值,指示屬性是否可設定。 yii\base\BaseObject
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
detach() 從元件分離行為物件。 yii\base\Behavior
evaluateAttributes() 評估屬性值並將其指派給目前的屬性。 yii\behaviors\AttributeBehavior
events() 宣告 $owner 事件的事件處理器。 yii\behaviors\AttributeBehavior
hasMethod() 傳回一個值,指示方法是否已定義。 yii\base\BaseObject
hasProperty() 傳回一個值,指示屬性是否已定義。 yii\base\BaseObject
init() 初始化物件。 yii\behaviors\BlameableBehavior

Protected Methods (保護方法)

Hide inherited methods (隱藏繼承的方法)

Method (方法) 描述 定義於
getDefaultValue() 取得預設值 yii\behaviors\BlameableBehavior
getValue() 傳回目前屬性的值。 yii\behaviors\BlameableBehavior

Property Details (屬性詳細資訊)

隱藏繼承的屬性

$createdByAttribute public property (公用屬性)

將接收目前使用者 ID 值的屬性。如果您不想記錄建立者 ID,請將此屬性設定為 false。

public string $createdByAttribute 'created_by'
$defaultValue public property (公用屬性) (自版本 2.0.14 起可用)

當使用者為訪客時的預設值

public mixed (混合) $defaultValue null
$updatedByAttribute public property (公用屬性)

將接收目前使用者 ID 值的屬性。如果您不想記錄更新者 ID,請將此屬性設定為 false。

public string $updatedByAttribute 'updated_by'
$value public property (公用屬性)

若屬性為 null,則會使用 Yii::$app->user->id 的值作為此值。

將被指派給目前屬性的值。這可以是匿名函數、陣列格式的可呼叫 (callable) 物件 (例如 [$this, 'methodName'])、代表資料庫 (DB) 運算式的 Expression 物件 (例如 new Expression('NOW()'))、純量、字串或任意值。若是前者,函數的傳回值將會被指派給屬性。函數的簽名應如下所示:

function ($event)
{
    // return value will be assigned to the attribute
}
public mixed (混合) $value null

Method Details (方法詳細資訊)

Hide inherited methods (隱藏繼承的方法)

__call() public method (公用方法)

Defined in: (定義於:) yii\base\BaseObject::__call()

呼叫非類別方法的具名方法。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當調用未知方法時將會隱含地呼叫它。

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

            
__construct() public method (公用方法)

Defined in: (定義於:) yii\base\BaseObject::__construct()

建構子 (Constructor)。

預設實作會執行兩件事

  • 使用給定的組態 $config 初始化物件。
  • 呼叫 init()

如果此方法在子類別中被覆寫,建議

  • 建構子的最後一個參數是組態陣列,如這裡的 $config
  • 在建構子的結尾呼叫父類別實作。
public void __construct ( $config = [] )
$config array

將用於初始化物件屬性的名稱-值配對

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public method (公用方法)

Defined in: (定義於:) yii\base\BaseObject::__get()

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $value = $object->property; 時將會隱含地呼叫它。

另請參閱 __set()

public mixed (混合) __get ( $name )
$name string (字串)

屬性名稱

return (傳回) mixed (混合)

屬性值

throws (拋出) yii\base\UnknownPropertyException

如果屬性未定義

throws (拋出) yii\base\InvalidCallException

如果屬性為唯寫 (write-only)

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

            
__isset() public method (公用方法)

Defined in: (定義於:) yii\base\BaseObject::__isset()

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($object->property) 時將會隱含地呼叫它。

請注意,如果屬性未定義,將會傳回 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

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;
}

            
__set() public method (公用方法)

Defined in: (定義於:) yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $object->property = $value; 時將會隱含地呼叫它。

另請參閱 __get()

public void __set ( $name, $value )
$name string (字串)

屬性名稱或事件名稱

$value mixed (混合)

屬性值

throws (拋出) yii\base\UnknownPropertyException

如果屬性未定義

throws (拋出) yii\base\InvalidCallException

如果屬性為唯讀 (read-only)

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

            
__unset() public method (公用方法)

Defined in: (定義於:) yii\base\BaseObject::__unset()

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 unset($object->property) 時將會隱含地呼叫它。

請注意,如果屬性未定義,此方法將不會執行任何動作。如果屬性為唯讀,則會拋出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

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

            
attach() public method (公用方法)

Defined in: (定義於:) yii\base\Behavior::attach()

將行為物件附加到元件。

預設實作將設定 $owner 屬性,並附加在 events() 中宣告的事件處理器。如果您覆寫此方法,請務必呼叫父類別實作。

public void attach ( $owner )
$owner yii\base\Component

此行為要附加到的元件。

                public function attach($owner)
{
    $this->owner = $owner;
    foreach ($this->events() as $event => $handler) {
        $this->_attachedEvents[$event] = $handler;
        $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
    }
}

            
canGetProperty() public method (公用方法)

Defined in: (定義於:) 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);
}

            
canSetProperty() public method (公用方法)

Defined in: (定義於:) 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);
}

            
className() public static method (公用靜態方法)
自 2.0.14 版本起已過時。在 PHP >=5.5 上,請改用 ::class

Defined in: (定義於:) yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string (字串) className ( )
return (傳回) string (字串)

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
detach() public method (公用方法)

Defined in: (定義於:) yii\base\Behavior::detach()

從元件分離行為物件。

預設實作將取消設定 $owner 屬性,並分離在 events() 中宣告的事件處理器。如果您覆寫此方法,請務必呼叫父類別實作。

public void detach ( )

                public function detach()
{
    if ($this->owner) {
        foreach ($this->_attachedEvents as $event => $handler) {
            $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
        }
        $this->_attachedEvents = [];
        $this->owner = null;
    }
}

            
evaluateAttributes() public method (公用方法)

Defined in: (定義於:) yii\behaviors\AttributeBehavior::evaluateAttributes()

評估屬性值並將其指派給目前的屬性。

public void evaluateAttributes ( $event )
$event yii\base\Event

                public function evaluateAttributes($event)
{
    if (
        $this->skipUpdateOnClean
        && $event->name == ActiveRecord::EVENT_BEFORE_UPDATE
        && empty($this->owner->dirtyAttributes)
    ) {
        return;
    }
    if (!empty($this->attributes[$event->name])) {
        $attributes = (array) $this->attributes[$event->name];
        $value = $this->getValue($event);
        foreach ($attributes as $attribute) {
            // ignore attribute names which are not string (e.g. when set by TimestampBehavior::updatedAtAttribute)
            if (is_string($attribute)) {
                if ($this->preserveNonEmptyValues && !empty($this->owner->$attribute)) {
                    continue;
                }
                $this->owner->$attribute = $value;
            }
        }
    }
}

            
events() public method (公用方法)

Defined in: (定義於:) yii\behaviors\AttributeBehavior::events()

宣告 $owner 事件的事件處理器。

子類別可以覆寫此方法,以宣告應附加到 $owner 元件事件的 PHP 回呼 (callback)。

當行為附加到擁有者時,回呼將會附加到 $owner 的事件;當行為從元件分離時,它們將會從事件中分離。

回呼可以是以下任何一種:

  • 此行為中的方法:'handleClick',等同於 [$this, 'handleClick']
  • 物件方法:[$object, 'handleClick']
  • 靜態方法:['Page', 'handleClick']
  • 匿名函數:function ($event) { ... }

以下是一個範例

[
    Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
    Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
]
public array (陣列) events ( )
return (傳回) array

事件 (陣列鍵) 和對應的事件處理器方法 (陣列值)。

                public function events()
{
    return array_fill_keys(
        array_keys($this->attributes),
        'evaluateAttributes'
    );
}

            
getDefaultValue() protected method (保護方法) (自版本 2.0.14 起可用)

取得預設值

protected array (陣列)|mixed (混合) getDefaultValue ( $event )
$event yii\base\Event

                protected function getDefaultValue($event)
{
    if ($this->defaultValue instanceof \Closure || (is_array($this->defaultValue) && is_callable($this->defaultValue))) {
        return call_user_func($this->defaultValue, $event);
    }
    return $this->defaultValue;
}

            
getValue() protected method (保護方法)

傳回目前屬性的值。

$value 屬性為 null,則會使用 $defaultValue 的值作為此值。

此方法由 evaluateAttributes() 呼叫。其傳回值將會指派給對應於觸發事件的屬性。

protected mixed (混合) getValue ( $event )
$event yii\base\Event

觸發目前屬性更新的事件。

return (傳回) mixed (混合)

屬性值

                protected function getValue($event)
{
    if ($this->value === null && Yii::$app->has('user')) {
        $userId = Yii::$app->get('user')->id;
        if ($userId === null) {
            return $this->getDefaultValue($event);
        }
        return $userId;
    } elseif ($this->value === null) {
        return $this->getDefaultValue($event);
    }
    return parent::getValue($event);
}

            
hasMethod() public method (公用方法)

Defined in: (定義於:) yii\base\BaseObject::hasMethod()

傳回一個值,指示方法是否已定義。

預設實作是對 php 函數 method_exists() 的呼叫。當您實作 php 魔術方法 __call() 時,可以覆寫此方法。

public boolean (布林值) hasMethod ( $name )
$name string (字串)

方法名稱

return (傳回) boolean (布林值)

方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method (公用方法)

Defined in: (定義於:) 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);
}

            
init() public method (公用方法)

初始化物件。

在物件使用給定的組態初始化後,此方法會在建構子的結尾被調用。

public void init ( )

                public function init()
{
    parent::init();
    if (empty($this->attributes)) {
        $this->attributes = [
            BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdByAttribute, $this->updatedByAttribute],
            BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedByAttribute,
        ];
    }
}