diff --git a/README.md b/README.md index 369218eaf..65b08a2bb 100644 --- a/README.md +++ b/README.md @@ -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(); ``` @@ -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 diff --git a/docs/create-model.md b/docs/create-model.md index 67889f320..6585b8747 100644 --- a/docs/create-model.md +++ b/docs/create-model.md @@ -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 @@ -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 @@ -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; } @@ -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. diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index 8e8bc9202..073a0089d 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -43,25 +43,25 @@ */ abstract class AbstractActiveRecord implements ActiveRecordInterface { - private array|null $oldAttributes = null; + private array|null $oldValues = null; private array $related = []; /** @psalm-var string[][] */ private array $relationsDependencies = []; /** - * Returns the public and protected property values of an Active Record object. + * Returns the available property values of an Active Record object. * * @return array * * @psalm-return array */ - abstract protected function getAttributesInternal(): array; + abstract protected function propertyValuesInternal(): array; /** * Inserts Active Record values into DB without considering transaction. * - * @param array|null $attributes List of attributes that need to be saved. Defaults to `null`, meaning all - * attributes that are loaded from DB will be saved. + * @param array|null $propertyNames List of property names that need to be saved. Defaults to `null`, meaning all + * changed property values will be saved. Only changed values will be saved. * * @throws Exception * @throws InvalidArgumentException @@ -70,12 +70,15 @@ abstract protected function getAttributesInternal(): array; * * @return bool Whether the record inserted successfully. */ - abstract protected function insertInternal(array $attributes = null): bool; + abstract protected function insertInternal(array|null $propertyNames = null): bool; /** - * Sets the value of the named attribute. + * Sets the value of the named property. + * + * @param string $name The property name. + * @param mixed $value The property value. */ - abstract protected function populateAttribute(string $name, mixed $value): void; + abstract protected function populateProperty(string $name, mixed $value): void; public function delete(): int { @@ -99,65 +102,65 @@ public function equals(ActiveRecordInterface $record): bool return $this->getTableName() === $record->getTableName() && $this->getPrimaryKey() === $record->getPrimaryKey(); } - public function getAttribute(string $name): mixed + public function get(string $propertyName): mixed { - return $this->getAttributesInternal()[$name] ?? null; + return $this->propertyValuesInternal()[$propertyName] ?? null; } - public function getAttributes(array|null $names = null, array $except = []): array + public function propertyValues(array|null $names = null, array $except = []): array { - $names ??= $this->attributes(); + $names ??= $this->propertyNames(); if (!empty($except)) { $names = array_diff($names, $except); } - return array_intersect_key($this->getAttributesInternal(), array_flip($names)); + return array_intersect_key($this->propertyValuesInternal(), array_flip($names)); } public function getIsNewRecord(): bool { - return $this->oldAttributes === null; + return $this->oldValues === null; } /** - * Returns the old value of the named attribute. + * Returns the old value of the named property. * - * If this record is the result of a query and the attribute is not loaded, `null` will be returned. + * If this record is the result of a query and the property is not loaded, `null` will be returned. * - * @param string $name The attribute name. + * @param string $propertyName The property name. * - * @return mixed The old attribute value. `null` if the attribute is not loaded before or doesn't exist. + * @return mixed The old property value. `null` if the property is not loaded before or doesn't exist. * - * {@see hasAttribute()} + * @see hasProperty() */ - public function getOldAttribute(string $name): mixed + public function oldValue(string $propertyName): mixed { - return $this->oldAttributes[$name] ?? null; + return $this->oldValues[$propertyName] ?? null; } /** - * Returns the attribute values that have been modified since they're loaded or saved most recently. + * Returns the property values that have been modified since they're loaded or saved most recently. * * The comparison of new and old values uses `===`. * - * @param array|null $names The names of the attributes whose values may be returned if they're changed recently. - * If null, {@see attributes()} will be used. + * @param array|null $propertyNames The names of the properties whose values may be returned if they're changed recently. + * If null, {@see propertyNames()} will be used. * - * @return array The changed attribute values (name-value pairs). + * @return array The changed property values (name-value pairs). */ - public function getDirtyAttributes(array $names = null): array + public function dirtyValues(array|null $propertyNames = null): array { - $attributes = $this->getAttributes($names); + $values = $this->propertyValues($propertyNames); - if ($this->oldAttributes === null) { - return $attributes; + if ($this->oldValues === null) { + return $values; } - $result = array_diff_key($attributes, $this->oldAttributes); + $result = array_diff_key($values, $this->oldValues); - foreach (array_diff_key($attributes, $result) as $name => $value) { - if ($value !== $this->oldAttributes[$name]) { + foreach (array_diff_key($values, $result) as $name => $value) { + if ($value !== $this->oldValues[$name]) { $result[$name] = $value; } } @@ -165,9 +168,9 @@ public function getDirtyAttributes(array $names = null): array return $result; } - public function getOldAttributes(): array + public function oldValues(): array { - return $this->oldAttributes ?? []; + return $this->oldValues ?? []; } /** @@ -186,13 +189,13 @@ public function getOldPrimaryKey(bool $asArray = false): mixed } if ($asArray === false && count($keys) === 1) { - return $this->oldAttributes[$keys[0]] ?? null; + return $this->oldValues[$keys[0]] ?? null; } $values = []; foreach ($keys as $name) { - $values[$name] = $this->oldAttributes[$name] ?? null; + $values[$name] = $this->oldValues[$name] ?? null; } return $values; @@ -203,13 +206,13 @@ public function getPrimaryKey(bool $asArray = false): mixed $keys = $this->primaryKey(); if ($asArray === false && count($keys) === 1) { - return $this->getAttribute($keys[0]); + return $this->get($keys[0]); } $values = []; foreach ($keys as $name) { - $values[$name] = $this->getAttribute($name); + $values[$name] = $this->get($name); } return $values; @@ -227,9 +230,9 @@ public function getRelatedRecords(): array return $this->related; } - public function hasAttribute(string $name): bool + public function hasProperty(string $name): bool { - return in_array($name, $this->attributes(), true); + return in_array($name, $this->propertyNames(), true); } /** @@ -251,16 +254,16 @@ public function hasAttribute(string $name): bool * } * ``` * - * Note that the `customer_id` key in the `$link` parameter refers to an attribute name in the related - * class `Order`, while the 'id' value refers to an attribute name in the current AR class. + * Note that the `customer_id` key in the `$link` parameter refers to a property name in the related + * class `Order`, while the 'id' value refers to a property name in the current active record class. * * Call methods declared in {@see ActiveQuery} to further customize the relation. * * @param ActiveRecordInterface|Closure|string $class The class name of the related record, or an instance of * the related record, or a Closure to create an {@see ActiveRecordInterface} object. - * @param array $link The primary-foreign key constraint. The keys of the array refer to the attributes of - * the record associated with the `$class` model, while the values of the array refer to the corresponding attributes - * in **this** AR class. + * @param array $link The primary-foreign key constraint. The keys of the array refer to the property names of + * the record associated with the `$class` model, while the values of the array refer to the corresponding property + * names in **this** active record class. * * @return ActiveQueryInterface The relational query object. * @@ -290,16 +293,16 @@ public function hasMany(string|ActiveRecordInterface|Closure $class, array $link * } * ``` * - * Note that the `id` key in the `$link` parameter refers to an attribute name in the related class - * `Country`, while the `country_id` value refers to an attribute name in the current AR class. + * Note that the `id` key in the `$link` parameter refers to a property name in the related class + * `Country`, while the `country_id` value refers to a property name in the current active record class. * * Call methods declared in {@see ActiveQuery} to further customize the relation. * * @param ActiveRecordInterface|Closure|string $class The class name of the related record, or an instance of * the related record, or a Closure to create an {@see ActiveRecordInterface} object. - * @param array $link The primary-foreign key constraint. The keys of the array refer to the attributes of - * the record associated with the `$class` model, while the values of the array refer to the corresponding attributes in - * **this** AR class. + * @param array $link The primary-foreign key constraint. The keys of the array refer to the property names of + * the record associated with the `$class` model, while the values of the array refer to the corresponding property + * names in **this** active record class. * * @return ActiveQueryInterface The relational query object. * @@ -310,9 +313,9 @@ public function hasOne(string|ActiveRecordInterface|Closure $class, array $link) return $this->createRelationQuery($class, $link, false); } - public function insert(array $attributes = null): bool + public function insert(array|null $propertyNames = null): bool { - return $this->insertInternal($attributes); + return $this->insertInternal($propertyNames); } /** @@ -327,23 +330,23 @@ public function instantiateQuery(string|ActiveRecordInterface|Closure $arClass): } /** - * Returns whether the named attribute has been changed. + * Returns whether the named property has been changed. * - * @param string $name The name of the attribute. + * @param string $name The name of the property. * @param bool $identical Whether the comparison of new and old value uses `===`, * defaults to `true`. Otherwise, `==` is used for comparison. * - * @return bool Whether the attribute has been changed. + * @return bool Whether the property value has been changed. */ - public function isAttributeChanged(string $name, bool $identical = true): bool + public function isPropertyChanged(string $name, bool $identical = true): bool { - $attributes = $this->getAttributesInternal(); + $values = $this->propertyValuesInternal(); - if (empty($this->oldAttributes) || !array_key_exists($name, $this->oldAttributes)) { - return array_key_exists($name, $attributes); + if (empty($this->oldValues) || !array_key_exists($name, $this->oldValues)) { + return array_key_exists($name, $values); } - return !array_key_exists($name, $attributes) || $attributes[$name] !== $this->oldAttributes[$name]; + return !array_key_exists($name, $values) || $values[$name] !== $this->oldValues[$name]; } public function isPrimaryKey(array $keys): bool @@ -359,11 +362,11 @@ public function isRelationPopulated(string $name): bool return array_key_exists($name, $this->related); } - public function link(string $name, ActiveRecordInterface $arClass, array $extraColumns = []): void + public function link(string $relationName, ActiveRecordInterface $arClass, array $extraColumns = []): void { $viaClass = null; $viaTable = null; - $relation = $this->relationQuery($name); + $relation = $this->relationQuery($relationName); $via = $relation->getVia(); if ($via !== null) { @@ -397,7 +400,7 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC */ foreach ($viaLink as $a => $b) { /** @psalm-var mixed */ - $columns[$a] = $this->getAttribute($b); + $columns[$a] = $this->get($b); } $link = $relation->getLink(); @@ -408,7 +411,7 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC */ foreach ($link as $a => $b) { /** @psalm-var mixed */ - $columns[$b] = $arClass->getAttribute($a); + $columns[$b] = $arClass->get($a); } /** @@ -426,7 +429,7 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC * @psalm-var mixed $value */ foreach ($columns as $column => $value) { - $viaClass->setAttribute($column, $value); + $viaClass->set($column, $value); } $viaClass->insert(); @@ -461,37 +464,37 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC // Update lazily loaded related objects. if (!$relation->getMultiple()) { - $this->related[$name] = $arClass; - } elseif (isset($this->related[$name])) { + $this->related[$relationName] = $arClass; + } elseif (isset($this->related[$relationName])) { $indexBy = $relation->getIndexBy(); if ($indexBy !== null) { if ($indexBy instanceof Closure) { - $index = $indexBy($arClass->getAttributes()); + $index = $indexBy($arClass->propertyValues()); } else { - $index = $arClass->getAttribute($indexBy); + $index = $arClass->get($indexBy); } if ($index !== null) { - $this->related[$name][$index] = $arClass; + $this->related[$relationName][$index] = $arClass; } } else { - $this->related[$name][] = $arClass; + $this->related[$relationName][] = $arClass; } } } /** - * Marks an attribute dirty. + * Marks a property dirty. * * This method may be called to force updating a record when calling {@see update()}, even if there is no change * being made to the record. * - * @param string $name The attribute name. + * @param string $name The property name. */ - public function markAttributeDirty(string $name): void + public function markPropertyDirty(string $name): void { - if ($this->oldAttributes !== null && $name !== '') { - unset($this->oldAttributes[$name]); + if ($this->oldValues !== null && $name !== '') { + unset($this->oldValues[$name]); } } @@ -527,17 +530,17 @@ public function optimisticLock(): string|null * This is an internal method meant to be called to create active record objects after fetching data from the * database. It is mainly used by {@see ActiveQuery} to populate the query results into active records. * - * @param array|object $row Attribute values (name => value). + * @param array|object $row Property values (name => value). */ public function populateRecord(array|object $row): void { if ($row instanceof ActiveRecordInterface) { - $row = $row->getAttributes(); + $row = $row->propertyValues(); } foreach ($row as $name => $value) { - $this->populateAttribute($name, $value); - $this->oldAttributes[$name] = $value; + $this->populateProperty($name, $value); + $this->oldValues[$name] = $value; } $this->related = []; @@ -599,67 +602,43 @@ protected function retrieveRelation(string $name): ActiveRecordInterface|array|n return $this->related[$name] = $query->relatedRecords(); } - /** - * Saves the current record. - * - * This method will call {@see insert()} when {@see getIsNewRecord} is `true`, or {@see update()} when - * {@see getIsNewRecord} is `false`. - * - * For example, to save a customer record: - * - * ```php - * $customer = new Customer($db); - * $customer->name = $name; - * $customer->email = $email; - * $customer->save(); - * ``` - * - * @param array|null $attributeNames List of attribute names that need to be saved. Defaults to null, meaning all - * attributes that are loaded from DB will be saved. - * - * @throws InvalidConfigException - * @throws StaleObjectException - * @throws Throwable - * - * @return bool Whether the saving succeeded (i.e., no validation errors occurred). - */ - public function save(array $attributeNames = null): bool + public function save(array|null $propertyNames = null): bool { if ($this->getIsNewRecord()) { - return $this->insert($attributeNames); + return $this->insert($propertyNames); } - $this->update($attributeNames); + $this->update($propertyNames); return true; } - public function setAttribute(string $name, mixed $value): void + public function set(string $propertyName, mixed $value): void { if ( - isset($this->relationsDependencies[$name]) - && ($value === null || $this->getAttribute($name) !== $value) + isset($this->relationsDependencies[$propertyName]) + && ($value === null || $this->get($propertyName) !== $value) ) { - $this->resetDependentRelations($name); + $this->resetDependentRelations($propertyName); } - $this->populateAttribute($name, $value); + $this->populateProperty($propertyName, $value); } /** - * Sets the attribute values in a massive way. + * Sets the property values in a massive way. * - * @param array $values Attribute values (name => value) to be assigned to the model. + * @param array $values Property values (name => value) to be assigned to the model. * - * {@see attributes()} + * @see propertyNames() */ - public function setAttributes(array $values): void + public function populateProperties(array $values): void { - $values = array_intersect_key($values, array_flip($this->attributes())); + $values = array_intersect_key($values, array_flip($this->propertyNames())); /** @psalm-var mixed $value */ foreach ($values as $name => $value) { - $this->populateAttribute($name, $value); + $this->populateProperty($name, $value); } } @@ -672,67 +651,68 @@ public function setAttributes(array $values): void */ public function setIsNewRecord(bool $value): void { - $this->oldAttributes = $value ? null : $this->getAttributesInternal(); + $this->oldValues = $value ? null : $this->propertyValuesInternal(); } /** - * Sets the old value of the named attribute. + * Sets the old value of the named property. * - * @param string $name The attribute name. + * @param string $propertyName The property name. * - * @throws InvalidArgumentException If the named attribute doesn't exist. + * @throws InvalidArgumentException If the named property doesn't exist. * - * {@see hasAttribute()} + * @see hasProperty() */ - public function setOldAttribute(string $name, mixed $value): void + public function assignOldValue(string $propertyName, mixed $value): void { - if (isset($this->oldAttributes[$name]) || $this->hasAttribute($name)) { - $this->oldAttributes[$name] = $value; + if (isset($this->oldValues[$propertyName]) || $this->hasProperty($propertyName)) { + $this->oldValues[$propertyName] = $value; } else { - throw new InvalidArgumentException(static::class . ' has no attribute named "' . $name . '".'); + throw new InvalidArgumentException(static::class . ' has no property named "' . $propertyName . '".'); } } /** - * Sets the old attribute values. + * Sets the old property values. * - * All existing old attribute values will be discarded. + * All existing old property values will be discarded. * - * @param array|null $values Old attribute values to be set. If set to `null` this record is {@see isNewRecord|new}. + * @param array|null $propertyValues Old property values (name => value) to be set. If set to `null` this record + * is {@see isNewRecord() new}. */ - public function setOldAttributes(array $values = null): void + public function assignOldValues(array|null $propertyValues = null): void { - $this->oldAttributes = $values; + $this->oldValues = $propertyValues; } - public function update(array $attributeNames = null): int + public function update(array|null $propertyNames = null): int { - return $this->updateInternal($attributeNames); + return $this->updateInternal($propertyNames); } - public function updateAll(array $attributes, array|string $condition = [], array $params = []): int + public function updateAll(array $propertyValues, array|string $condition = [], array $params = []): int { $command = $this->db()->createCommand(); - $command->update($this->getTableName(), $attributes, $condition, $params); + $command->update($this->getTableName(), $propertyValues, $condition, $params); return $command->execute(); } - public function updateAttributes(array $attributes): int + public function updateProperties(array $properties): int { - $attrs = []; + $names = []; - foreach ($attributes as $name => $value) { + foreach ($properties as $name => $value) { if (is_int($name)) { - $attrs[] = $value; + $names[] = $value; } else { - $this->setAttribute($name, $value); - $attrs[] = $name; + $this->set($name, $value); + $names[] = $name; } } - $values = $this->getDirtyAttributes($attrs); + $values = $this->dirtyValues($names); if (empty($values) || $this->getIsNewRecord()) { return 0; @@ -740,7 +720,7 @@ public function updateAttributes(array $attributes): int $rows = $this->updateAll($values, $this->getOldPrimaryKey(true)); - $this->oldAttributes = array_merge($this->oldAttributes ?? [], $values); + $this->oldValues = array_merge($this->oldValues ?? [], $values); return $rows; } @@ -757,7 +737,7 @@ public function updateAttributes(array $attributes): int * * Note that this method will not trigger any events. * - * @param array $counters The counters to be updated (attribute name => increment value). + * @param array $counters The counters to be updated (property name => increment value). * Use negative values if you want to decrement the counters. * @param array|string $condition The conditions that will be put in the `WHERE` part of the `UPDATE` SQL. * Please refer to {@see Query::where()} on how to specify this parameter. @@ -800,7 +780,7 @@ public function updateAllCounters(array $counters, array|string $condition = '', * $post->updateCounters(['view_count' => 1]); * ``` * - * @param array $counters The counters to be updated (attribute name => increment value), use negative values if you + * @param array $counters The counters to be updated (property name => increment value), use negative values if you * want to decrement the counters. * * @psalm-param array $counters @@ -819,19 +799,19 @@ public function updateCounters(array $counters): bool } foreach ($counters as $name => $value) { - $value += $this->getAttribute($name) ?? 0; - $this->populateAttribute($name, $value); - $this->oldAttributes[$name] = $value; + $value += $this->get($name) ?? 0; + $this->populateProperty($name, $value); + $this->oldValues[$name] = $value; } return true; } - public function unlink(string $name, ActiveRecordInterface $arClass, bool $delete = false): void + public function unlink(string $relationName, ActiveRecordInterface $arClass, bool $delete = false): void { $viaClass = null; $viaTable = null; - $relation = $this->relationQuery($name); + $relation = $this->relationQuery($relationName); $viaRelation = $relation->getVia(); if ($viaRelation !== null) { @@ -853,14 +833,14 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet foreach ($viaRelation->getLink() as $a => $b) { /** @psalm-var mixed */ - $columns[$a] = $this->getAttribute($b); + $columns[$a] = $this->get($b); } $link = $relation->getLink(); foreach ($link as $a => $b) { /** @psalm-var mixed */ - $columns[$b] = $arClass->getAttribute($a); + $columns[$b] = $arClass->get($a); } $nulls = array_fill_keys(array_keys($columns), null); @@ -890,22 +870,22 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet $arClass->delete(); } else { foreach ($relation->getLink() as $a => $b) { - $arClass->setAttribute($a, null); + $arClass->set($a, null); } $arClass->save(); } } elseif ($arClass->isPrimaryKey(array_keys($relation->getLink()))) { foreach ($relation->getLink() as $a => $b) { /** @psalm-var mixed $values */ - $values = $this->getAttribute($b); - /** relation via array valued attribute */ + $values = $this->get($b); + /** relation via array valued property */ if (is_array($values)) { - if (($key = array_search($arClass->getAttribute($a), $values, false)) !== false) { + if (($key = array_search($arClass->get($a), $values, false)) !== false) { unset($values[$key]); - $this->setAttribute($b, array_values($values)); + $this->set($b, array_values($values)); } } else { - $this->setAttribute($b, null); + $this->set($b, null); } } $delete ? $this->delete() : $this->save(); @@ -915,13 +895,13 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet } if (!$relation->getMultiple()) { - unset($this->related[$name]); - } elseif (isset($this->related[$name]) && is_array($this->related[$name])) { + unset($this->related[$relationName]); + } elseif (isset($this->related[$relationName]) && is_array($this->related[$relationName])) { /** @psalm-var array $related */ - $related = $this->related[$name]; + $related = $this->related[$relationName]; foreach ($related as $a => $b) { if ($arClass->getPrimaryKey() === $b->getPrimaryKey()) { - unset($this->related[$name][$a]); + unset($this->related[$relationName][$a]); } } } @@ -935,8 +915,7 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet * * To destroy the relationship without removing records, make sure your keys can be set to `null`. * - * @param string $name The case-sensitive name of the relationship, e.g., `orders` for a relation defined via - * `getOrders()` method. + * @param string $relationName The case-sensitive name of the relationship. * @param bool $delete Whether to delete the model that contains the foreign key. * * @throws Exception @@ -944,11 +923,11 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet * @throws StaleObjectException * @throws Throwable */ - public function unlinkAll(string $name, bool $delete = false): void + public function unlinkAll(string $relationName, bool $delete = false): void { $viaClass = null; $viaTable = null; - $relation = $this->relationQuery($name); + $relation = $this->relationQuery($relationName); $viaRelation = $relation->getVia(); if ($viaRelation !== null) { @@ -971,7 +950,7 @@ public function unlinkAll(string $name, bool $delete = false): void foreach ($viaRelation->getLink() as $a => $b) { $nulls[$a] = null; /** @psalm-var mixed */ - $condition[$a] = $this->getAttribute($b); + $condition[$a] = $this->get($b); } if (!empty($viaRelation->getWhere())) { @@ -1001,9 +980,9 @@ public function unlinkAll(string $name, bool $delete = false): void $relatedModel = $relation->getARInstance(); $link = $relation->getLink(); - if (!$delete && count($link) === 1 && is_array($this->getAttribute($b = reset($link)))) { - /** relation via array valued attribute */ - $this->setAttribute($b, []); + if (!$delete && count($link) === 1 && is_array($this->get($b = reset($link)))) { + /** relation via array valued property */ + $this->set($b, []); $this->save(); } else { $nulls = []; @@ -1012,7 +991,7 @@ public function unlinkAll(string $name, bool $delete = false): void foreach ($relation->getLink() as $a => $b) { $nulls[$a] = null; /** @psalm-var mixed */ - $condition[$a] = $this->getAttribute($b); + $condition[$a] = $this->get($b); } if (!empty($relation->getWhere())) { @@ -1031,7 +1010,7 @@ public function unlinkAll(string $name, bool $delete = false): void } } - unset($this->related[$name]); + unset($this->related[$relationName]); } /** @@ -1049,10 +1028,10 @@ protected function setRelationDependencies( $via = $relation->getVia(); if (empty($via)) { - foreach ($relation->getLink() as $attribute) { - $this->relationsDependencies[$attribute][$name] = $name; + foreach ($relation->getLink() as $propertyName) { + $this->relationsDependencies[$propertyName][$name] = $name; if ($viaRelationName !== null) { - $this->relationsDependencies[$attribute][] = $viaRelationName; + $this->relationsDependencies[$propertyName][] = $viaRelationName; } } } elseif ($via instanceof ActiveQueryInterface) { @@ -1105,7 +1084,7 @@ protected function deleteInternal(): int $lock = $this->optimisticLock(); if ($lock !== null) { - $condition[$lock] = $this->getAttribute($lock); + $condition[$lock] = $this->get($lock); $result = $this->deleteAll($condition); @@ -1116,7 +1095,7 @@ protected function deleteInternal(): int $result = $this->deleteAll($condition); } - $this->setOldAttributes(); + $this->assignOldValues(); return $result; } @@ -1124,23 +1103,23 @@ protected function deleteInternal(): int /** * Repopulates this active record with the latest data from a newly fetched instance. * - * @param ActiveRecordInterface|array|null $record The record to take attributes from. + * @param ActiveRecordInterface|array|null $record The record to take property values from. * * @return bool Whether refresh was successful. * * {@see refresh()} */ - protected function refreshInternal(array|ActiveRecordInterface $record = null): bool + protected function refreshInternal(array|ActiveRecordInterface|null $record = null): bool { if ($record === null || is_array($record)) { return false; } - foreach ($this->attributes() as $name) { - $this->populateAttribute($name, $record->getAttribute($name)); + foreach ($this->propertyNames() as $name) { + $this->populateProperty($name, $record->get($name)); } - $this->oldAttributes = $record->getOldAttributes(); + $this->oldValues = $record->oldValues(); $this->related = []; $this->relationsDependencies = []; @@ -1150,7 +1129,7 @@ protected function refreshInternal(array|ActiveRecordInterface $record = null): /** * {@see update()} * - * @param array|null $attributes Attributes to update. + * @param array|null $propertyNames Property names to update. * * @throws Exception * @throws NotSupportedException @@ -1158,9 +1137,9 @@ protected function refreshInternal(array|ActiveRecordInterface $record = null): * * @return int The number of rows affected. */ - protected function updateInternal(array $attributes = null): int + protected function updateInternal(array|null $propertyNames = null): int { - $values = $this->getDirtyAttributes($attributes); + $values = $this->dirtyValues($propertyNames); if (empty($values)) { return 0; @@ -1170,7 +1149,7 @@ protected function updateInternal(array $attributes = null): int $lock = $this->optimisticLock(); if ($lock !== null) { - $lockValue = $this->getAttribute($lock); + $lockValue = $this->get($lock); $condition[$lock] = $lockValue; $values[$lock] = ++$lockValue; @@ -1181,12 +1160,12 @@ protected function updateInternal(array $attributes = null): int throw new StaleObjectException('The object being updated is outdated.'); } - $this->populateAttribute($lock, $lockValue); + $this->populateProperty($lock, $lockValue); } else { $rows = $this->updateAll($values, $condition); } - $this->oldAttributes = array_merge($this->oldAttributes ?? [], $values); + $this->oldValues = array_merge($this->oldValues ?? [], $values); return $rows; } @@ -1199,7 +1178,7 @@ private function bindModels( /** @psalm-var string[] $link */ foreach ($link as $fk => $pk) { /** @psalm-var mixed $value */ - $value = $primaryModel->getAttribute($pk); + $value = $primaryModel->get($pk); if ($value === null) { throw new InvalidCallException( @@ -1208,37 +1187,37 @@ private function bindModels( } /** - * Relation via array valued attribute. + * Relation via array valued property. */ - if (is_array($fkValue = $foreignModel->getAttribute($fk))) { + if (is_array($fkValue = $foreignModel->get($fk))) { /** @psalm-var mixed */ $fkValue[] = $value; - $foreignModel->setAttribute($fk, $fkValue); + $foreignModel->set($fk, $fkValue); } else { - $foreignModel->setAttribute($fk, $value); + $foreignModel->set($fk, $value); } } $foreignModel->save(); } - protected function hasDependentRelations(string $attribute): bool + protected function hasDependentRelations(string $propertyName): bool { - return isset($this->relationsDependencies[$attribute]); + return isset($this->relationsDependencies[$propertyName]); } /** - * Resets dependent related models checking if their links contain specific attribute. + * Resets dependent related models checking if their links contain specific property. * - * @param string $attribute The changed attribute name. + * @param string $propertyName The changed property name. */ - protected function resetDependentRelations(string $attribute): void + protected function resetDependentRelations(string $propertyName): void { - foreach ($this->relationsDependencies[$attribute] as $relation) { + foreach ($this->relationsDependencies[$propertyName] as $relation) { unset($this->related[$relation]); } - unset($this->relationsDependencies[$attribute]); + unset($this->relationsDependencies[$propertyName]); } public function getTableName(): string diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 692ad3b66..29f33b430 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -316,7 +316,7 @@ private function removeDuplicatedModels(array $models): array if (count($pks) === 1) { $key = array_key_first($pks); - $hash = array_map(static fn ($model): string => (string) $model->getAttribute($key), $models); + $hash = array_map(static fn ($model): string => (string) $model->get($key), $models); } else { $hash = array_map(static fn ($model): string => serialize($model->getPrimaryKey(true)), $models); } diff --git a/src/ActiveQueryInterface.php b/src/ActiveQueryInterface.php index c880e27ad..6171c43a6 100644 --- a/src/ActiveQueryInterface.php +++ b/src/ActiveQueryInterface.php @@ -378,7 +378,7 @@ public function relatedRecords(): ActiveRecordInterface|array|null; * (or `null` if not found). * - A non-associative array: query by a list of primary key values and return the first record (or `null` if not * found). - * - An associative array of name-value pairs: query by a set of attribute values and return a single record + * - An associative array of name-value pairs: query by a set of property values and return a single record * matching all them (or `null` if not found). * * Note that `['id' => 1, 2]` is treated as a non-associative array. @@ -395,27 +395,27 @@ public function relatedRecords(): ActiveRecordInterface|array|null; * * ```php * // find a single customer whose primary key value is 10 - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $query = $customerQuery->findOne(10); * * // the above code is equal to: - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $query = $customerQuery->where(['id' => 10])->one(); * * // find the customers whose primary key value is 10, 11 or 12. - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $query = $customerQuery->findOne([10, 11, 12]); * * // the above code is equal to: - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $query = $customerQuery->where(['id' => [10, 11, 12]])->one(); * * // find the first customer whose age is 30 and whose status is 1 - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $query = $customerQuery->findOne(['age' => 30, 'status' => 1]); * * // the above code is equal to: - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $query = $customerQuery->where(['age' => 30, 'status' => 1])->one(); * ``` * @@ -427,7 +427,7 @@ public function relatedRecords(): ActiveRecordInterface|array|null; * { * $id = (string) $request->getAttribute('id'); * - * $aqClass = new ActiveQuery(Post::class, $db); + * $aqClass = new ActiveQuery(Post::class); * $query = $aqClass->findOne($id); * } * @@ -435,14 +435,14 @@ public function relatedRecords(): ActiveRecordInterface|array|null; * single record: * * ```php - * $aqClass = new ActiveQuery(Post::class, $db); + * $aqClass = new ActiveQuery(Post::class); * $query = $aqClass->findOne(['id' => $id); * ``` * * Do NOT use the following code!, it is possible to inject an array condition to filter by arbitrary column values!: * * ```php - * $aqClass = new ActiveQuery(Post::class, $db); + * $aqClass = new ActiveQuery(Post::class); * $query = $aqClass->findOne($id); * ``` * @@ -463,7 +463,7 @@ public function findOne(mixed $condition): array|ActiveRecordInterface|null; * empty array if none found). * Note that an empty condition will result in an empty result as it will be interpreted as a search for * primary keys and not an empty `WHERE` condition. - * - An associative array of name-value pairs: query by a set of attribute values and return an array of records + * - An associative array of name-value pairs: query by a set of property values and return an array of records * matching all them (or an empty array if none was found). * * Note that `['id' => 1, 2]` is treated as a non-associative array. @@ -481,27 +481,27 @@ public function findOne(mixed $condition): array|ActiveRecordInterface|null; * * ```php * // find the customers whose primary key value is 10. - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $customers = $customerQuery->findAll(10); * * // the above code is equal to. - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $customers = $customerQuery->where(['id' => 10])->all(); * * // find the customers whose primary key value is 10, 11 or 12. - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $customers = $customerQuery->findAll([10, 11, 12]); * * // the above code is equal to, - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $customers = $customerQuery->where(['id' => [10, 11, 12]])->all(); * * // find customers whose age is 30 and whose status is 1. - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $customers = $customerQuery->findAll(['age' => 30, 'status' => 1]); * * // the above code is equal to. - * $customerQuery = new ActiveQuery(Customer::class, $db); + * $customerQuery = new ActiveQuery(Customer::class); * $customers = $customerQuery->where(['age' => 30, 'status' => 1])->all(); * ``` * @@ -513,7 +513,7 @@ public function findOne(mixed $condition): array|ActiveRecordInterface|null; * { * $id = (string) $request->getAttribute('id'); * - * $aqClass = new ActiveQuery(Post::class, $db); + * $aqClass = new ActiveQuery(Post::class); * $query = $aqClass->findOne($id); * } * ``` @@ -522,14 +522,14 @@ public function findOne(mixed $condition): array|ActiveRecordInterface|null; * record: * * ```php - * $aqClass = new ActiveQuery(Post::class, $db); + * $aqClass = new ActiveQuery(Post::class); * $aqCLass = $aqClass->findOne(['id' => $id]); * ``` * * Do NOT use the following code! It's possible to inject an array condition to filter by arbitrary column values!: * * ```php - * $aqClass = new ActiveQuery(Post::class, $db); + * $aqClass = new ActiveQuery(Post::class); * $aqClass = $aqClass->findOne($id); * ``` * diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index e6f40bc05..150575a9b 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -28,13 +28,13 @@ * Active Record implements the [Active Record design pattern](https://en.wikipedia.org/wiki/Active_record). * * The premise behind Active Record is that an individual {@see ActiveRecord} object is associated with a specific row - * in a database table. The object's attributes are mapped to the columns of the corresponding table. + * in a database table. The object's properties are mapped to the columns of the corresponding table. * - * Referencing an Active Record attribute is equivalent to accessing the corresponding table column for that record. + * Referencing an Active Record property is equivalent to accessing the corresponding table column for that record. * * As an example, say that the `Customer` ActiveRecord class is associated with the `customer` table. * - * This would mean that the class's `name` attribute is automatically mapped to the `name` column in `customer` table. + * This would mean that the class's `name` property is automatically mapped to the `name` column in `customer` table. * Thanks to Active Record, assuming the variable `$customer` is an object of type `Customer`, to get the value of the * `name` column for the table row, you can use the expression `$customer->name`. * @@ -80,14 +80,14 @@ */ class ActiveRecord extends AbstractActiveRecord { - public function attributes(): array + public function propertyNames(): array { return $this->getTableSchema()->getColumnNames(); } - public function columnType(string $columnName): string + public function columnType(string $propertyName): string { - return $this->getTableSchema()->getColumn($columnName)?->getType() ?? SchemaInterface::TYPE_STRING; + return $this->getTableSchema()->getColumn($propertyName)?->getType() ?? SchemaInterface::TYPE_STRING; } public function filterCondition(array $condition, array $aliases = []): array @@ -147,7 +147,7 @@ public function getTableSchema(): TableSchemaInterface * $customer->loadDefaultValues(); * ``` * - * @param bool $skipIfSet Whether existing value should be preserved. This will only set defaults for attributes + * @param bool $skipIfSet Whether existing value should be preserved. This will only set defaults for properties * that are `null`. * * @throws Exception @@ -158,8 +158,8 @@ public function getTableSchema(): TableSchemaInterface public function loadDefaultValues(bool $skipIfSet = true): self { foreach ($this->getTableSchema()->getColumns() as $name => $column) { - if ($column->getDefaultValue() !== null && (!$skipIfSet || $this->getAttribute($name) === null)) { - $this->setAttribute($name, $column->getDefaultValue()); + if ($column->getDefaultValue() !== null && (!$skipIfSet || $this->get($name) === null)) { + $this->set($name, $column->getDefaultValue()); } } @@ -235,14 +235,14 @@ protected function filterValidColumnNames(array $aliases): array return $columnNames; } - protected function getAttributesInternal(): array + protected function propertyValuesInternal(): array { return get_object_vars($this); } - protected function insertInternal(array $attributes = null): bool + protected function insertInternal(array $propertyNames = null): bool { - $values = $this->getDirtyAttributes($attributes); + $values = $this->dirtyValues($propertyNames); $primaryKeys = $this->db()->createCommand()->insertWithReturningPks($this->getTableName(), $values); if ($primaryKeys === false) { @@ -253,16 +253,16 @@ protected function insertInternal(array $attributes = null): bool foreach ($primaryKeys as $name => $value) { $id = $columns[$name]->phpTypecast($value); - $this->setAttribute($name, $id); + $this->set($name, $id); $values[$name] = $id; } - $this->setOldAttributes($values); + $this->assignOldValues($values); return true; } - protected function populateAttribute(string $name, mixed $value): void + protected function populateProperty(string $name, mixed $value): void { $this->$name = $value; } diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index dcf087f78..6fd9e97b5 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -11,26 +11,22 @@ use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Exception\StaleObjectException; -use Yiisoft\Db\Schema\SchemaInterface; interface ActiveRecordInterface { /** - * Returns the list of all attribute names of the model. + * Returns the list of property names mapped to column names of the table associated with this active record class. * - * The default implementation will return all column names of the table associated with this AR class. - * - * @return array List of attribute names. + * @return array List of property names. * * @psalm-return string[] */ - public function attributes(): array; + public function propertyNames(): array; /** - * Returns the abstract type of the column. See {@see SchemaInterface} constants started with prefix `TYPE_` for - * possible abstract types. + * Returns the abstract type of the property. */ - public function columnType(string $columnName): string; + public function columnType(string $propertyName): string; /** * Returns the database connection used by the Active Record instance. @@ -120,32 +116,31 @@ public function filterCondition(array $condition, array $aliases = []): array; public function filterValidAliases(ActiveQuery $query): array; /** - * Returns the named attribute value. + * Returns the named property value. * - * If this record is the result of a query and the attribute isn't loaded, `null` will be returned. + * If this record is the result of a query and the property isn't loaded, `null` will be returned. * - * @param string $name The attribute name. + * @param string $propertyName The property name. * - * @return mixed The attribute value. `null` if the attribute isn't set or doesn't exist. + * @return mixed The property value. `null` if the property isn't set or doesn't exist. * - * {@see hasAttribute()} + * @see hasProperty() */ - public function getAttribute(string $name): mixed; + public function get(string $propertyName): mixed; /** - * Returns attribute values. + * Returns property values. * - * @param array|null $names List of attributes whose value needs to be returned. Defaults to null, meaning all - * attributes listed in {@see attributes()} will be returned. - * If it's an array, only the attributes in the array will be returned. - * @param array $except List of attributes whose value shouldn't be returned. + * @param array|null $names List of property names whose value needs to be returned. Defaults to `null`, meaning all + * properties listed in {@see propertyNames()} will be returned. + * @param array $except List of property names whose value shouldn't be returned. * - * @throws InvalidConfigException * @throws Exception + * @throws InvalidConfigException * - * @return array Attribute values (name => value). + * @return array Property values (name => value). */ - public function getAttributes(array|null $names = null, array $except = []): array; + public function propertyValues(array|null $names = null, array $except = []): array; /** * Returns a value indicating whether the current record is new (not saved in the database). @@ -160,15 +155,15 @@ public function getIsNewRecord(): bool; * This refers to the primary key value that's populated into the record after executing a find method (for example, * `findOne()`). * - * The value remains unchanged even if the primary key attribute is manually assigned with a different value. + * The value remains unchanged even if the primary key property is manually assigned with a different value. * * @param bool $asArray Whether to return the primary key value as an array. If true, the return value will be an - * array with column name as key and column value as value. If this is `false` (default), a scalar value will be + * array with property name as key and property value as value. If this is `false` (default), a scalar value will be * returned for a non-composite primary key. * - * @return mixed The old primary key value. An array (column name => column value) is returned if the primary key - * is composite or `$asArray` is true. A string is returned otherwise (`null` will be returned if the key value is - * `null`). + * @return mixed The old primary key value. An array (property name => property value) is returned if the primary + * key is composite or `$asArray` is true. A string is returned otherwise (`null` will be returned if the key value + * is `null`). * * @psalm-return ( * $asArray is true @@ -182,10 +177,10 @@ public function getOldPrimaryKey(bool $asArray = false): mixed; * Returns the primary key value(s). * * @param bool $asArray Whether to return the primary key value as an array. If true, the return value will be an - * array with attribute names as keys and attribute values as values. Note that for composite primary keys, an array + * array with property names as keys and property values as values. Note that for composite primary keys, an array * will always be returned regardless of this parameter value. * - * @return mixed The primary key value. An array (attribute name => attribute value) is returned if the primary key + * @return mixed The primary key value. An array (property name => property value) is returned if the primary key * is composite or `$asArray` is true. A string is returned otherwise (`null` will be returned if the key value is * `null`). * @@ -214,18 +209,16 @@ public function getPrimaryKey(bool $asArray = false): mixed; public function getTableName(): string; /** - * Returns a value indicating whether the record has an attribute with the specified name. - * - * @param string $name The name of the attribute. + * Returns a value indicating whether the record has a property with the specified name. * - * @return bool Whether the record has an attribute with the specified name. + * @param string $name The name of the property. */ - public function hasAttribute(string $name): bool; + public function hasProperty(string $name): bool; /** - * Inserts a row into the associated database table using the attribute values of this record. + * Inserts a row into the associated database table using the property values of this record. * - * Only the {@see dirtyAttributes|changed attribute values} will be inserted into a database. + * Only the {@see dirtyValues() changed property values} will be inserted into a database. * * If the table's primary key is auto incremental and is `null` during insertion, it will be populated with the * actual value after insertion. @@ -239,30 +232,30 @@ public function hasAttribute(string $name): bool; * $customer->insert(); * ``` * - * @param array|null $attributes List of attributes that need to be saved. Defaults to `null`, meaning all - * attributes that are loaded from DB will be saved. + * @param array|null $propertyNames List of property names that need to be saved. Defaults to `null`, meaning all + * changed property values will be saved. * * @throws InvalidConfigException * @throws Throwable In case insert failed. * - * @return bool Whether the attributes are valid and the record is inserted successfully. + * @return bool Whether the record is inserted successfully. */ - public function insert(array $attributes = null): bool; + public function insert(array|null $propertyNames = null): bool; /** - * Returns a value indicating whether the given set of attributes represents the primary key for this active record. + * Returns a value indicating whether the given set of property names represents the primary key for this active + * record. * - * @param array $keys The set of attributes to check. + * @param array $keys The set of property names to check. * - * @return bool whether The given set of attributes represents the primary key for this active record. + * @return bool whether The given set of property names represents the primary key for this active record. */ public function isPrimaryKey(array $keys): bool; /** * Check whether the named relation has been populated with records. * - * @param string $name The relation name, for example, `orders` for a relation defined via `getOrders()` method - * (case-sensitive). + * @param string $name The relation name, for example, `orders` (case-sensitive). * * @return bool Whether relation has been populated with records. * @@ -283,22 +276,20 @@ public function isRelationPopulated(string $name): bool; * * This method requires that the primary key value isn't `null`. * - * @param string $name The case-sensitive name of the relationship, for example, `orders` for a relation defined via - * `getOrders()` method. + * @param string $relationName The relation name, for example, `orders` (case-sensitive). * @param self $arClass The record to be linked with the current one. * @param array $extraColumns More column values to be saved into the junction table. This parameter is only * meaningful for a relationship involving a junction table (that's a relation set with * {@see ActiveQueryInterface::via()}). */ - public function link(string $name, self $arClass, array $extraColumns = []): void; + public function link(string $relationName, self $arClass, array $extraColumns = []): void; /** * Populates the named relation with the related records. * * Note that this method doesn't check if the relation exists or not. * - * @param string $name The relation name, for example, `orders` for a relation defined via `getOrders()` method - * (case-sensitive). + * @param string $name The relation name, for example, `orders` (case-sensitive). * @param array|self|null $records The related records to be populated into the relation. */ public function populateRelation(string $name, array|self|null $records): void; @@ -309,8 +300,8 @@ public function populateRelation(string $name, array|self|null $records): void; * The default implementation will return the primary key(s) as declared in the DB table that's associated with * this AR class. * - * If the DB table doesn't declare any primary key, you should override this method to return the attributes that - * you want to use as primary keys for this AR class. + * If the DB table doesn't declare any primary key, you should override this method to return the property names + * that you want to use as primary keys for this active record class. * * Note that an array should be returned even for a table with a single primary key. * @@ -379,26 +370,26 @@ public function resetRelation(string $name): void; * $customer->save(); * ``` * - * @param array|null $attributeNames List of attribute names that need to be saved. Defaults to `null`, - * meaning all attributes that are loaded from DB will be saved. + * @param array|null $propertyNames List of property names that need to be saved. Defaults to `null`, + * meaning all changed property values will be saved. * * @return bool Whether the saving succeeded (that's no validation errors occurred). */ - public function save(array $attributeNames = null): bool; + public function save(array|null $propertyNames = null): bool; /** - * Sets the named attribute value. + * Sets the named property value. * - * @param string $name The attribute name. + * @param string $propertyName The property name. * - * @throws InvalidArgumentException If the named attribute doesn't exist. + * @throws InvalidArgumentException If the named property doesn't exist. */ - public function setAttribute(string $name, mixed $value): void; + public function set(string $propertyName, mixed $value): void; /** * Saves the changes to this active record into the associated database table. * - * Only the {@see dirtyAttributes|changed attribute values} will be saved into a database. + * Only the {@see dirtyValues() changed property values} will be saved into a database. * * For example, to update a customer record: * @@ -421,19 +412,19 @@ public function setAttribute(string $name, mixed $value): void; * } * ``` * - * @param array|null $attributeNames List of attributes that need to be saved. Defaults to `null`, meaning all - * attributes that are loaded from DB will be saved. + * @param array|null $propertyNames List of property names that need to be saved. Defaults to `null`, meaning all + * changed property values will be saved. * - * @throws StaleObjectException If {@see optimisticLock|optimistic locking} is enabled and the data being updated is + * @throws StaleObjectException If {@see optimisticLock() optimistic locking} is enabled and the data being updated is * outdated. * @throws Throwable In case update failed. * * @return int The number of rows affected. */ - public function update(array $attributeNames = null): int; + public function update(array|null $propertyNames = null): int; /** - * Updates the whole table using the provided attribute values and conditions. + * Updates the whole table using the provided property values and conditions. * * For example, to change the status to be 1 for all customers whose status is 2: * @@ -455,7 +446,7 @@ public function update(array $attributeNames = null): int; * * For a large set of models you might consider using {@see ActiveQuery::each()} to keep memory usage within limits. * - * @param array $attributes Attribute values (name-value pairs) to be saved into the table. + * @param array $propertyValues Property values (name-value pairs) to be saved into the table. * @param array|string $condition The conditions that will be put in the `WHERE` part of the `UPDATE` SQL. * Please refer to {@see Query::where()} on how to specify this parameter. * @param array $params The parameters (name => value) to be bound to the query. @@ -466,29 +457,28 @@ public function update(array $attributeNames = null): int; * * @return int The number of rows updated. */ - public function updateAll(array $attributes, array|string $condition = [], array $params = []): int; + public function updateAll(array $propertyValues, array|string $condition = [], array $params = []): int; /** - * Updates the specified attributes. + * Updates the specified properties. * - * This method is a shortcut to {@see update()} when data validation isn't necessary and only a small set attributes - * need to be updated. + * This method is a shortcut to {@see update()} when only a small set of properties need to be updated. * - * You may specify the attributes to be updated as name list or name-value pairs. - * If the latter, the corresponding attribute values will be modified so. + * You may specify the properties to be updated as name list or name-value pairs. + * If the latter, the corresponding property values will be modified so. * - * The method will then save the specified attributes into a database. + * The method will then save the specified properties into a database. * * Note that this method will **not** perform data validation and will **not** trigger events. * - * @param array $attributes The attributes (names or name-value pairs) to be updated. + * @param array $properties The properties (names or name-value pairs) to be updated. * * @throws Exception * @throws NotSupportedException * * @return int The number of rows affected. */ - public function updateAttributes(array $attributes): int; + public function updateProperties(array $properties): int; /** * Destroys the relationship between two records. @@ -497,21 +487,20 @@ public function updateAttributes(array $attributes): int; * * Otherwise, the foreign key will be set `null` and the record will be saved without validation. * - * @param string $name The case-sensitive name of the relationship, for example, `orders` for a relation defined via - * `getOrders()` method. + * @param string $relationName The relation name, for example, `orders` (case-sensitive). * @param self $arClass The active record to be unlinked from the current one. * @param bool $delete Whether to delete the active record that contains the foreign key. * If false, the active record's foreign key will be set `null` and saved. * If true, the active record containing the foreign key will be deleted. */ - public function unlink(string $name, self $arClass, bool $delete = false): void; + public function unlink(string $relationName, self $arClass, bool $delete = false): void; /** - * Returns the old attribute values. + * Returns the old property values. * - * @return array The old attribute values (name-value pairs). + * @return array The old property values (name-value pairs). */ - public function getOldAttributes(): array; + public function oldValues(): array; /** * Populates an active record object using a row of data from the database/storage. @@ -520,7 +509,7 @@ public function getOldAttributes(): array; * database. * It's mainly used by {@see ActiveQuery} to populate the query results into active records. * - * @param array|object $row Attribute values (name => value). + * @param array|object $row Property values (name => value). * * @throws Exception * @throws InvalidConfigException diff --git a/src/ActiveRelationTrait.php b/src/ActiveRelationTrait.php index 12bf4bf83..93699662f 100644 --- a/src/ActiveRelationTrait.php +++ b/src/ActiveRelationTrait.php @@ -469,32 +469,29 @@ private function indexBuckets(array $buckets, Closure|string $indexBy): array } /** - * @param array $attributes the attributes to prefix. + * @param array $columnNames The column names to prefix. * * @throws \Yiisoft\Definitions\Exception\InvalidConfigException */ - private function prefixKeyColumns(array $attributes): array + private function prefixKeyColumns(array $columnNames): array { if (!empty($this->join) || !empty($this->joinWith)) { if (empty($this->from)) { $alias = $this->getARInstance()->getTableName(); } else { - foreach ($this->from as $alias => $table) { - if (!is_string($alias)) { - $alias = $table; - } - break; + $alias = array_key_first($this->from); + + if (!is_string($alias)) { + $alias = reset($this->from); } } - if (isset($alias)) { - foreach ($attributes as $i => $attribute) { - $attributes[$i] = "$alias.$attribute"; - } + foreach ($columnNames as $i => $columnName) { + $columnNames[$i] = "$alias.$columnName"; } } - return $attributes; + return $columnNames; } /** @@ -502,19 +499,19 @@ private function prefixKeyColumns(array $attributes): array */ protected function filterByModels(array $models): void { - $attributes = array_keys($this->link); - $attributes = $this->prefixKeyColumns($attributes); + $properties = array_keys($this->link); + $columnNames = $this->prefixKeyColumns($properties); $model = reset($models); $values = []; - if (count($attributes) === 1) { + if (count($columnNames) === 1) { /** single key */ - $linkedAttribute = reset($this->link); + $linkedProperty = reset($this->link); if ($model instanceof ActiveRecordInterface) { foreach ($models as $model) { - $value = $model->getAttribute($linkedAttribute); + $value = $model->get($linkedProperty); if ($value !== null) { if (is_array($value)) { @@ -526,8 +523,8 @@ protected function filterByModels(array $models): void } } else { foreach ($models as $model) { - if (isset($model[$linkedAttribute])) { - $value = $model[$linkedAttribute]; + if (isset($model[$linkedProperty])) { + $value = $model[$linkedProperty]; if (is_array($value)) { $values = [...$values, ...$value]; @@ -550,14 +547,14 @@ protected function filterByModels(array $models): void $scalarValues = array_unique($scalarValues); $values = [...$scalarValues, ...$nonScalarValues]; - $attribute = reset($attributes); - /** @var string $columnName */ - $columnName = array_key_first($this->link); + $columnName = reset($columnNames); + /** @var string $propertyName */ + $propertyName = array_key_first($this->link); - match ($this->getARInstance()->columnType($columnName)) { - 'array' => $this->andWhere(new ArrayOverlapsCondition($attribute, $values)), - SchemaInterface::TYPE_JSON => $this->andWhere(new JsonOverlapsCondition($attribute, $values)), - default => $this->andWhere(new InCondition($attribute, 'IN', $values)), + match ($this->getARInstance()->columnType($propertyName)) { + 'array' => $this->andWhere(new ArrayOverlapsCondition($columnName, $values)), + SchemaInterface::TYPE_JSON => $this->andWhere(new JsonOverlapsCondition($columnName, $values)), + default => $this->andWhere(new InCondition($columnName, 'IN', $values)), }; return; @@ -567,10 +564,10 @@ protected function filterByModels(array $models): void if ($model instanceof ActiveRecordInterface) { foreach ($models as $model) { - $value = $model->getAttributes($this->link); + $value = $model->propertyValues($this->link); if (!empty($value)) { - $values[] = array_combine($attributes, array_merge($nulls, $value)); + $values[] = array_combine($columnNames, array_merge($nulls, $value)); } } } else { @@ -578,7 +575,7 @@ protected function filterByModels(array $models): void $value = array_intersect_key($model, $nulls); if (!empty($value)) { - $values[] = array_combine($attributes, array_merge($nulls, $value)); + $values[] = array_combine($columnNames, array_merge($nulls, $value)); } } } @@ -589,24 +586,24 @@ protected function filterByModels(array $models): void return; } - $this->andWhere(new InCondition($attributes, 'IN', $values)); + $this->andWhere(new InCondition($columnNames, 'IN', $values)); } - private function getModelKeys(ActiveRecordInterface|array $activeRecord, array $attributes): array + private function getModelKeys(ActiveRecordInterface|array $model, array $properties): array { $key = []; - if (is_array($activeRecord)) { - foreach ($attributes as $attribute) { - if (isset($activeRecord[$attribute])) { - $key[] = is_array($activeRecord[$attribute]) - ? $activeRecord[$attribute] - : (string) $activeRecord[$attribute]; + if (is_array($model)) { + foreach ($properties as $property) { + if (isset($model[$property])) { + $key[] = is_array($model[$property]) + ? $model[$property] + : (string) $model[$property]; } } } else { - foreach ($attributes as $attribute) { - $value = $activeRecord->getAttribute($attribute); + foreach ($properties as $property) { + $value = $model->get($property); if ($value !== null) { $key[] = is_array($value) diff --git a/src/ArArrayHelper.php b/src/ArArrayHelper.php index d1b8246e8..7b7559922 100644 --- a/src/ArArrayHelper.php +++ b/src/ArArrayHelper.php @@ -94,8 +94,8 @@ public static function getColumn(array $array, string $name): array public static function getValueByPath(ActiveRecordInterface|array $array, string $key, mixed $default = null): mixed { if ($array instanceof ActiveRecordInterface) { - if ($array->hasAttribute($key)) { - return $array->getAttribute($key); + if ($array->hasProperty($key)) { + return $array->get($key); } if (property_exists($array, $key)) { @@ -180,7 +180,7 @@ public static function toArray(array|object $object): array } if ($object instanceof ActiveRecordInterface) { - return $object->getAttributes(); + return $object->propertyValues(); } if ($object instanceof Traversable) { diff --git a/src/Trait/ArrayAccessTrait.php b/src/Trait/ArrayAccessTrait.php index 0bf495960..9e9fef60d 100644 --- a/src/Trait/ArrayAccessTrait.php +++ b/src/Trait/ArrayAccessTrait.php @@ -7,21 +7,20 @@ use InvalidArgumentException; use Yiisoft\ActiveRecord\ActiveRecordInterface; -use function get_object_vars; use function is_array; use function property_exists; /** * Trait to implement {@see ArrayAccess} interface for ActiveRecord. * - * @method mixed getAttribute(string $name) - * @see ActiveRecordInterface::getAttribute() + * @method mixed get(string $name) + * @see ActiveRecordInterface::get() * - * @method bool hasAttribute(string $name) - * @see ActiveRecordInterface::hasAttribute() + * @method bool hasProperty(string $name) + * @see ActiveRecordInterface::hasProperty() * - * @method void setAttribute(string $name, mixed $value) - * @see ActiveRecordInterface::getAttribute() + * @method void set(string $name, mixed $value) + * @see ActiveRecordInterface::set() * * @method ActiveRecordInterface|array|null relation(string $name) * @see ActiveRecordInterface::relation() @@ -50,12 +49,12 @@ trait ArrayAccessTrait */ public function offsetExists(mixed $offset): bool { - if ($this->hasAttribute($offset)) { - return $this->getAttribute($offset) !== null; + if ($this->hasProperty($offset)) { + return $this->get($offset) !== null; } if (property_exists($this, $offset)) { - return isset(get_object_vars($this)[$offset]); + return isset($this->$offset); } if ($this->isRelationPopulated($offset)) { @@ -70,12 +69,12 @@ public function offsetExists(mixed $offset): bool */ public function offsetGet(mixed $offset): mixed { - if ($this->hasAttribute($offset)) { - return $this->getAttribute($offset); + if ($this->hasProperty($offset)) { + return $this->get($offset); } if (property_exists($this, $offset)) { - return get_object_vars($this)[$offset] ?? null; + return $this->$offset ?? null; } return $this->relation($offset); @@ -92,8 +91,8 @@ public function offsetGet(mixed $offset): mixed */ public function offsetSet(mixed $offset, mixed $value): void { - if ($this->hasAttribute($offset)) { - $this->setAttribute($offset, $value); + if ($this->hasProperty($offset)) { + $this->set($offset, $value); return; } @@ -121,13 +120,9 @@ public function offsetSet(mixed $offset, mixed $value): void */ public function offsetUnset(mixed $offset): void { - if ($this->hasAttribute($offset)) { - $this->setAttribute($offset, null); - return; - } - - if (property_exists($this, $offset)) { - $this->$offset = null; + if ($this->hasProperty($offset) || property_exists($this, $offset)) { + $this->set($offset, null); + unset($this->$offset); return; } diff --git a/src/Trait/ArrayIteratorTrait.php b/src/Trait/ArrayIteratorTrait.php index 0b0f5deb2..35c6ff382 100644 --- a/src/Trait/ArrayIteratorTrait.php +++ b/src/Trait/ArrayIteratorTrait.php @@ -10,13 +10,13 @@ /** * Trait to implement {@see IteratorAggregate} interface for ActiveRecord. * - * @method array getAttributes(array|null $names = null, array $except = []) - * @see ActiveRecordInterface::getAttributes() + * @method array propertyValues(array|null $names = null, array $except = []) + * @see ActiveRecordInterface::propertyValues() */ trait ArrayIteratorTrait { /** - * Returns an iterator for traversing the attributes in the ActiveRecord. + * Returns an iterator for traversing the properties in the ActiveRecord. * * This method is required by the interface {@see IteratorAggregate}. * @@ -24,8 +24,8 @@ trait ArrayIteratorTrait */ public function getIterator(): ArrayIterator { - $attributes = $this->getAttributes(); + $values = $this->propertyValues(); - return new ArrayIterator($attributes); + return new ArrayIterator($values); } } diff --git a/src/Trait/ArrayableTrait.php b/src/Trait/ArrayableTrait.php index d686338a9..e9a5cd79f 100644 --- a/src/Trait/ArrayableTrait.php +++ b/src/Trait/ArrayableTrait.php @@ -14,8 +14,8 @@ /** * Trait to implement {@see \Yiisoft\Arrays\ArrayableInterface} interface for ActiveRecord. * - * @method string[] attributes() - * @see ActiveRecordInterface::attributes() + * @method string[] propertyNames() + * @see ActiveRecordInterface::propertyNames() * * @method array getRelatedRecords() * @see AbstractActiveRecord::getRelatedRecords() @@ -40,7 +40,7 @@ public function extraFields(): array */ public function fields(): array { - $fields = $this->attributes(); + $fields = $this->propertyNames(); return array_combine($fields, $fields); } diff --git a/src/Trait/MagicPropertiesTrait.php b/src/Trait/MagicPropertiesTrait.php index 62e7b26e8..a953dbdec 100644 --- a/src/Trait/MagicPropertiesTrait.php +++ b/src/Trait/MagicPropertiesTrait.php @@ -15,7 +15,6 @@ use Yiisoft\Db\Exception\UnknownPropertyException; use function array_merge; -use function get_object_vars; use function in_array; use function method_exists; use function property_exists; @@ -24,22 +23,16 @@ /** * Trait to define magic methods to access values of an ActiveRecord instance. * - * @method array getOldAttributes() - * @see AbstractActiveRecord::getOldAttributes() - * - * @method mixed getOldAttribute(string $name) - * @see AbstractActiveRecord::getOldAttribute() - * * @method array getRelatedRecords() * @see AbstractActiveRecord::getRelatedRecords() * - * @method bool hasDependentRelations(string $attribute) + * @method bool hasDependentRelations(string $propertyName) * @see AbstractActiveRecord::hasDependentRelations() * * @method bool isRelationPopulated(string $name) * @see ActiveRecordInterface::isRelationPopulated() * - * @method void resetDependentRelations(string $attribute) + * @method void resetDependentRelations(string $propertyName) * @see AbstractActiveRecord::resetDependentRelations() * * @method void resetRelation(string $name) @@ -50,28 +43,27 @@ */ trait MagicPropertiesTrait { - /** @psalm-var array $attributes */ - private array $attributes = []; + /** @psalm-var array $propertyValues */ + private array $propertyValues = []; /** * PHP getter magic method. + * This method is overridden so that values and related objects can be accessed like properties. * - * This method is overridden so that attributes and related objects can be accessed like properties. - * - * @param string $name Property name. + * @param string $name Property or relation name. * * @throws InvalidArgumentException|InvalidCallException|InvalidConfigException|ReflectionException|Throwable * @throws UnknownPropertyException * * @throws Exception - * @return mixed Property value. + * @return mixed Property or relation value. * - * {@see getAttribute()} + * @see get() */ public function __get(string $name) { - if ($this->hasAttribute($name)) { - return $this->getAttribute($name); + if ($this->hasProperty($name)) { + return $this->get($name); } if ($this->isRelationPopulated($name)) { @@ -92,17 +84,14 @@ public function __get(string $name) throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name); } - throw new UnknownPropertyException('Getting unknown property: ' . static::class . '::' . $name); + throw new UnknownPropertyException('Getting unknown property or relation: ' . static::class . '::' . $name); } /** - * Checks if a property value is null. + * PHP isset magic method. + * Checks if a property or relation exists and its value is not `null`. * - * This method overrides the parent implementation by checking if the named attribute is `null` or not. - * - * @param string $name The property name or the event name. - * - * @return bool Whether the property value is null. + * @param string $name The property or relation name. */ public function __isset(string $name): bool { @@ -114,20 +103,15 @@ public function __isset(string $name): bool } /** - * Sets a component property to be null. + * PHP unset magic method. + * Unsets the property or relation. * - * This method overrides the parent implementation by clearing the specified attribute value. - * - * @param string $name The property name or the event name. + * @param string $name The property or relation name. */ public function __unset(string $name): void { - if ($this->hasAttribute($name)) { - unset($this->attributes[$name]); - - if ($name !== 'attributes' && isset(get_object_vars($this)[$name])) { - $this->$name = null; - } + if ($this->hasProperty($name)) { + unset($this->propertyValues[$name]); if ($this->hasDependentRelations($name)) { $this->resetDependentRelations($name); @@ -139,8 +123,7 @@ public function __unset(string $name): void /** * PHP setter magic method. - * - * This method is overridden so that AR attributes can be accessed like properties. + * Sets the value of a property. * * @param string $name Property name. * @@ -148,8 +131,8 @@ public function __unset(string $name): void */ public function __set(string $name, mixed $value): void { - if ($this->hasAttribute($name)) { - $this->setAttributeInternal($name, $value); + if ($this->hasProperty($name)) { + parent::set($name, $value); return; } @@ -168,17 +151,17 @@ public function __set(string $name, mixed $value): void throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name); } - public function hasAttribute(string $name): bool + public function hasProperty(string $name): bool { - return isset($this->attributes[$name]) || in_array($name, $this->attributes(), true); + return isset($this->propertyValues[$name]) || in_array($name, $this->propertyNames(), true); } - public function setAttribute(string $name, mixed $value): void + public function set(string $propertyName, mixed $value): void { - if ($this->hasAttribute($name)) { - $this->setAttributeInternal($name, $value); + if ($this->hasProperty($propertyName)) { + parent::set($propertyName, $value); } else { - throw new InvalidArgumentException(static::class . ' has no attribute named "' . $name . '".'); + throw new InvalidArgumentException(static::class . ' has no property named "' . $propertyName . '".'); } } @@ -199,13 +182,13 @@ public function setAttribute(string $name, mixed $value): void * {@see canGetProperty()} * {@see canSetProperty()} */ - public function hasProperty(string $name, bool $checkVars = true): bool + public function isProperty(string $name, bool $checkVars = true): bool { return method_exists($this, 'get' . ucfirst($name)) || method_exists($this, 'set' . ucfirst($name)) || method_exists($this, 'get' . ucfirst($name) . 'Query') || ($checkVars && property_exists($this, $name)) - || $this->hasAttribute($name); + || $this->hasProperty($name); } public function canGetProperty(string $name, bool $checkVars = true): bool @@ -213,39 +196,28 @@ public function canGetProperty(string $name, bool $checkVars = true): bool return method_exists($this, 'get' . ucfirst($name)) || method_exists($this, 'get' . ucfirst($name) . 'Query') || ($checkVars && property_exists($this, $name)) - || $this->hasAttribute($name); + || $this->hasProperty($name); } public function canSetProperty(string $name, bool $checkVars = true): bool { return method_exists($this, 'set' . ucfirst($name)) || ($checkVars && property_exists($this, $name)) - || $this->hasAttribute($name); + || $this->hasProperty($name); } /** @psalm-return array */ - protected function getAttributesInternal(): array + protected function propertyValuesInternal(): array { - return array_merge($this->attributes, parent::getAttributesInternal()); + return array_merge($this->propertyValues, parent::propertyValuesInternal()); } - protected function populateAttribute(string $name, mixed $value): void + protected function populateProperty(string $name, mixed $value): void { - if ($name !== 'attributes' && property_exists($this, $name)) { + if ($name !== 'propertyValues' && property_exists($this, $name)) { $this->$name = $value; } else { - $this->attributes[$name] = $value; - } - } - - private function setAttributeInternal(string $name, mixed $value): void - { - if ($this->hasDependentRelations($name) - && ($value === null || $this->getAttribute($name) !== $value) - ) { - $this->resetDependentRelations($name); + $this->propertyValues[$name] = $value; } - - $this->populateAttribute($name, $value); } } diff --git a/src/Trait/TransactionalTrait.php b/src/Trait/TransactionalTrait.php index bec8bf17d..828cb4548 100644 --- a/src/Trait/TransactionalTrait.php +++ b/src/Trait/TransactionalTrait.php @@ -38,16 +38,16 @@ public function delete(): int } } - public function insert(array $attributes = null): bool + public function insert(array|null $propertyNames = null): bool { if (!$this->isTransactional(TransactionalInterface::OP_INSERT)) { - return $this->insertInternal($attributes); + return $this->insertInternal($propertyNames); } $transaction = $this->db()->beginTransaction(); try { - $result = $this->insertInternal($attributes); + $result = $this->insertInternal($propertyNames); if ($result === false) { $transaction->rollBack(); } else { @@ -75,16 +75,16 @@ public function transactions(): array ]; } - public function update(array $attributeNames = null): int + public function update(array|null $propertyNames = null): int { if (!$this->isTransactional(TransactionalInterface::OP_UPDATE)) { - return $this->updateInternal($attributeNames); + return $this->updateInternal($propertyNames); } $transaction = $this->db()->beginTransaction(); try { - $result = $this->updateInternal($attributeNames); + $result = $this->updateInternal($propertyNames); if ($result === 0) { $transaction->rollBack(); } else { diff --git a/tests/ActiveQueryFindTest.php b/tests/ActiveQueryFindTest.php index 1f07510e7..2ab515bf4 100644 --- a/tests/ActiveQueryFindTest.php +++ b/tests/ActiveQueryFindTest.php @@ -80,7 +80,7 @@ public function testFindBySql(): void /** find one() */ $customers = $customerQuery->findBySql('SELECT * FROM {{customer}} ORDER BY [[id]] DESC')->one(); $this->assertInstanceOf(Customer::class, $customers); - $this->assertEquals('user3', $customers->getAttribute('name')); + $this->assertEquals('user3', $customers->get('name')); /** find all() */ $customers = $customerQuery->findBySql('SELECT * FROM {{customer}}')->all(); @@ -91,7 +91,7 @@ public function testFindBySql(): void ->findBySql('SELECT * FROM {{customer}} WHERE [[id]]=:id', [':id' => 2]) ->one(); $this->assertInstanceOf(Customer::class, $customers); - $this->assertEquals('user2', $customers->getAttribute('name')); + $this->assertEquals('user2', $customers->get('name')); } public function testFindLazyViaTable(): void @@ -102,7 +102,7 @@ public function testFindLazyViaTable(): void $orders = $orderQuery->findOne(2); $this->assertCount(0, $orders->getBooks()); - $this->assertEquals(2, $orders->getAttribute('id')); + $this->assertEquals(2, $orders->get('id')); $orders = $orderQuery->where(['id' => 1])->asArray()->one(); $this->assertIsArray($orders); @@ -118,18 +118,18 @@ public function testFindEagerViaTable(): void $order = $orders[0]; $this->assertCount(2, $order->getBooks()); - $this->assertEquals(1, $order->getAttribute('id')); - $this->assertEquals(1, $order->getBooks()[0]->getAttribute('id')); - $this->assertEquals(2, $order->getBooks()[1]->getAttribute('id')); + $this->assertEquals(1, $order->get('id')); + $this->assertEquals(1, $order->getBooks()[0]->get('id')); + $this->assertEquals(2, $order->getBooks()[1]->get('id')); $order = $orders[1]; $this->assertCount(0, $order->getBooks()); - $this->assertEquals(2, $order->getAttribute('id')); + $this->assertEquals(2, $order->get('id')); $order = $orders[2]; $this->assertCount(1, $order->getBooks()); - $this->assertEquals(3, $order->getAttribute('id')); - $this->assertEquals(2, $order->getBooks()[0]->getAttribute('id')); + $this->assertEquals(3, $order->get('id')); + $this->assertEquals(2, $order->getBooks()[0]->get('id')); /** https://github.com/yiisoft/yii2/issues/1402 */ $orderQuery = new ActiveQuery(Order::class); @@ -252,7 +252,7 @@ public function testFind(): void $customer = $customerQuery->findOne(['name' => 'user5']); $this->assertNull($customer); - /** find by attributes */ + /** find by column */ $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->where(['name' => 'user2'])->one(); $this->assertInstanceOf(Customer::class, $customer); diff --git a/tests/ActiveQueryTest.php b/tests/ActiveQueryTest.php index d74395ab2..ee9691525 100644 --- a/tests/ActiveQueryTest.php +++ b/tests/ActiveQueryTest.php @@ -372,7 +372,7 @@ public function testCustomColumns(): void ->where(['name' => 'user3'])->one(); } - $this->assertEquals(3, $customers->getAttribute('id')); + $this->assertEquals(3, $customers->get('id')); $this->assertEquals(4, $customers->status2); } @@ -420,8 +420,8 @@ public function testDeeplyNestedTableRelation(): void $items = $customers->getOrderItems(); $this->assertCount(2, $items); - $this->assertEquals(1, $items[0]->getAttribute('id')); - $this->assertEquals(2, $items[1]->getAttribute('id')); + $this->assertEquals(1, $items[0]->get('id')); + $this->assertEquals(2, $items[1]->get('id')); $this->assertInstanceOf(Item::class, $items[0]); $this->assertInstanceOf(Item::class, $items[1]); } @@ -446,7 +446,7 @@ public function testDeeplyNestedTableRelation2(): void $this->assertInstanceOf(Order::class, $orders[0]); $this->assertInstanceOf(Order::class, $orders[1]); - $ids = [$orders[0]->getId(), $orders[1]->getAttribute('id')]; + $ids = [$orders[0]->getId(), $orders[1]->get('id')]; sort($ids); $this->assertEquals([1, 3], $ids); @@ -455,7 +455,7 @@ public function testDeeplyNestedTableRelation2(): void $orders = $categories->getOrders(); $this->assertCount(1, $orders); - $this->assertEquals(2, $orders[0]->getAttribute('id')); + $this->assertEquals(2, $orders[0]->get('id')); $this->assertInstanceOf(Order::class, $orders[0]); } @@ -1899,9 +1899,9 @@ public function testOutdatedRelationsAreResetForExistingRecords(): void $this->assertEquals(2, $orderItems->getOrder()->getId()); $this->assertEquals(1, $orderItems->getItem()->getId()); - /** Test `setAttribute()`. */ - $orderItems->setAttribute('order_id', 3); - $orderItems->setAttribute('item_id', 1); + /** Test `set()`. */ + $orderItems->set('order_id', 3); + $orderItems->set('item_id', 1); $this->assertEquals(3, $orderItems->getOrder()->getId()); $this->assertEquals(1, $orderItems->getItem()->getId()); } @@ -2059,14 +2059,14 @@ public function testBit(): void $this->assertTrue($trueBit->val); } - public function testUpdateAttributes(): void + public function testUpdateProperties(): void { $this->checkFixture($this->db(), 'order'); $orderQuery = new ActiveQuery(Order::class); $order = $orderQuery->findOne(1); $newTotal = 978; - $this->assertSame(1, $order->updateAttributes(['total' => $newTotal])); + $this->assertSame(1, $order->updateProperties(['total' => $newTotal])); $this->assertEquals($newTotal, $order->getTotal()); $order = $orderQuery->findOne(1); @@ -2077,7 +2077,7 @@ public function testUpdateAttributes(): void $this->assertTrue($newOrder->getIsNewRecord()); $newTotal = 200; - $this->assertSame(0, $newOrder->updateAttributes(['total' => $newTotal])); + $this->assertSame(0, $newOrder->updateProperties(['total' => $newTotal])); $this->assertTrue($newOrder->getIsNewRecord()); $this->assertEquals($newTotal, $newOrder->getTotal()); } @@ -2113,156 +2113,153 @@ public function testCustomARRelation(): void $this->assertInstanceOf(Order::class, $orderItem->getCustom()); } - public function testGetAttributes(): void + public function testPropertyValues(): void { - $attributesExpected = []; $this->checkFixture($this->db(), 'customer'); - $attributesExpected['id'] = 1; - $attributesExpected['email'] = 'user1@example.com'; - $attributesExpected['name'] = 'user1'; - $attributesExpected['address'] = 'address1'; - $attributesExpected['status'] = 1; - $attributesExpected['bool_status'] = true; - $attributesExpected['profile_id'] = 1; + $expectedValues = [ + 'id' => 1, + 'email' => 'user1@example.com', + 'name' => 'user1', + 'address' => 'address1', + 'status' => 1, + 'bool_status' => true, + 'profile_id' => 1, + ]; $customer = new ActiveQuery(Customer::class); - $attributes = $customer->findOne(1)->getAttributes(); + $values = $customer->findOne(1)->propertyValues(); - $this->assertEquals($attributes, $attributesExpected); + $this->assertEquals($expectedValues, $values); } - public function testGetAttributesOnly(): void + public function testPropertyValuesOnly(): void { $this->checkFixture($this->db(), 'customer'); $customer = new ActiveQuery(Customer::class); - $attributes = $customer->findOne(1)->getAttributes(['id', 'email', 'name']); + $values = $customer->findOne(1)->propertyValues(['id', 'email', 'name']); - $this->assertEquals(['id' => 1, 'email' => 'user1@example.com', 'name' => 'user1'], $attributes); + $this->assertEquals(['id' => 1, 'email' => 'user1@example.com', 'name' => 'user1'], $values); } - public function testGetAttributesExcept(): void + public function testPropertyValuesExcept(): void { $this->checkFixture($this->db(), 'customer'); $customer = new ActiveQuery(Customer::class); - $attributes = $customer->findOne(1)->getAttributes(null, ['status', 'bool_status', 'profile_id']); + $values = $customer->findOne(1)->propertyValues(null, ['status', 'bool_status', 'profile_id']); $this->assertEquals( - $attributes, - ['id' => 1, 'email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1'] + ['id' => 1, 'email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1'], + $values ); } - public function testGetOldAttribute(): void + public function testGetOldValue(): void { $this->checkFixture($this->db(), 'customer'); $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); - $this->assertEquals('user1', $query->getOldAttribute('name')); - $this->assertEquals($query->getAttributes(), $query->getOldAttributes()); + $this->assertEquals('user1', $query->oldValue('name')); + $this->assertEquals($query->propertyValues(), $query->oldValues()); - $query->setAttribute('name', 'samdark'); - $this->assertEquals('samdark', $query->getAttribute('name')); - $this->assertEquals('user1', $query->getOldAttribute('name')); - $this->assertNotEquals($query->getAttribute('name'), $query->getOldAttribute('name')); + $query->set('name', 'samdark'); + $this->assertEquals('samdark', $query->get('name')); + $this->assertEquals('user1', $query->oldValue('name')); + $this->assertNotEquals($query->get('name'), $query->oldValue('name')); } - public function testGetOldAttributes(): void + public function testGetOldValues(): void { - $attributes = []; - $attributesNew = []; $this->checkFixture($this->db(), 'customer'); - $attributes['id'] = 1; - $attributes['email'] = 'user1@example.com'; - $attributes['name'] = 'user1'; - $attributes['address'] = 'address1'; - $attributes['status'] = 1; - $attributes['bool_status'] = true; - $attributes['profile_id'] = 1; + $expectedValues = [ + 'id' => 1, + 'email' => 'user1@example.com', + 'name' => 'user1', + 'address' => 'address1', + 'status' => 1, + 'bool_status' => true, + 'profile_id' => 1, + ]; $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); - $this->assertEquals($query->getAttributes(), $attributes); - $this->assertEquals($query->getAttributes(), $query->getOldAttributes()); + $this->assertEquals($expectedValues, $query->propertyValues()); + $this->assertEquals($query->propertyValues(), $query->oldValues()); + + $query->set('name', 'samdark'); - $query->setAttribute('name', 'samdark'); - $attributesNew['id'] = 1; - $attributesNew['email'] = 'user1@example.com'; - $attributesNew['name'] = 'samdark'; - $attributesNew['address'] = 'address1'; - $attributesNew['status'] = 1; - $attributesNew['bool_status'] = true; - $attributesNew['profile_id'] = 1; + $expectedNewValues = $expectedValues; + $expectedNewValues['name'] = 'samdark'; - $this->assertEquals($attributesNew, $query->getAttributes()); - $this->assertEquals($attributes, $query->getOldAttributes()); - $this->assertNotEquals($query->getAttributes(), $query->getOldAttributes()); + $this->assertEquals($expectedNewValues, $query->propertyValues()); + $this->assertEquals($expectedValues, $query->oldValues()); + $this->assertNotEquals($query->propertyValues(), $query->oldValues()); } - public function testIsAttributeChanged(): void + public function testIsPropertyChanged(): void { $this->checkFixture($this->db(), 'customer'); $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); - $this->assertEquals('user1', $query->getAttribute('name')); - $this->assertEquals('user1', $query->getOldAttribute('name')); + $this->assertEquals('user1', $query->get('name')); + $this->assertEquals('user1', $query->oldValue('name')); - $query->setAttribute('name', 'samdark'); - $this->assertEquals('samdark', $query->getAttribute('name')); - $this->assertEquals('user1', $query->getOldAttribute('name')); - $this->assertNotEquals($query->getAttribute('name'), $query->getOldAttribute('name')); - $this->assertTrue($query->isAttributeChanged('name', true)); + $query->set('name', 'samdark'); + $this->assertEquals('samdark', $query->get('name')); + $this->assertEquals('user1', $query->oldValue('name')); + $this->assertNotEquals($query->get('name'), $query->oldValue('name')); + $this->assertTrue($query->isPropertyChanged('name', true)); } - public function testIsAttributeChangedNotIdentical(): void + public function testIsPropertyChangedNotIdentical(): void { $this->checkFixture($this->db(), 'customer'); $customer = new ActiveQuery(Customer::class); $query = $customer->findOne(1); - $this->assertEquals('user1', $query->getAttribute('name')); - $this->assertEquals('user1', $query->getOldAttribute('name')); + $this->assertEquals('user1', $query->get('name')); + $this->assertEquals('user1', $query->oldValue('name')); - $query->setAttribute('name', 'samdark'); - $this->assertEquals('samdark', $query->getAttribute('name')); - $this->assertEquals('user1', $query->getOldAttribute('name')); - $this->assertNotEquals($query->getAttribute('name'), $query->getOldAttribute('name')); - $this->assertTrue($query->isAttributeChanged('name', false)); + $query->set('name', 'samdark'); + $this->assertEquals('samdark', $query->get('name')); + $this->assertEquals('user1', $query->oldValue('name')); + $this->assertNotEquals($query->get('name'), $query->oldValue('name')); + $this->assertTrue($query->isPropertyChanged('name', false)); } - public function testOldAttributeAfterInsertAndUpdate(): void + public function testOldPropertyAfterInsertAndUpdate(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $customer->setAttributes([ + $customer->populateProperties([ 'email' => 'info@example.com', 'name' => 'Jack', 'address' => '123 Ocean Dr', 'status' => 1, ]); - $this->assertNull($customer->getOldAttribute('name')); + $this->assertNull($customer->oldValue('name')); $this->assertTrue($customer->save()); - $this->assertSame('Jack', $customer->getOldAttribute('name')); + $this->assertSame('Jack', $customer->oldValue('name')); - $customer->setAttribute('name', 'Harry'); + $customer->set('name', 'Harry'); $this->assertTrue($customer->save()); - $this->assertSame('Harry', $customer->getOldAttribute('name')); + $this->assertSame('Harry', $customer->oldValue('name')); } public function testCheckRelationUnknownPropertyException(): void @@ -2467,35 +2464,35 @@ public function testUpdate(): void $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(2); $this->assertInstanceOf(Customer::class, $customer); - $this->assertEquals('user2', $customer->getAttribute('name')); + $this->assertEquals('user2', $customer->get('name')); $this->assertFalse($customer->getIsNewRecord()); - $this->assertEmpty($customer->getDirtyAttributes()); + $this->assertEmpty($customer->dirtyValues()); - $customer->setAttribute('name', 'user2x'); + $customer->set('name', 'user2x'); $customer->save(); - $this->assertEquals('user2x', $customer->getAttribute('name')); + $this->assertEquals('user2x', $customer->get('name')); $this->assertFalse($customer->getIsNewRecord()); $customer2 = $customerQuery->findOne(2); - $this->assertEquals('user2x', $customer2->getAttribute('name')); + $this->assertEquals('user2x', $customer2->get('name')); /** no update */ $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); - $customer->setAttribute('name', 'user1'); + $customer->set('name', 'user1'); $this->assertEquals(0, $customer->update()); /** updateAll */ $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(3); - $this->assertEquals('user3', $customer->getAttribute('name')); + $this->assertEquals('user3', $customer->get('name')); $ret = $customer->updateAll(['name' => 'temp'], ['id' => 3]); $this->assertEquals(1, $ret); $customer = $customerQuery->findOne(3); - $this->assertEquals('temp', $customer->getAttribute('name')); + $this->assertEquals('temp', $customer->get('name')); $ret = $customer->updateAll(['name' => 'tempX']); $this->assertEquals(3, $ret); diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index c411abd04..7a3553b5a 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -46,46 +46,46 @@ public function testStoreNull(): void $record = new NullValues(); - $this->assertNull($record->getAttribute('var1')); - $this->assertNull($record->getAttribute('var2')); - $this->assertNull($record->getAttribute('var3')); - $this->assertNull($record->getAttribute('stringcol')); - - $record->setAttribute('var1', 123); - $record->setAttribute('var2', 456); - $record->setAttribute('var3', 789); - $record->setAttribute('stringcol', 'hello!'); + $this->assertNull($record->get('var1')); + $this->assertNull($record->get('var2')); + $this->assertNull($record->get('var3')); + $this->assertNull($record->get('stringcol')); + + $record->set('var1', 123); + $record->set('var2', 456); + $record->set('var3', 789); + $record->set('stringcol', 'hello!'); $record->save(); $this->assertTrue($record->refresh()); - $this->assertEquals(123, $record->getAttribute('var1')); - $this->assertEquals(456, $record->getAttribute('var2')); - $this->assertEquals(789, $record->getAttribute('var3')); - $this->assertEquals('hello!', $record->getAttribute('stringcol')); - - $record->setAttribute('var1', null); - $record->setAttribute('var2', null); - $record->setAttribute('var3', null); - $record->setAttribute('stringcol', null); + $this->assertEquals(123, $record->get('var1')); + $this->assertEquals(456, $record->get('var2')); + $this->assertEquals(789, $record->get('var3')); + $this->assertEquals('hello!', $record->get('stringcol')); + + $record->set('var1', null); + $record->set('var2', null); + $record->set('var3', null); + $record->set('stringcol', null); $record->save(); $this->assertTrue($record->refresh()); - $this->assertNull($record->getAttribute('var1')); - $this->assertNull($record->getAttribute('var2')); - $this->assertNull($record->getAttribute('var3')); - $this->assertNull($record->getAttribute('>stringcol')); - - $record->setAttribute('var1', 0); - $record->setAttribute('var2', 0); - $record->setAttribute('var3', 0); - $record->setAttribute('stringcol', ''); + $this->assertNull($record->get('var1')); + $this->assertNull($record->get('var2')); + $this->assertNull($record->get('var3')); + $this->assertNull($record->get('>stringcol')); + + $record->set('var1', 0); + $record->set('var2', 0); + $record->set('var3', 0); + $record->set('stringcol', ''); $record->save(); $this->assertTrue($record->refresh()); - $this->assertEquals(0, $record->getAttribute('var1')); - $this->assertEquals(0, $record->getAttribute('var2')); - $this->assertEquals(0, $record->getAttribute('var3')); - $this->assertEquals('', $record->getAttribute('stringcol')); + $this->assertEquals(0, $record->get('var1')); + $this->assertEquals(0, $record->get('var2')); + $this->assertEquals(0, $record->get('var3')); + $this->assertEquals('', $record->get('stringcol')); } public function testStoreEmpty(): void @@ -146,9 +146,9 @@ public function testOutdatedRelationsAreResetForNewRecords(): void $this->assertEquals(2, $orderItem->getOrder()->getId()); $this->assertEquals(1, $orderItem->getItem()->getId()); - /** test `setAttribute()`. */ - $orderItem->setAttribute('order_id', 2); - $orderItem->setAttribute('item_id', 2); + /** test `set()`. */ + $orderItem->set('order_id', 2); + $orderItem->set('item_id', 2); $this->assertEquals(2, $orderItem->getOrder()->getId()); $this->assertEquals(2, $orderItem->getItem()->getId()); } @@ -431,30 +431,31 @@ public function testIssetNonExisting(): void $this->assertFalse(isset($cat->non_existing_property)); } - public function testSetAttributes(): void + public function testSetProperties(): void { - $attributes = []; $this->checkFixture($this->db(), 'customer'); - $attributes['email'] = 'samdark@mail.ru'; - $attributes['name'] = 'samdark'; - $attributes['address'] = 'rusia'; - $attributes['status'] = 1; + $properties = [ + 'email' => 'samdark@mail.ru', + 'name' => 'samdark', + 'address' => 'rusia', + 'status' => 1, + ]; if ($this->db()->getDriverName() === 'pgsql') { - $attributes['bool_status'] = true; + $properties['bool_status'] = true; } - $attributes['profile_id'] = null; + $properties['profile_id'] = null; $customer = new Customer(); - $customer->setAttributes($attributes); + $customer->populateProperties($properties); $this->assertTrue($customer->save()); } - public function testSetAttributeNoExist(): void + public function testSetPropertyNoExist(): void { self::markTestSkipped('There are no magic properties in the Cat class'); @@ -464,49 +465,49 @@ public function testSetAttributeNoExist(): void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Cat has no attribute named "noExist"' + 'Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Cat has no property named "noExist"' ); - $cat->setAttribute('noExist', 1); + $cat->set('noExist', 1); } - public function testSetOldAttribute(): void + public function testAssignOldValue(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertEmpty($customer->getOldAttribute('name')); + $this->assertEmpty($customer->oldValue('name')); - $customer->setOldAttribute('name', 'samdark'); + $customer->assignOldValue('name', 'samdark'); - $this->assertEquals('samdark', $customer->getOldAttribute('name')); + $this->assertEquals('samdark', $customer->oldValue('name')); } - public function testSetOldAttributeException(): void + public function testaAssignOldValueException(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertEmpty($customer->getOldAttribute('name')); + $this->assertEmpty($customer->oldValue('name')); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer has no attribute named "noExist"' + 'Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer has no property named "noExist"' ); - $customer->setOldAttribute('noExist', 'samdark'); + $customer->assignOldValue('noExist', 'samdark'); } - public function testIsAttributeChangedNotChanged(): void + public function testIsPropertyChangedNotChanged(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertEmpty($customer->getAttribute('email')); - $this->assertEmpty($customer->getOldAttribute('email')); - $this->assertFalse($customer->isAttributeChanged('email', false)); + $this->assertEmpty($customer->get('email')); + $this->assertEmpty($customer->oldValue('email')); + $this->assertFalse($customer->isPropertyChanged('email', false)); } public function testTableSchemaException(): void @@ -528,7 +529,7 @@ public function testInsert(): void $customer->setName('user4'); $customer->setAddress('address4'); - $this->assertNull($customer->getAttribute('id')); + $this->assertNull($customer->get('id')); $this->assertTrue($customer->getIsNewRecord()); $customer->save(); @@ -542,7 +543,7 @@ public function testInsert(): void * * Make sure this does not affect AR layer. */ - public function testBooleanAttribute(): void + public function testBooleanProperty(): void { $this->checkFixture($this->db(), 'customer', true); @@ -571,7 +572,7 @@ public function testBooleanAttribute(): void $this->assertCount(1, $customers); } - public function testAttributeAccess(): void + public function testPropertyAccess(): void { self::markTestSkipped('There are no magic properties in the Cat class'); @@ -619,7 +620,7 @@ public function testAttributeAccess(): void $this->expectExceptionMessage('Setting read-only property: ' . Customer::class . '::orderItems'); $customer->orderItems = [new Item()]; - /** related attribute $customer->orderItems didn't change cause it's read-only */ + /** related property $customer->orderItems didn't change cause it's read-only */ $this->assertSame([], $customer->orderItems); $this->assertFalse($customer->canGetProperty('non_existing_property')); $this->assertFalse($customer->canSetProperty('non_existing_property')); @@ -629,21 +630,21 @@ public function testAttributeAccess(): void $customer->non_existing_property = null; } - public function testHasAttribute(): void + public function testHasProperty(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertTrue($customer->hasAttribute('id')); - $this->assertTrue($customer->hasAttribute('email')); - $this->assertFalse($customer->hasAttribute('notExist')); + $this->assertTrue($customer->hasProperty('id')); + $this->assertTrue($customer->hasProperty('email')); + $this->assertFalse($customer->hasProperty('notExist')); $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); - $this->assertTrue($customer->hasAttribute('id')); - $this->assertTrue($customer->hasAttribute('email')); - $this->assertFalse($customer->hasAttribute('notExist')); + $this->assertTrue($customer->hasProperty('id')); + $this->assertTrue($customer->hasProperty('email')); + $this->assertFalse($customer->hasProperty('notExist')); } public function testRefresh(): void @@ -788,7 +789,7 @@ public function testGetOldPrimaryKey(): void $this->assertSame(['id' => 1], $customer->getOldPrimaryKey(true)); } - public function testGetDirtyAttributesOnNewRecord(): void + public function testGetDirtyValuesOnNewRecord(): void { $this->checkFixture($this->db(), 'customer'); @@ -802,14 +803,14 @@ public function testGetDirtyAttributesOnNewRecord(): void 'bool_status' => false, 'profile_id' => null, ], - $customer->getDirtyAttributes() + $customer->dirtyValues() ); - $customer->setAttribute('name', 'Adam'); - $customer->setAttribute('email', 'adam@example.com'); - $customer->setAttribute('address', null); + $customer->set('name', 'Adam'); + $customer->set('email', 'adam@example.com'); + $customer->set('address', null); - $this->assertEquals([], $customer->getDirtyAttributes([])); + $this->assertEquals([], $customer->dirtyValues([])); $this->assertEquals( [ @@ -820,7 +821,7 @@ public function testGetDirtyAttributesOnNewRecord(): void 'bool_status' => false, 'profile_id' => null, ], - $customer->getDirtyAttributes() + $customer->dirtyValues() ); $this->assertEquals( [ @@ -828,37 +829,37 @@ public function testGetDirtyAttributesOnNewRecord(): void 'address' => null, 'status' => 0, ], - $customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']), + $customer->dirtyValues(['id', 'email', 'address', 'status', 'unknown']), ); $this->assertTrue($customer->save()); - $this->assertSame([], $customer->getDirtyAttributes()); + $this->assertSame([], $customer->dirtyValues()); - $customer->setAttribute('address', ''); + $customer->set('address', ''); - $this->assertSame(['address' => ''], $customer->getDirtyAttributes()); + $this->assertSame(['address' => ''], $customer->dirtyValues()); } - public function testGetDirtyAttributesAfterFind(): void + public function testGetDirtyValuesAfterFind(): void { $this->checkFixture($this->db(), 'customer'); $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); - $this->assertSame([], $customer->getDirtyAttributes()); + $this->assertSame([], $customer->dirtyValues()); - $customer->setAttribute('name', 'Adam'); - $customer->setAttribute('email', 'adam@example.com'); - $customer->setAttribute('address', null); + $customer->set('name', 'Adam'); + $customer->set('email', 'adam@example.com'); + $customer->set('address', null); $this->assertEquals( ['name' => 'Adam', 'email' => 'adam@example.com', 'address' => null], - $customer->getDirtyAttributes(), + $customer->dirtyValues(), ); $this->assertEquals( ['email' => 'adam@example.com', 'address' => null], - $customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']), + $customer->dirtyValues(['id', 'email', 'address', 'status', 'unknown']), ); } @@ -1002,7 +1003,7 @@ public function testSerialization(): void $profile = new Profile(); $this->assertEquals( - "O:53:\"Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Profile\":3:{s:56:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0oldAttributes\";N;s:50:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0related\";a:0:{}s:64:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0relationsDependencies\";a:0:{}}", + "O:53:\"Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Profile\":3:{s:52:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0oldValues\";N;s:50:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0related\";a:0:{}s:64:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0relationsDependencies\";a:0:{}}", serialize($profile) ); @@ -1010,7 +1011,7 @@ public function testSerialization(): void $profile = $profileQuery->findOne(1); $this->assertEquals( - "O:53:\"Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Profile\":5:{s:56:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0oldAttributes\";a:2:{s:2:\"id\";i:1;s:11:\"description\";s:18:\"profile customer 1\";}s:50:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0related\";a:0:{}s:64:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0relationsDependencies\";a:0:{}s:5:\"\0*\0id\";i:1;s:14:\"\0*\0description\";s:18:\"profile customer 1\";}", + "O:53:\"Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Profile\":5:{s:52:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0oldValues\";a:2:{s:2:\"id\";i:1;s:11:\"description\";s:18:\"profile customer 1\";}s:50:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0related\";a:0:{}s:64:\"\0Yiisoft\ActiveRecord\AbstractActiveRecord\0relationsDependencies\";a:0:{}s:5:\"\0*\0id\";i:1;s:14:\"\0*\0description\";s:18:\"profile customer 1\";}", serialize($profile) ); } diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index af89974c4..e56917d83 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -90,7 +90,7 @@ public function testDefaultValues(): void * * Make sure this does not affect AR layer. */ - public function testBooleanAttribute(): void + public function testBooleanProperty(): void { $this->checkFixture($this->db(), 'customer', true); diff --git a/tests/Driver/Oracle/MagicActiveRecordTest.php b/tests/Driver/Oracle/MagicActiveRecordTest.php index 5a59a69d5..664cd2416 100644 --- a/tests/Driver/Oracle/MagicActiveRecordTest.php +++ b/tests/Driver/Oracle/MagicActiveRecordTest.php @@ -84,7 +84,7 @@ public function testDefaultValues(): void * * Make sure this does not affect AR layer. */ - public function testBooleanAttribute(): void + public function testBooleanProperty(): void { $this->checkFixture($this->db(), 'customer', true); diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 101994460..1270ba910 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -135,7 +135,7 @@ public function testEagerLoadingUsingStringIdentifiers(): void $this->assertEquals(['1', '01', '001', '001', '2', '2b', '2b', '02'], $alphaIdentifiers); } - public function testBooleanAttribute(): void + public function testBooleanProperty(): void { $this->checkFixture($this->db(), 'customer', true); @@ -353,14 +353,14 @@ public static function arrayValuesProvider(): array /** * @dataProvider arrayValuesProvider */ - public function testArrayValues($attributes): void + public function testArrayValues($properties): void { $this->checkFixture($this->db(), 'array_and_json_types', true); $type = new ArrayAndJsonTypes(); - foreach ($attributes as $attribute => $expected) { - $type->setAttribute($attribute, $expected[0]); + foreach ($properties as $property => $expected) { + $type->set($property, $expected[0]); } $type->save(); @@ -369,29 +369,29 @@ public function testArrayValues($attributes): void $type = $typeQuery->one(); - foreach ($attributes as $attribute => $expected) { + foreach ($properties as $property => $expected) { $expected = $expected[1] ?? $expected[0]; - $value = $type->getAttribute($attribute); + $value = $type->get($property); if ($expected instanceof ArrayExpression) { $expected = $expected->getValue(); } - $this->assertEquals($expected, $value, 'In column ' . $attribute); + $this->assertEquals($expected, $value, 'In column ' . $property); if ($value instanceof ArrayExpression) { $this->assertInstanceOf(ArrayAccess::class, $value); $this->assertInstanceOf(Traversable::class, $value); /** testing arrayaccess */ - foreach ($type->getAttribute($attribute) as $key => $v) { + foreach ($type->get($property) as $key => $v) { $this->assertSame($expected[$key], $value[$key]); } } } /** Testing update */ - foreach ($attributes as $attribute => $expected) { - $type->markAttributeDirty($attribute); + foreach ($properties as $property => $expected) { + $type->markPropertyDirty($property); } $this->assertSame(1, $type->update(), 'The record got updated'); diff --git a/tests/Driver/Pgsql/MagicActiveRecordTest.php b/tests/Driver/Pgsql/MagicActiveRecordTest.php index 951f59d27..41ada1d75 100644 --- a/tests/Driver/Pgsql/MagicActiveRecordTest.php +++ b/tests/Driver/Pgsql/MagicActiveRecordTest.php @@ -66,7 +66,7 @@ public function testEagerLoadingUsingStringIdentifiers(): void $this->assertEquals(['1', '01', '001', '001', '2', '2b', '2b', '02'], $alphaIdentifiers); } - public function testBooleanAttribute(): void + public function testBooleanProperty(): void { $this->checkFixture($this->db(), 'customer', true); @@ -284,14 +284,14 @@ public static function arrayValuesProvider(): array /** * @dataProvider arrayValuesProvider */ - public function testArrayValues($attributes): void + public function testArrayValues($properties): void { $this->checkFixture($this->db(), 'array_and_json_types', true); $type = new ArrayAndJsonTypes(); - foreach ($attributes as $attribute => $expected) { - $type->$attribute = $expected[0]; + foreach ($properties as $property => $expected) { + $type->$property = $expected[0]; } $type->save(); @@ -300,29 +300,29 @@ public function testArrayValues($attributes): void $type = $typeQuery->one(); - foreach ($attributes as $attribute => $expected) { + foreach ($properties as $property => $expected) { $expected = $expected[1] ?? $expected[0]; - $value = $type->$attribute; + $value = $type->$property; if ($expected instanceof ArrayExpression) { $expected = $expected->getValue(); } - $this->assertEquals($expected, $value, 'In column ' . $attribute); + $this->assertEquals($expected, $value, 'In column ' . $property); if ($value instanceof ArrayExpression) { $this->assertInstanceOf(ArrayAccess::class, $value); $this->assertInstanceOf(Traversable::class, $value); /** testing arrayaccess */ - foreach ($type->$attribute as $key => $v) { + foreach ($type->$property as $key => $v) { $this->assertSame($expected[$key], $value[$key]); } } } /** Testing update */ - foreach ($attributes as $attribute => $expected) { - $type->markAttributeDirty($attribute); + foreach ($properties as $property => $expected) { + $type->markPropertyDirty($property); } $this->assertSame(1, $type->update(), 'The record got updated'); diff --git a/tests/MagicActiveRecordTest.php b/tests/MagicActiveRecordTest.php index d7fb24ac8..e8c3ba64b 100644 --- a/tests/MagicActiveRecordTest.php +++ b/tests/MagicActiveRecordTest.php @@ -37,46 +37,46 @@ public function testStoreNull(): void $record = new NullValues(); - $this->assertNull($record->getAttribute('var1')); - $this->assertNull($record->getAttribute('var2')); - $this->assertNull($record->getAttribute('var3')); - $this->assertNull($record->getAttribute('stringcol')); - - $record->setAttribute('var1', 123); - $record->setAttribute('var2', 456); - $record->setAttribute('var3', 789); - $record->setAttribute('stringcol', 'hello!'); + $this->assertNull($record->get('var1')); + $this->assertNull($record->get('var2')); + $this->assertNull($record->get('var3')); + $this->assertNull($record->get('stringcol')); + + $record->set('var1', 123); + $record->set('var2', 456); + $record->set('var3', 789); + $record->set('stringcol', 'hello!'); $record->save(); $this->assertTrue($record->refresh()); - $this->assertEquals(123, $record->getAttribute('var1')); - $this->assertEquals(456, $record->getAttribute('var2')); - $this->assertEquals(789, $record->getAttribute('var3')); - $this->assertEquals('hello!', $record->getAttribute('stringcol')); - - $record->setAttribute('var1', null); - $record->setAttribute('var2', null); - $record->setAttribute('var3', null); - $record->setAttribute('stringcol', null); + $this->assertEquals(123, $record->get('var1')); + $this->assertEquals(456, $record->get('var2')); + $this->assertEquals(789, $record->get('var3')); + $this->assertEquals('hello!', $record->get('stringcol')); + + $record->set('var1', null); + $record->set('var2', null); + $record->set('var3', null); + $record->set('stringcol', null); $record->save(); $this->assertTrue($record->refresh()); - $this->assertNull($record->getAttribute('var1')); - $this->assertNull($record->getAttribute('var2')); - $this->assertNull($record->getAttribute('var3')); - $this->assertNull($record->getAttribute('>stringcol')); - - $record->setAttribute('var1', 0); - $record->setAttribute('var2', 0); - $record->setAttribute('var3', 0); - $record->setAttribute('stringcol', ''); + $this->assertNull($record->get('var1')); + $this->assertNull($record->get('var2')); + $this->assertNull($record->get('var3')); + $this->assertNull($record->get('>stringcol')); + + $record->set('var1', 0); + $record->set('var2', 0); + $record->set('var3', 0); + $record->set('stringcol', ''); $record->save(); $this->assertTrue($record->refresh()); - $this->assertEquals(0, $record->getAttribute('var1')); - $this->assertEquals(0, $record->getAttribute('var2')); - $this->assertEquals(0, $record->getAttribute('var3')); - $this->assertEquals('', $record->getAttribute('stringcol')); + $this->assertEquals(0, $record->get('var1')); + $this->assertEquals(0, $record->get('var2')); + $this->assertEquals(0, $record->get('var3')); + $this->assertEquals('', $record->get('stringcol')); } public function testStoreEmpty(): void @@ -138,9 +138,9 @@ public function testOutdatedRelationsAreResetForNewRecords(): void $this->assertEquals(2, $orderItem->order->id); $this->assertEquals(1, $orderItem->item->id); - /** test `setAttribute()`. */ - $orderItem->setAttribute('order_id', 2); - $orderItem->setAttribute('item_id', 2); + /** test `set()`. */ + $orderItem->set('order_id', 2); + $orderItem->set('item_id', 2); $this->assertEquals(2, $orderItem->order->id); $this->assertEquals(2, $orderItem->item->id); } @@ -417,30 +417,31 @@ public function testIssetNonExisting(): void $this->assertFalse(isset($cat->non_existing_property)); } - public function testSetAttributes(): void + public function testAssignProperties(): void { - $attributes = []; $this->checkFixture($this->db(), 'customer'); - $attributes['email'] = 'samdark@mail.ru'; - $attributes['name'] = 'samdark'; - $attributes['address'] = 'rusia'; - $attributes['status'] = 1; + $properties = [ + 'email' => 'samdark@mail.ru', + 'name' => 'samdark', + 'address' => 'rusia', + 'status' => 1, + ]; if ($this->db()->getDriverName() === 'pgsql') { - $attributes['bool_status'] = true; + $properties['bool_status'] = true; } - $attributes['profile_id'] = null; + $properties['profile_id'] = null; $customer = new Customer(); - $customer->setAttributes($attributes); + $customer->populateProperties($properties); $this->assertTrue($customer->save()); } - public function testSetAttributeNoExist(): void + public function testSetNoExistProperty(): void { $this->checkFixture($this->db(), 'cat'); @@ -448,49 +449,49 @@ public function testSetAttributeNoExist(): void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Cat has no attribute named "noExist"' + 'Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Cat has no property named "noExist"' ); - $cat->setAttribute('noExist', 1); + $cat->set('noExist', 1); } - public function testSetOldAttribute(): void + public function testAssignOldValue(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertEmpty($customer->getOldAttribute('name')); + $this->assertEmpty($customer->oldValue('name')); - $customer->setOldAttribute('name', 'samdark'); + $customer->assignOldValue('name', 'samdark'); - $this->assertEquals('samdark', $customer->getOldAttribute('name')); + $this->assertEquals('samdark', $customer->oldValue('name')); } - public function testSetOldAttributeException(): void + public function testAssignOldValueException(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertEmpty($customer->getOldAttribute('name')); + $this->assertEmpty($customer->oldValue('name')); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Customer has no attribute named "noExist"' + 'Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Customer has no property named "noExist"' ); - $customer->setOldAttribute('noExist', 'samdark'); + $customer->assignOldValue('noExist', 'samdark'); } - public function testIsAttributeChangedNotChanged(): void + public function testIsPropertyChangedNotChanged(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertEmpty($customer->getAttribute('name')); - $this->assertEmpty($customer->getOldAttribute('name')); - $this->assertFalse($customer->isAttributeChanged('name', false)); + $this->assertEmpty($customer->get('name')); + $this->assertEmpty($customer->oldValue('name')); + $this->assertFalse($customer->isPropertyChanged('name', false)); } public function testTableSchemaException(): void @@ -526,7 +527,7 @@ public function testInsert(): void * * Make sure this does not affect AR layer. */ - public function testBooleanAttribute(): void + public function testBooleanProperty(): void { $this->checkFixture($this->db(), 'customer', true); @@ -555,7 +556,7 @@ public function testBooleanAttribute(): void $this->assertCount(1, $customers); } - public function testAttributeAccess(): void + public function testPropertyAccess(): void { $this->checkFixture($this->db(), 'customer'); @@ -601,7 +602,7 @@ public function testAttributeAccess(): void $this->expectExceptionMessage('Setting read-only property: ' . Customer::class . '::orderItems'); $customer->orderItems = [new Item()]; - /** related attribute $customer->orderItems didn't change cause it's read-only */ + /** related property $customer->orderItems didn't change cause it's read-only */ $this->assertSame([], $customer->orderItems); $this->assertFalse($customer->canGetProperty('non_existing_property')); $this->assertFalse($customer->canSetProperty('non_existing_property')); @@ -611,21 +612,21 @@ public function testAttributeAccess(): void $customer->non_existing_property = null; } - public function testHasAttribute(): void + public function testHasProperty(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertTrue($customer->hasAttribute('id')); - $this->assertTrue($customer->hasAttribute('email')); - $this->assertFalse($customer->hasAttribute('notExist')); + $this->assertTrue($customer->hasProperty('id')); + $this->assertTrue($customer->hasProperty('email')); + $this->assertFalse($customer->hasProperty('notExist')); $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); - $this->assertTrue($customer->hasAttribute('id')); - $this->assertTrue($customer->hasAttribute('email')); - $this->assertFalse($customer->hasAttribute('notExist')); + $this->assertTrue($customer->hasProperty('id')); + $this->assertTrue($customer->hasProperty('email')); + $this->assertFalse($customer->hasProperty('notExist')); } public function testRefresh(): void @@ -770,59 +771,59 @@ public function testGetOldPrimaryKey(): void $this->assertSame(['id' => 1], $customer->getOldPrimaryKey(true)); } - public function testGetDirtyAttributesOnNewRecord(): void + public function testGetDirtyValuesOnNewRecord(): void { $this->checkFixture($this->db(), 'customer'); $customer = new Customer(); - $this->assertSame([], $customer->getDirtyAttributes()); + $this->assertSame([], $customer->dirtyValues()); - $customer->setAttribute('name', 'Adam'); - $customer->setAttribute('email', 'adam@example.com'); - $customer->setAttribute('address', null); + $customer->set('name', 'Adam'); + $customer->set('email', 'adam@example.com'); + $customer->set('address', null); $this->assertEquals( ['name' => 'Adam', 'email' => 'adam@example.com', 'address' => null], - $customer->getDirtyAttributes() + $customer->dirtyValues() ); $this->assertEquals( ['email' => 'adam@example.com', 'address' => null], - $customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']), + $customer->dirtyValues(['id', 'email', 'address', 'status', 'unknown']), ); $this->assertTrue($customer->save()); - $this->assertSame([], $customer->getDirtyAttributes()); + $this->assertSame([], $customer->dirtyValues()); - $customer->setAttribute('address', ''); + $customer->set('address', ''); - $this->assertSame(['address' => ''], $customer->getDirtyAttributes()); + $this->assertSame(['address' => ''], $customer->dirtyValues()); } - public function testGetDirtyAttributesAfterFind(): void + public function testGetDirtyValuesAfterFind(): void { $this->checkFixture($this->db(), 'customer'); $customerQuery = new ActiveQuery(Customer::class); $customer = $customerQuery->findOne(1); - $this->assertSame([], $customer->getDirtyAttributes()); + $this->assertSame([], $customer->dirtyValues()); - $customer->setAttribute('name', 'Adam'); - $customer->setAttribute('email', 'adam@example.com'); - $customer->setAttribute('address', null); + $customer->set('name', 'Adam'); + $customer->set('email', 'adam@example.com'); + $customer->set('address', null); $this->assertEquals( ['name' => 'Adam', 'email' => 'adam@example.com', 'address' => null], - $customer->getDirtyAttributes(), + $customer->dirtyValues(), ); $this->assertEquals( ['email' => 'adam@example.com', 'address' => null], - $customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']), + $customer->dirtyValues(['id', 'email', 'address', 'status', 'unknown']), ); } - public function testGetDirtyAttributesWithProperties(): void + public function testGetDirtyValuesWithProperties(): void { $this->checkFixture($this->db(), 'customer'); @@ -830,12 +831,12 @@ public function testGetDirtyAttributesWithProperties(): void $this->assertSame([ 'name' => null, 'address' => null, - ], $customer->getDirtyAttributes()); + ], $customer->dirtyValues()); $customerQuery = new ActiveQuery(CustomerWithProperties::class); $customer = $customerQuery->findOne(1); - $this->assertSame([], $customer->getDirtyAttributes()); + $this->assertSame([], $customer->dirtyValues()); $customer->setEmail('adam@example.com'); $customer->setName('Adam'); @@ -844,11 +845,11 @@ public function testGetDirtyAttributesWithProperties(): void $this->assertEquals( ['email' => 'adam@example.com', 'name' => 'Adam', 'address' => null, 'status' => null], - $customer->getDirtyAttributes(), + $customer->dirtyValues(), ); $this->assertEquals( ['email' => 'adam@example.com', 'address' => null], - $customer->getDirtyAttributes(['id', 'email', 'address', 'unknown']), + $customer->dirtyValues(['id', 'email', 'address', 'unknown']), ); } diff --git a/tests/Stubs/ActiveRecord/Category.php b/tests/Stubs/ActiveRecord/Category.php index e151077cc..382f94556 100644 --- a/tests/Stubs/ActiveRecord/Category.php +++ b/tests/Stubs/ActiveRecord/Category.php @@ -44,7 +44,7 @@ public function getName(): string public function setId(int|null $id): void { - $this->setAttribute('id', $id); + $this->set('id', $id); } public function getLimitedItems(): array diff --git a/tests/Stubs/ActiveRecord/Customer.php b/tests/Stubs/ActiveRecord/Customer.php index 22d118c35..56b1b9a36 100644 --- a/tests/Stubs/ActiveRecord/Customer.php +++ b/tests/Stubs/ActiveRecord/Customer.php @@ -125,7 +125,7 @@ public function setBoolStatus(bool|null $bool_status): void public function setProfileId(int|null $profile_id): void { - $this->setAttribute('profile_id', $profile_id); + $this->set('profile_id', $profile_id); } public function getProfile(): Profile|null diff --git a/tests/Stubs/ActiveRecord/Dossier.php b/tests/Stubs/ActiveRecord/Dossier.php index 56642adc2..edd8099f3 100644 --- a/tests/Stubs/ActiveRecord/Dossier.php +++ b/tests/Stubs/ActiveRecord/Dossier.php @@ -50,12 +50,12 @@ public function setId(int $id): void public function setDepartmentId(int $departmentId): void { - $this->setAttribute('department_id', $departmentId); + $this->set('department_id', $departmentId); } public function setEmployeeId(int $employeeId): void { - $this->setAttribute('employee_id', $employeeId); + $this->set('employee_id', $employeeId); } public function setSummary(string $summary): void diff --git a/tests/Stubs/ActiveRecord/Order.php b/tests/Stubs/ActiveRecord/Order.php index beea8e89c..1d8b1e119 100644 --- a/tests/Stubs/ActiveRecord/Order.php +++ b/tests/Stubs/ActiveRecord/Order.php @@ -52,12 +52,12 @@ public function getTotal(): float public function setId(int|null $id): void { - $this->setAttribute('id', $id); + $this->set('id', $id); } public function setCustomerId(int $customerId): void { - $this->setAttribute('customer_id', $customerId); + $this->set('customer_id', $customerId); } public function setCreatedAt(int $createdAt): void diff --git a/tests/Stubs/ActiveRecord/OrderItem.php b/tests/Stubs/ActiveRecord/OrderItem.php index 800c17e7f..471a730ab 100644 --- a/tests/Stubs/ActiveRecord/OrderItem.php +++ b/tests/Stubs/ActiveRecord/OrderItem.php @@ -48,12 +48,12 @@ public function getSubtotal(): float public function setOrderId(int $orderId): void { - $this->setAttribute('order_id', $orderId); + $this->set('order_id', $orderId); } public function setItemId(int $itemId): void { - $this->setAttribute('item_id', $itemId); + $this->set('item_id', $itemId); } public function setQuantity(int $quantity): void diff --git a/tests/Stubs/MagicActiveRecord.php b/tests/Stubs/MagicActiveRecord.php index f67c947ac..90d79541d 100644 --- a/tests/Stubs/MagicActiveRecord.php +++ b/tests/Stubs/MagicActiveRecord.php @@ -12,7 +12,7 @@ /** * Active Record class which implements {@see ActiveRecordInterface} and provides additional features like: * - * @see MagicPropertiesTrait to access attributes as properties; + * @see MagicPropertiesTrait to access column values and relations via PHP magic methods as properties; * @see MagicRelationsTrait to access relation queries. */ class MagicActiveRecord extends ActiveRecord diff --git a/tests/Stubs/MagicActiveRecord/Customer.php b/tests/Stubs/MagicActiveRecord/Customer.php index 1c24001ad..1ea2217ef 100644 --- a/tests/Stubs/MagicActiveRecord/Customer.php +++ b/tests/Stubs/MagicActiveRecord/Customer.php @@ -37,7 +37,7 @@ public function getTableName(): string public function getName(): string { - return $this->getAttribute('name'); + return $this->get('name'); } public function getProfileQuery(): ActiveQuery diff --git a/tests/Stubs/MagicActiveRecord/CustomerWithProperties.php b/tests/Stubs/MagicActiveRecord/CustomerWithProperties.php index 79c2c581f..37f7a8af0 100644 --- a/tests/Stubs/MagicActiveRecord/CustomerWithProperties.php +++ b/tests/Stubs/MagicActiveRecord/CustomerWithProperties.php @@ -44,7 +44,7 @@ public function getAddress(): string|null public function getStatus(): int|null { - return $this->getAttribute('status'); + return $this->get('status'); } public function getProfileQuery(): ActiveQuery @@ -74,6 +74,6 @@ public function setAddress(string|null $address): void public function setStatus(int|null $status): void { - $this->setAttribute('status', $status); + $this->set('status', $status); } } diff --git a/tests/Stubs/MagicActiveRecord/OrderItem.php b/tests/Stubs/MagicActiveRecord/OrderItem.php index 1fd40ed91..ddcbad6df 100644 --- a/tests/Stubs/MagicActiveRecord/OrderItem.php +++ b/tests/Stubs/MagicActiveRecord/OrderItem.php @@ -26,11 +26,11 @@ public function fields(): array { $fields = parent::fields(); - $fields['order_id'] = $this->getAttribute('order_id'); - $fields['item_id'] = $this->getAttribute('item_id'); - $fields['price'] = $this->getAttribute('subtotal') / $this->getAttribute('quantity'); - $fields['quantity'] = $this->getAttribute('quantity'); - $fields['subtotal'] = $this->getAttribute('subtotal'); + $fields['order_id'] = $this->get('order_id'); + $fields['item_id'] = $this->get('item_id'); + $fields['price'] = $this->get('subtotal') / $this->get('quantity'); + $fields['quantity'] = $this->get('quantity'); + $fields['subtotal'] = $this->get('subtotal'); return $fields; }