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..c81139a04 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -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, bool $throwException = true): 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;