BlameableBehavior.php 3.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\behaviors;

use Yii;
use yii\base\Event;
use yii\db\BaseActiveRecord;

/**
 * BlameableBehavior automatically fills the specified attributes with the current user ID.
 *
17
 * To use BlameableBehavior, insert the following code to your ActiveRecord class:
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * ```php
 * use yii\behaviors\BlameableBehavior;
 *
 * public function behaviors()
 * {
 *     return [
 *         BlameableBehavior::className(),
 *     ];
 * }
 * ```
 *
 * By default, BlameableBehavior will fill the `created_by` and `updated_by` attributes with the current user ID
 * when the associated AR object is being inserted; it will fill the `updated_by` attribute
 * with the current user ID when the AR object is being updated. If your attribute names are different, you may configure
33
 * the [[createdByAttribute]] and [[updatedByAttribute]] properties like the following:
34 35 36 37 38 39 40
 *
 * ```php
 * public function behaviors()
 * {
 *     return [
 *         [
 *             'class' => BlameableBehavior::className(),
41 42
 *             'createdByAttribute' => 'author_id',
 *             'updatedByAttribute' => 'updater_id',
43 44 45 46 47 48 49
 *         ],
 *     ];
 * }
 * ```
 *
 * @author Luciano Baraglia <luciano.baraglia@gmail.com>
 * @author Qiang Xue <qiang.xue@gmail.com>
50
 * @author Alexander Kochetov <creocoder@gmail.com>
51 52 53 54
 * @since 2.0
 */
class BlameableBehavior extends AttributeBehavior
{
55
    /**
56
     * @var string the attribute that will receive current user ID value
57
     * Set this property to be null if you do not want to record the creator ID.
58
     */
59 60 61
    public $createdByAttribute = 'created_by';
    /**
     * @var string the attribute that will receive current user ID value
62
     * Set this property to be null if you do not want to record the updater ID.
63 64
     */
    public $updatedByAttribute = 'updated_by';
65 66 67 68 69 70 71 72 73 74 75 76 77 78
    /**
     * @var callable the value that will be assigned to the attributes. This should be a valid
     * PHP callable whose return value will be assigned to the current attribute(s).
     * The signature of the callable should be:
     *
     * ```php
     * function ($event) {
     *     // return value will be assigned to the attribute(s)
     * }
     * ```
     *
     * If this property is not set, the value of `Yii::$app->user->id` will be assigned to the attribute(s).
     */
    public $value;
79

80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    /**
     * @inheritdoc
     */
    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,
            ];
        }
    }

96 97 98
    /**
     * Evaluates the value of the user.
     * The return result of this method will be assigned to the current attribute(s).
99
     * @param Event $event
100 101 102 103 104
     * @return mixed the value of the user.
     */
    protected function getValue($event)
    {
        if ($this->value === null) {
Qiang Xue committed
105 106
            $user = Yii::$app->get('user', false);
            return $user && !$user->isGuest ? $user->id : null;
107 108 109 110
        } else {
            return call_user_func($this->value, $event);
        }
    }
111
}