BaseActiveRecord::__set PHP Method

__set() public method

NOTE once a property is set, this magic method will not be called by php for setting it again, unless the property is unset first.
public __set ( string $name, mixed $value ) : mixed | void
$name string
$value mixed
return mixed | void
    public function __set($name, $value)
    {
        // Only perform this override if turned on for the given model
        if ($this->auto_update_relations && is_array($value) && count($value) && isset($this->getMetaData()->relations[$name])) {
            $rel = $this->getMetaData()->relations[$name];
            $cls = get_class($rel);
            if ($cls == self::HAS_MANY || $cls == self::MANY_MANY) {
                $rel_cls = $rel->className;
                $pk_attr = $rel_cls::model()->getMetaData()->tableSchema->primaryKey;
                // not supporting composite primary keys at this point
                if (is_string($pk_attr)) {
                    $m_set = array();
                    foreach ($value as $v) {
                        if (is_array($v)) {
                            // looks like a list of attribute values, try to find or instantiate the classes
                            if (array_key_exists($pk_attr, $v) && isset($v[$pk_attr])) {
                                $m = $rel_cls::model()->findByPk($v[$pk_attr]);
                            } else {
                                $m = new $rel_cls();
                            }
                            $m->attributes = array_merge($this->getRelationsDefaults($name), $v);
                            // set foreign key on the related object
                            $m->{$rel->foreignKey} = $this->getPrimaryKey();
                        } elseif (is_object($v)) {
                            $m = $v;
                        } else {
                            // try to find the instance
                            if (!($m = $rel_cls::model()->findByPk($v))) {
                                throw new Exception('Unable to understand value ' . print_r($v, true) . " for {$name}");
                            }
                        }
                        $m_set[] = $m;
                    }
                    // reset the value for it to be set by parent method
                    $value = $m_set;
                }
            }
        }
        parent::__set($name, $value);
    }