getValue() protected method

This method is called by AttributeBehavior::evaluateAttributes. Its return value will be assigned to the attributes corresponding to the triggering event.
protected getValue ( Event $event ) : mixed
$event yii\base\Event the event that triggers the current attribute updating.
return mixed the attribute value
コード例 #1
0
ファイル: SluggableBehavior.php プロジェクト: arogachev/yii2
 /**
  * @inheritdoc
  */
 protected function getValue($event)
 {
     if ($this->attribute !== null) {
         if ($this->isNewSlugNeeded()) {
             $slugParts = [];
             foreach ((array) $this->attribute as $attribute) {
                 $slugParts[] = $this->owner->{$attribute};
             }
             $slug = $this->generateSlug($slugParts);
         } else {
             return $this->owner->{$this->slugAttribute};
         }
     } else {
         $slug = parent::getValue($event);
     }
     return $this->ensureUnique ? $this->makeUnique($slug) : $slug;
 }
コード例 #2
0
ファイル: SluggableBehavior.php プロジェクト: SergAHell/yii2
 /**
  * @inheritdoc
  */
 protected function getValue($event)
 {
     $isNewSlug = true;
     if ($this->attribute !== null) {
         $attributes = (array) $this->attribute;
         /* @var $owner BaseActiveRecord */
         $owner = $this->owner;
         if (!empty($owner->{$this->slugAttribute})) {
             $isNewSlug = false;
             if (!$this->immutable) {
                 foreach ($attributes as $attribute) {
                     if ($owner->isAttributeChanged($attribute)) {
                         $isNewSlug = true;
                         break;
                     }
                 }
             }
         }
         if ($isNewSlug) {
             $slugParts = [];
             foreach ($attributes as $attribute) {
                 $slugParts[] = $owner->{$attribute};
             }
             $slug = $this->generateSlug($slugParts);
         } else {
             $slug = $owner->{$this->slugAttribute};
         }
     } else {
         $slug = parent::getValue($event);
     }
     if ($this->ensureUnique && $isNewSlug) {
         $baseSlug = $slug;
         $iteration = 0;
         while (!$this->validateSlug($slug)) {
             $iteration++;
             $slug = $this->generateUniqueSlug($baseSlug, $iteration);
         }
     }
     return $slug;
 }
コード例 #3
0
ファイル: TimestampBehavior.php プロジェクト: assad2012/yii2
 /**
  * @inheritdoc
  *
  * In case, when the [[value]] is `null`, the result of the PHP function [time()](http://php.net/manual/en/function.time.php)
  * will be used as value.
  */
 protected function getValue($event)
 {
     if ($this->value === null) {
         return time();
     }
     return parent::getValue($event);
 }
コード例 #4
0
 /**
  * @inheritdoc
  *
  * In case, when the [[value]] property is `null`, the value of `Yii::$app->user->id` will be used as the value.
  */
 protected function getValue($event)
 {
     if ($this->value === null) {
         $user = Yii::$app->get('user', false);
         return $user && !$user->isGuest ? $user->id : null;
     }
     return parent::getValue($event);
 }
コード例 #5
0
 /**
  * @inheritdoc
  *
  * In case, when the [[value]] is `null`, position of last created entity or default value
  * will be used as value.
  */
 protected function getValue($event)
 {
     if ($this->value === null) {
         if (!$this->owner instanceof ActiveRecordInterface) {
             throw new \LogicException('Owner should be instanceof ActiveRecordInterface');
         }
         if (!($last = $this->owner->find()->orderBy([$this->positionAttribute => SORT_DESC])->one())) {
             return $this->defaultValue;
         }
         return $last->{$this->positionAttribute} + 1;
     }
     return parent::getValue($event);
 }
コード例 #6
0
 /**
  * @inheritdoc
  *
  * In case, when the [[value]] is `null`, the result of the PHP function [time()](http://php.net/manual/en/function.time.php)
  * will be used as value.
  */
 protected function getValue($event)
 {
     if ($this->value === null) {
         return \Yii::$app->user->id;
     }
     return parent::getValue($event);
 }