yii\behaviors\AttributeBehavior::evaluateAttributes PHP Method

evaluateAttributes() public method

Evaluates the attribute value and assigns it to the current attributes.
public evaluateAttributes ( Event $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)) {
                    $this->owner->{$attribute} = $value;
                }
            }
        }
    }

Usage Example

コード例 #1
0
 /**
  * Evaluates the attribute value and assigns it to the current attributes. If no foreignClass 
  * is specified, it behaves like the normal AttributeBehavior.
  * @param Event $event
  */
 public function evaluateAttributes($event)
 {
     if ($this->foreignClass === null) {
         parent::evaluateAttributes($event);
     } elseif (!empty($this->attributes[$event->name])) {
         $fObjects = $this->getForeignObjects($this->foreignClass[0], $this->foreignClass[1]);
         $attributes = (array) $this->attributes[$event->name];
         $value = $this->getValue($event);
         $uAttributes = [];
         $n = count($fObjects);
         foreach ($attributes as $attribute) {
             for ($i = 0; $i < $n; ++$i) {
                 $fObjects[$i]->{$attribute} = $value;
             }
             $uAttributes[] = $attribute;
         }
         foreach ($fObjects as $fObject) {
             $fObject->save(false, $uAttributes);
         }
     }
 }