You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`ArrayAccessTrait` implements [ArrayAccess](https://www.php.net/manual/en/class.arrayaccess.php) interface and allows accessing model properties and relations using array
4
+
syntax.
5
+
6
+
## Methods
7
+
8
+
The following methods are provided by the `ArrayAccessTrait`:
9
+
10
+
-`offsetExists($offset)` checks if the specified property or relation exists and is not null;
11
+
-`offsetGet($offset)` retrieves the value of the specified property or relation;
12
+
-`offsetSet($offset, $value)` sets the value of the specified property or relation;
13
+
-`offsetUnset($offset)` unsets or sets to null the specified property or relation.
14
+
15
+
## Usage
16
+
17
+
```php
18
+
use Yiisoft\ActiveRecord\ActiveRecord;
19
+
use Yiisoft\ActiveRecord\Trait\ArrayAccessTrait;
20
+
21
+
final class User extends ActiveRecord implements ArrayAccess
22
+
{
23
+
use ArrayAccessTrait;
24
+
25
+
protected string $username;
26
+
}
27
+
28
+
$user = new User();
29
+
$user['username'] = 'admin'; // Set the property value using array syntax
30
+
echo $user['username']; // Get the property value using array syntax
31
+
unset($user['username']); // Unset the property or set it to null using array syntax
32
+
isset($user['username']); // Check if the property exists and is not null using array syntax
`ArrayIteratorTrait` implements [IteratorAggregate](https://www.php.net/manual/en/class.iteratoraggregate.php) interface and allows accessing model properties iteratively.
4
+
5
+
## Methods
6
+
7
+
The following method is provided by the `ArrayIteratorTrait`:
8
+
9
+
-`getIterator()` returns an `ArrayIterator` instance that allows iterating over the model's properties.
10
+
11
+
## Usage
12
+
13
+
```php
14
+
use Yiisoft\ActiveRecord\ActiveRecord;
15
+
use Yiisoft\ActiveRecord\Trait\ArrayIteratorTrait;
16
+
17
+
final class User extends ActiveRecord implements IteratorAggregate
Copy file name to clipboardExpand all lines: docs/traits/arrayable.md
+10-5Lines changed: 10 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,26 @@
1
1
# ArrayableTrait
2
2
3
-
This trait provides methods to convert an `ActiveRecord` instance to an array format, using the `fields()`
4
-
and `extraFields()` methods to determine which properties and relations should be included in the output.
3
+
`ArrayableTrait` implements the [\Yiisoft\Arrays\ArrayableInterface](https://github.com/yiisoft/arrays/blob/master/src/ArrayableInterface.php) interface and provides methods to convert
4
+
an `ActiveRecord` instance to an array format, using the `fields()` and `extraFields()` methods to determine which
5
+
properties and relations should be included in the output.
5
6
6
7
## Methods
7
8
8
9
The following methods are provided by the `ArrayableTrait`:
10
+
9
11
-`extraFields()` returns an associative array of relation names that can be included in the array representation
10
-
of the record, with keys equal to values.
11
-
-`fields()` returns an associative array of the record's property names, with keys equal to values.
12
+
of the record, with keys equal to values;
13
+
-`fields()` returns an associative array of the record's property names, with keys equal to values;
12
14
-`toArray()` converts the `ActiveRecord` instance to an array using the defined fields and extra fields.
13
15
14
16
## Usage
15
17
16
18
```php
17
19
use Yiisoft\ActiveRecord\ActiveRecord;
18
20
use Yiisoft\ActiveRecord\Trait\ArrayableTrait;
21
+
use Yiisoft\Arrays\ArrayableInterface;
19
22
20
-
class User extends ActiveRecord
23
+
final class User extends ActiveRecord implements ArrayableInterface
21
24
{
22
25
use ArrayableTrait;
23
26
@@ -34,3 +37,5 @@ You can override the `fields()` and `extraFields()` methods in your `ActiveRecor
34
37
and relations are included in the array representation.
35
38
36
39
Both `fields()` and `extraFields()` may return values that are either strings or `Closure` instances.
0 commit comments