Trait Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait
Trait to define magic methods to access values of an ActiveRecord instance.
See also:
- Yiisoft\ActiveRecord\AbstractActiveRecord::relatedRecords()
- Yiisoft\ActiveRecord\AbstractActiveRecord::hasDependentRelations()
- Yiisoft\ActiveRecord\ActiveRecordInterface::isRelationPopulated()
- Yiisoft\ActiveRecord\AbstractActiveRecord::resetDependentRelations()
- Yiisoft\ActiveRecord\ActiveRecordInterface::resetRelation()
- Yiisoft\ActiveRecord\AbstractActiveRecord::retrieveRelation()
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| populateProperty() | Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait | |
| propertyValuesInternal() | Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait |
Method Details
PHP getter magic method.
This method is overridden so that values and related objects can be accessed like properties.
See also \Yiisoft\ActiveRecord\Trait\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);
}
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;
}
}
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);
}
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);
}
}
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);
}
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);
}
| public boolean hasDependentRelations ( string $propertyName ) | ||
| $propertyName | string | |
| return | boolean | |
|---|---|---|
| public boolean hasProperty ( string $name ) | ||
| $name | string | |
public function hasProperty(string $name): bool
{
return isset($this->propertyValues[$name]) || in_array($name, $this->propertyNames(), true);
}
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");
}
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
$checkVarsis true).
| 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. {@see \Yiisoft\ActiveRecord\Trait\canGetProperty()} {@see \Yiisoft\ActiveRecord\Trait\canSetProperty()} |
|---|---|---|
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);
}
| public boolean isRelationPopulated ( string $name ) | ||
| $name | string | |
| return | boolean | |
|---|---|---|
| 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;
}
}
| protected array propertyValuesInternal ( ) |
protected function propertyValuesInternal(): array
{
return array_merge($this->propertyValues, parent::propertyValuesInternal());
}
| public void resetDependentRelations ( string $propertyName ) | ||
| $propertyName | string | |
| return | void | |
|---|---|---|
| public Yiisoft\ActiveRecord\ActiveRecordInterface|array|null retrieveRelation ( string $name ) | ||
| $name | string | |
| return | Yiisoft\ActiveRecord\ActiveRecordInterface|array|null | |
|---|---|---|
| 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 . '".');
}
}
Signup or Login in order to comment.