Skip to content

Commit

Permalink
Add resetRelation() method to ActiveRecordInterface (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored May 21, 2024
1 parent a4816d0 commit 5c28c20
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
81 changes: 44 additions & 37 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,43 +184,6 @@ public function getOldPrimaryKey(bool $asArray = false): mixed;
*/
public function getPrimaryKey(bool $asArray = false): mixed;

/**
* Returns the relation object with the specified name.
*
* @param string $name The relation name, for example `orders` (case-sensitive).
*
* @return ActiveRecordInterface|array|null The relation object.
*/
public function relation(string $name): self|array|null;

/**
* Returns the relation query object with the specified name.
*
* 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;

/**
* Return the name of the table associated with this AR class.
*
Expand Down Expand Up @@ -345,6 +308,50 @@ public function populateRelation(string $name, array|self|null $records): void;
*/
public function primaryKey(): array;

/**
* Returns the relation object with the specified name.
*
* @param string $name The relation name, for example `orders` (case-sensitive).
*
* @return ActiveRecordInterface|array|null The relation object.
*/
public function relation(string $name): self|array|null;

/**
* Returns the relation query object with the specified name.
*
* 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;

/**
* Resets relation data for the specified name.
*
* @param string $name The relation name, for example `orders` (case-sensitive).
*/
public function resetRelation(string $name): void;

/**
* Saves the current record.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Trait/MagicPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @see AbstractActiveRecord::resetDependentRelations()
*
* @method void resetRelation(string $name)
* @see AbstractActiveRecord::resetRelation()
* @see ActiveRecordInterface::resetRelation()
*
* @method ActiveRecordInterface|array|null retrieveRelation(string $name)
* @see AbstractActiveRecord::retrieveRelation()
Expand Down

0 comments on commit 5c28c20

Please sign in to comment.