From 41d818808065e8f7db3c1948347f43e05558acda Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 20 May 2024 17:35:05 +0700 Subject: [PATCH 1/3] Refactoring --- src/AbstractActiveRecord.php | 16 ---------------- src/ActiveRecord.php | 13 ++++++++----- src/ActiveRecordInterface.php | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index f77b54c66..cfdc2b1c5 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -549,22 +549,6 @@ public function relation(string $name): ActiveRecordInterface|array|null return $this->retrieveRelation($name); } - /** - * @inheritdoc - * - * Relations can be defined using {@see hasOne()} and {@see hasMany()} methods. For example: - * - * ```php - * public function relationQuery(string $name): ActiveQueryInterface - * { - * return match ($name) { - * 'orders' => $this->hasMany(Order::class, ['customer_id' => 'id']), - * 'country' => $this->hasOne(Country::class, ['id' => 'country_id']), - * default => parent::relationQuery($name), - * }; - * } - * ``` - */ public function relationQuery(string $name, bool $throwException = true): ActiveQueryInterface|null { if (!$throwException) { diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index be45c41e8..d282e144d 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -172,9 +172,9 @@ public function getTableSchema(): TableSchemaInterface */ public function loadDefaultValues(bool $skipIfSet = true): self { - foreach ($this->getTableSchema()->getColumns() as $column) { - if ($column->getDefaultValue() !== null && (!$skipIfSet || $this->getAttribute($column->getName()) === null)) { - $this->setAttribute($column->getName(), $column->getDefaultValue()); + foreach ($this->getTableSchema()->getColumns() as $name => $column) { + if ($column->getDefaultValue() !== null && (!$skipIfSet || $this->getAttribute($name) === null)) { + $this->setAttribute($name, $column->getDefaultValue()); } } @@ -267,13 +267,16 @@ protected function filterValidColumnNames(array $aliases): array protected function insertInternal(array $attributes = null): bool { $values = $this->getDirtyAttributes($attributes); + $primaryKeys = $this->db->createCommand()->insertWithReturningPks($this->getTableName(), $values); - if (($primaryKeys = $this->db->createCommand()->insertWithReturningPks($this->getTableName(), $values)) === false) { + if ($primaryKeys === false) { return false; } + $columns = $this->getTableSchema()->getColumns(); + foreach ($primaryKeys as $name => $value) { - $id = $this->getTableSchema()->getColumn($name)?->phpTypecast($value); + $id = $columns[$name]->phpTypecast($value); $this->setAttribute($name, $id); $values[$name] = $id; } diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 4843b6d50..b635ee118 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -191,7 +191,7 @@ public function getPrimaryKey(bool $asArray = false): mixed; * * @return ActiveRecordInterface|array|null The relation object. */ - public function relation(string $name): self|array|null; + public function relation(string $name): ActiveRecordInterface|array|null; /** * Returns the relation query object with the specified name. @@ -199,9 +199,24 @@ public function relation(string $name): self|array|null; * A relation is defined by a getter method which returns an object implementing the {@see ActiveQueryInterface} * (normally this would be a relational {@see ActiveQuery} object). * + * Relations can be defined using {@see hasOne()} and {@see hasMany()} methods. For example: + * + * ```php + * public function relationQuery(string $name): ActiveQueryInterface + * { + * return match ($name) { + * 'orders' => $this->hasMany(Order::class, ['customer_id' => 'id']), + * 'country' => $this->hasOne(Country::class, ['id' => 'country_id']), + * default => parent::relationQuery($name), + * }; + * } + * ``` + * * @param string $name The relation name, for example `orders` (case-sensitive). * @param bool $throwException Whether to throw exception if the relation doesn't exist. * + * @throws InvalidArgumentException + * * @return ActiveQueryInterface|null The relational query object. */ public function relationQuery(string $name, bool $throwException = true): ActiveQueryInterface|null; From 75561002cbebb50a7f908a9302281be1c9fb4da6 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 20 May 2024 10:35:29 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- src/ActiveRecordInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index b635ee118..adf291d0d 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -191,7 +191,7 @@ public function getPrimaryKey(bool $asArray = false): mixed; * * @return ActiveRecordInterface|array|null The relation object. */ - public function relation(string $name): ActiveRecordInterface|array|null; + public function relation(string $name): self|array|null; /** * Returns the relation query object with the specified name. From 841ae6b3f3f4169e85506b75b13d48915aa5d84f Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 20 May 2024 17:39:28 +0700 Subject: [PATCH 3/3] Fix doc [skip ci] --- src/ActiveRecordInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index adf291d0d..c81139a04 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -202,7 +202,7 @@ public function relation(string $name): self|array|null; * Relations can be defined using {@see hasOne()} and {@see hasMany()} methods. For example: * * ```php - * public function relationQuery(string $name): ActiveQueryInterface + * public function relationQuery(string $name, bool $throwException = true): ActiveQueryInterface * { * return match ($name) { * 'orders' => $this->hasMany(Order::class, ['customer_id' => 'id']),