Skip to content

Commit a8b88b9

Browse files
authored
Add doc about ArrayAccessTrait and ArrayIteratorTrait (#456)
1 parent 5a2efc5 commit a8b88b9

File tree

9 files changed

+95
-15
lines changed

9 files changed

+95
-15
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,13 @@ $email = $user->get('email');
178178

179179
## Documentation
180180

181-
- [Optimistic Locking](docs/optimistic-locking.md)
182-
- [Internals](docs/internals.md)
181+
- [Define the Database Connection for Active Record](docs/define-connection.md);
182+
- [Create Active Record Model](docs/create-model.md);
183+
- [Define Active Record Relations](docs/define-relations.md);
184+
- [Extending Functionality With Traits](docs/traits/traits.md);
185+
- [Using Dependency Injection With Active Record Model](docs/using-di.md);
186+
- [Optimistic Locking](docs/optimistic-locking.md);
187+
- [Internals](docs/internals.md).
183188

184189
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
185190
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).

docs/create-model.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ $orders = $user->getOrders();
274274
For more information on defining relations, see [Define Relations](define-relations.md).
275275

276276
Also see
277+
- [Extending Functionality With Traits](traits/traits.md);
277278
- [Using Dependency Injection With Active Record Model](using-di.md);
278-
- [Extending functionality with traits](traits/traits.md).
279+
- [Optimistic Locking](optimistic-locking.md).
279280

280281
Back to [README](../README.md)

docs/define-connection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Define the database connection for Active Record
1+
# Define the Database Connection for Active Record
22

33
To use the Active Record, you need to define the DB connection in one of the following ways:
44

docs/define-relations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Active Record Relations
1+
# Define Active Record Relations
22

33
```mermaid
44
erDiagram

docs/traits/array-access.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ArrayAccessTrait
2+
3+
`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
33+
```
34+
35+
Back to [Extending with traits](traits.md).

docs/traits/array-iterator.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ArrayIteratorTrait
2+
3+
`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
18+
{
19+
use ArrayIteratorTrait;
20+
21+
public string $username;
22+
public string $email;
23+
}
24+
25+
$user = new User();
26+
$user->username = 'admin';
27+
$user->email = '[email protected]';
28+
29+
foreach ($user as $property => $value) {
30+
echo "$property: $value\n"; // Iterates over the model's properties
31+
}
32+
```
33+
34+
Back to [Extending with traits](traits.md).

docs/traits/arrayable.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# ArrayableTrait
22

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.
56

67
## Methods
78

89
The following methods are provided by the `ArrayableTrait`:
10+
911
- `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;
1214
- `toArray()` converts the `ActiveRecord` instance to an array using the defined fields and extra fields.
1315

1416
## Usage
1517

1618
```php
1719
use Yiisoft\ActiveRecord\ActiveRecord;
1820
use Yiisoft\ActiveRecord\Trait\ArrayableTrait;
21+
use Yiisoft\Arrays\ArrayableInterface;
1922

20-
class User extends ActiveRecord
23+
final class User extends ActiveRecord implements ArrayableInterface
2124
{
2225
use ArrayableTrait;
2326

@@ -34,3 +37,5 @@ You can override the `fields()` and `extraFields()` methods in your `ActiveRecor
3437
and relations are included in the array representation.
3538

3639
Both `fields()` and `extraFields()` may return values that are either strings or `Closure` instances.
40+
41+
Back to [Extending with traits](traits.md).

docs/traits/traits.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Extending with traits
1+
# Extending Functionality With Traits
22

33
The library provides several traits that can be used to extend the functionality of `ActiveRecord` models.
44
These traits can be included in your model classes to add specific behaviors or features.
55

66
- [ArrayableTrait](arrayable.md) provides `toArray()` method to convert a model to an array format;
7-
- `ArrayAccessTrait` allows accessing model properties and relations using array syntax;
8-
- `ArrayIteratorTrait` allows accessing model properties and relations iteratively;
7+
- [ArrayAccessTrait](array-access.md) allows accessing model properties and relations using array syntax;
8+
- [ArrayIteratorTrait](array-iterator.md) allows accessing model properties and relations iteratively;
99
- `CustomConnectionTrait` allows using a custom database connection for a model;
1010
- `CustomTableNameTrait` allows using a custom table name for a model;
1111
- `FactoryTrait` allows creating models and relations using [yiisoft/factory](https://github.com/yiisoft/factory);
@@ -17,4 +17,4 @@ These traits can be included in your model classes to add specific behaviors or
1717
in a model;
1818
- `RepositoryTrait` provides methods to interact with a model as a repository.
1919

20-
Back to [Create Active Record Model](../create-model.md)
20+
Back to [README](../../README.md)

src/Trait/ArrayableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use function array_keys;
1414

1515
/**
16-
* Trait to implement {@see \Yiisoft\Arrays\ArrayableInterface} interface for ActiveRecord.
16+
* Trait to implement {@see ArrayableInterface} interface for ActiveRecord.
1717
*
1818
* @method string[] propertyNames()
1919
* @see ActiveRecordInterface::propertyNames()

0 commit comments

Comments
 (0)