diff --git a/CHANGELOG.md b/CHANGELOG.md index ea1b1846b..036d4bc04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Bug #562: Fix `ActiveRecordInterface::upsert()` to prioritize passed associative values during updates (@Tigrov) - Bug #561: Fix `ActiveRecordInterface::upsert()` with `$updateProperties = false` (@Tigrov) - Bug #550: Relation query should be created by related class, not primary model class (@batyrmastyr) +- Enh #571: Optimize performance of `ActiveRecord::get()` method (@Tigrov) ## 1.0.2 March 11, 2026 diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index 9be9d6a16..985f8d90c 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -766,7 +766,7 @@ public function db(): ConnectionInterface /** * Returns the available property values of an Active Record object. * - * @return array + * @return array The property values (name-value pairs). * * @psalm-return array */ diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 410d1a5c8..9ee4c107a 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -79,6 +79,11 @@ */ class ActiveRecord extends AbstractActiveRecord { + public function get(string $propertyName): mixed + { + return $this->$propertyName ?? null; + } + public function propertyNames(): array { return $this->tableSchema()->getColumnNames(); diff --git a/src/Trait/MagicPropertiesTrait.php b/src/Trait/MagicPropertiesTrait.php index 3be39cb42..b35346c28 100644 --- a/src/Trait/MagicPropertiesTrait.php +++ b/src/Trait/MagicPropertiesTrait.php @@ -165,6 +165,15 @@ public function hasRelationQuery(string $name): bool return method_exists($this, "get{$name}Query"); } + public function get(string $propertyName): mixed + { + if ($propertyName !== 'propertyValues' && property_exists($this, $propertyName)) { + return $this->$propertyName ?? null; + } + + return $this->propertyValues[$propertyName] ?? null; + } + public function set(string $propertyName, mixed $value): void { if ($this->hasProperty($propertyName)) { diff --git a/src/Trait/PrivatePropertiesTrait.php b/src/Trait/PrivatePropertiesTrait.php index fb60d3d9a..c931decc5 100644 --- a/src/Trait/PrivatePropertiesTrait.php +++ b/src/Trait/PrivatePropertiesTrait.php @@ -17,6 +17,11 @@ */ trait PrivatePropertiesTrait { + public function get(string $propertyName): mixed + { + return $this->$propertyName ?? null; + } + protected function populateProperty(string $name, mixed $value): void { $this->$name = $value;