Trait Yiisoft\ActiveRecord\Trait\ArrayAccessTrait
Trait to implement {@see ArrayAccess} interface for ActiveRecord.
See also:
- Yiisoft\ActiveRecord\ActiveRecordInterface::get()
- Yiisoft\ActiveRecord\ActiveRecordInterface::hasProperty()
- Yiisoft\ActiveRecord\ActiveRecordInterface::set()
- Yiisoft\ActiveRecord\ActiveRecordInterface::relation()
- Yiisoft\ActiveRecord\ActiveRecordInterface::isRelationPopulated()
- Yiisoft\ActiveRecord\ActiveRecordInterface::populateRelation()
- Yiisoft\ActiveRecord\ActiveRecordInterface::resetRelation()
Public Methods
| Method | Description | Defined By |
|---|---|---|
| get() | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait | |
| hasProperty() | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait | |
| isRelationPopulated() | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait | |
| offsetExists() | Returns whether there is an element at the specified offset. | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait |
| offsetGet() | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait | |
| offsetSet() | Sets the element at the specified offset. | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait |
| offsetUnset() | Sets the element value at the specified offset to null. | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait |
| populateRelation() | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait | |
| relation() | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait | |
| resetRelation() | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait | |
| set() | Yiisoft\ActiveRecord\Trait\ArrayAccessTrait |
Method Details
| public boolean isRelationPopulated ( string $name ) | ||
| $name | string | |
| return | boolean | |
|---|---|---|
Returns whether there is an element at the specified offset.
This method is required by the SPL interface {@see \Yiisoft\ActiveRecord\Trait\ArrayAccess}.
It is implicitly called when you use something like isset($model[$offset]).
| public boolean offsetExists ( string $offset ) | ||
| $offset | string |
The offset to check on. |
| return | boolean |
Whether an offset exists. |
|---|---|---|
public function offsetExists(mixed $offset): bool
{
if ($this->hasProperty($offset)) {
return $this->get($offset) !== null;
}
if (property_exists($this, $offset)) {
return isset($this->$offset);
}
if ($this->isRelationPopulated($offset)) {
return $this->relation($offset) !== null;
}
return false;
}
| public mixed offsetGet ( string $offset ) | ||
| $offset | string |
The offset to retrieve element. |
public function offsetGet(mixed $offset): mixed
{
if ($this->hasProperty($offset)) {
return $this->get($offset);
}
if (property_exists($this, $offset)) {
return $this->$offset ?? null;
}
return $this->relation($offset);
}
Sets the element at the specified offset.
This method is required by the SPL interface {@see \Yiisoft\ActiveRecord\Trait\ArrayAccess}.
It is implicitly called when you use something like $model[$offset] = $item;.
| public void offsetSet ( string $offset, mixed $value ) | ||
| $offset | string |
The offset to set element. |
| $value | mixed | |
public function offsetSet(mixed $offset, mixed $value): void
{
if ($this->hasProperty($offset)) {
$this->set($offset, $value);
return;
}
if (property_exists($this, $offset)) {
$this->$offset = $value;
return;
}
if ($value instanceof ActiveRecordInterface || is_array($value) || $value === null) {
$this->populateRelation($offset, $value);
return;
}
throw new InvalidArgumentException('Setting unknown property: ' . static::class . '::' . $offset);
}
Sets the element value at the specified offset to null.
This method is required by the SPL interface {@see \Yiisoft\ActiveRecord\Trait\ArrayAccess}.
It is implicitly called when you use something like unset($model[$offset]).
| public void offsetUnset ( string $offset ) | ||
| $offset | string |
The offset to unset element. |
public function offsetUnset(mixed $offset): void
{
if ($this->hasProperty($offset) || property_exists($this, $offset)) {
$this->set($offset, null);
unset($this->$offset);
return;
}
$this->resetRelation($offset);
}
| public void populateRelation ( string $name, Yiisoft\ActiveRecord\ActiveRecordInterface|array|null $record ) | ||
| $name | string | |
| $record | Yiisoft\ActiveRecord\ActiveRecordInterface|array|null | |
| return | void | |
|---|---|---|
| public Yiisoft\ActiveRecord\ActiveRecordInterface|array|null relation ( string $name ) | ||
| $name | string | |
| return | Yiisoft\ActiveRecord\ActiveRecordInterface|array|null | |
|---|---|---|
Signup or Login in order to comment.