0 follower

Trait Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait

Public Methods

Hide inherited methods

Method Description Defined By
__get() PHP getter magic method. Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
__isset() PHP isset magic method. Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
__set() PHP setter magic method. Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
__unset() PHP unset magic method. Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
canGetProperty() Returns a value indicating whether a property can be read. Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
canSetProperty() Returns a value indicating whether a property can be set. Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
get() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
hasDependentRelations() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
hasProperty() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
hasRelationQuery() Returns a value indicating whether the record has a relation query with the specified name. Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
isProperty() Returns a value indicating whether a property is defined for this component. Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
isRelationPopulated() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
relatedRecords() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
resetDependentRelations() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
resetRelation() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
retrieveRelation() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
set() Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait

Method Details

Hide inherited methods

__get() public method

PHP getter magic method.

This method is overridden so that values and related objects can be accessed like properties.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::get().

public mixed __get ( string $name )
$name string

Property or relation name.

return mixed

Property or relation value.

throws \InvalidArgumentException\Yiisoft\Db\Exception\InvalidCallException\Yiisoft\Db\Exception\InvalidConfigException\ReflectionException\Throwable
throws Yiisoft\ActiveRecord\UnknownPropertyException
throws \Yiisoft\Db\Exception\Exception

                public function __get(string $name)
{
    if (method_exists($this, $getter = "get$name")) {
        /** Read getter, e.g., getName() */
        return $this->$getter();
    }
    if ($this->hasProperty($name)) {
        return $this->get($name);
    }
    if ($this->isRelationPopulated($name)) {
        return $this->relatedRecords()[$name];
    }
    if ($this->hasRelationQuery($name)) {
        /** Read relation query getter, e.g., getUserQuery() */
        return $this->retrieveRelation($name);
    }
    if (method_exists($this, "set$name")) {
        throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property or relation: ' . static::class . '::' . $name);
}

            
__isset() public method

PHP isset magic method.

Checks if a property or relation exists and its value is not null.

public boolean __isset ( string $name )
$name string

The property or relation name.

                public function __isset(string $name): bool
{
    try {
        return $this->__get($name) !== null;
    } catch (InvalidCallException|UnknownPropertyException) {
        return false;
    }
}

            
__set() public method

PHP setter magic method.

Sets the value of a property.

public void __set ( string $name, mixed $value )
$name string

Property name.

$value mixed
throws \Yiisoft\Db\Exception\InvalidCallException\Yiisoft\ActiveRecord\UnknownPropertyException

                public function __set(string $name, mixed $value): void
{
    if (method_exists($this, $setter = "set$name")) {
        $this->$setter($value);
        return;
    }
    if ($this->hasProperty($name)) {
        parent::set($name, $value);
        return;
    }
    if (
        method_exists($this, "get$name")
        || $this->hasRelationQuery($name)
    ) {
        throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name);
    }
    throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name);
}

            
__unset() public method

PHP unset magic method.

Unsets the property or relation.

public void __unset ( string $name )
$name string

The property or relation name.

                public function __unset(string $name): void
{
    if ($this->hasProperty($name)) {
        unset($this->propertyValues[$name]);
        if ($this->hasDependentRelations($name)) {
            $this->resetDependentRelations($name);
        }
    } elseif ($this->isRelationPopulated($name)) {
        $this->resetRelation($name);
    }
}

            
canGetProperty() public method

Returns a value indicating whether a property can be read.

public boolean canGetProperty ( string $name, boolean $checkVars true )
$name string
$checkVars boolean

                public function canGetProperty(string $name, bool $checkVars = true): bool
{
    return method_exists($this, "get$name")
        || $this->hasRelationQuery($name)
        || ($checkVars && property_exists($this, $name))
        || $this->hasProperty($name);
}

            
canSetProperty() public method

Returns a value indicating whether a property can be set.

public boolean canSetProperty ( string $name, boolean $checkVars true )
$name string
$checkVars boolean

                public function canSetProperty(string $name, bool $checkVars = true): bool
{
    return method_exists($this, "set$name")
        || ($checkVars && property_exists($this, $name))
        || $this->hasProperty($name);
}

            
get() public method

public mixed get ( string $propertyName )
$propertyName string

                public function get(string $propertyName): mixed
{
    if ($propertyName !== 'propertyValues' && property_exists($this, $propertyName)) {
        return $this->$propertyName ?? null;
    }
    return $this->propertyValues[$propertyName] ?? null;
}

            
hasDependentRelations() public method

public boolean hasDependentRelations ( string $propertyName )
$propertyName string
return boolean
hasProperty() public method

public boolean hasProperty ( string $name )
$name string

                public function hasProperty(string $name): bool
{
    return isset($this->propertyValues[$name]) || in_array($name, $this->propertyNames(), true);
}

            
hasRelationQuery() public method

Returns a value indicating whether the record has a relation query with the specified name.

public boolean hasRelationQuery ( string $name )
$name string

The name of the relation query.

                public function hasRelationQuery(string $name): bool
{
    return method_exists($this, "get{$name}Query");
}

            
isProperty() public method

Returns a value indicating whether a property is defined for this component.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive).
  • the class has a member variable with the specified name (when $checkVars is true).

See also:

public boolean isProperty ( string $name, boolean $checkVars true )
$name string

The property name.

$checkVars boolean

Whether to treat member variables as properties.

return boolean

Whether the property is defined.

                public function isProperty(string $name, bool $checkVars = true): bool
{
    return method_exists($this, "get$name")
        || method_exists($this, "set$name")
        || $this->hasRelationQuery($name)
        || ($checkVars && property_exists($this, $name))
        || $this->hasProperty($name);
}

            
isRelationPopulated() public method

public boolean isRelationPopulated ( string $name )
$name string
return boolean
populateProperty() protected method

protected void populateProperty ( string $name, mixed $value )
$name string
$value mixed

                protected function populateProperty(string $name, mixed $value): void
{
    if ($name !== 'propertyValues' && property_exists($this, $name)) {
        $this->$name = $value;
    } else {
        $this->propertyValues[$name] = $value;
    }
}

            
propertyValuesInternal() protected method

protected array propertyValuesInternal ( )

                protected function propertyValuesInternal(): array
{
    return array_merge($this->propertyValues, parent::propertyValuesInternal());
}

            
relatedRecords() public method

public array relatedRecords ( )
return array
resetDependentRelations() public method

public void resetDependentRelations ( string $propertyName )
$propertyName string
return void
resetRelation() public method

public void resetRelation ( string $name )
$name string
return void
set() public method

public void set ( string $propertyName, mixed $value )
$propertyName string
$value mixed

                public function set(string $propertyName, mixed $value): void
{
    if ($this->hasProperty($propertyName)) {
        parent::set($propertyName, $value);
    } else {
        throw new InvalidArgumentException(static::class . ' has no property named "' . $propertyName . '".');
    }
}