Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactoring #326

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down