Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename $attributes to $properties #386

Merged
merged 12 commits into from
Sep 16, 2024
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ Now you can use the Active Record:
use App\Entity\User;

$user = new User();
$user->setAttribute('username', 'yiiliveext');
$user->setAttribute('email', 'yiiliveext@mail.ru');
$user->set('username', 'yiiliveext');
$user->set('email', 'yiiliveext@mail.ru');
$user->save();
```

Expand All @@ -146,8 +146,8 @@ $userQuery = new ActiveQuery(User::class);

$user = $userQuery->where(['id' => 1])->one();

$username = $user->getAttribute('username');
$email = $user->getAttribute('email');
$username = $user->get('username');
$email = $user->get('email');
```

## Documentation
Expand Down
14 changes: 7 additions & 7 deletions docs/create-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ final class User extends ActiveRecord

public function setId(int $id): void
{
$this->setAttribute('id', $id);
$this->set('id', $id);
}

public function setUsername(string $username): void
Expand Down Expand Up @@ -148,11 +148,11 @@ Notes:
- To access properties, you need to define getter and setter methods.
- ✔️ It allows using strict typing and define default values for properties;
- ✔️ It allows accessing uninitialized properties, using **null coalescing operator** `return $this->id ?? null;`
- ✔️ It allows resetting relations when setting the property, using `ActiveRecordInterface::setAttribute()` method.
- ✔️ It allows resetting relations when setting the property, using `ActiveRecordInterface::set()` method.

### Private properties

To use `private` properties inside the model class, you need to copy `getAttributesInternal()` and `populateAttribute()`
To use `private` properties inside the model class, you need to copy `propertyValuesInternal()` and `populateProperty()`
methods from the `ActiveRecord` class and adjust them to work with the `private` properties.

```php
Expand All @@ -176,13 +176,13 @@ final class User extends ActiveRecord
// Getters and setters as for protected properties
// ...

// Copied `getAttributesInternal()` and `populateAttribute()` methods from `ActiveRecord` class
protected function getAttributesInternal(): array
// Copied `propertyValuesInternal()` and `populateProperty()` methods from `ActiveRecord` class
protected function propertyValuesInternal(): array
{
return get_object_vars($this);
}

protected function populateAttribute(string $name, mixed $value): void
protected function populateProperty(string $name, mixed $value): void
{
$this->$name = $value;
}
Expand Down Expand Up @@ -224,7 +224,7 @@ You can use `$user->id`, `$user->username`, `$user->email` to access the propert

Notes:
- It needs to use the `MagicPropertiesTrait` to enable magic properties;
- Compared to dynamic properties, they're stored in the `private array $attributes` property;
- Compared to dynamic properties, they're stored in the `private array $properties` property;
- ✔️ It allows accessing relations as properties;
- ❌ It doesn't use strict typing and can be a reason of hard-to-detect errors;
- ❌ It is slower than explicitly defined properties, it is not optimized by PHP opcache and uses more memory.
Expand Down
Loading
Loading