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

Allows override query relation in descendant class #13642

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
23 changes: 15 additions & 8 deletions framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,7 @@ public function __unset($name)
*/
public function hasOne($class, $link)
{
/* @var $class ActiveRecordInterface */
/* @var $query ActiveQuery */
$query = $class::find();
$query->primaryModel = $this;
$query->link = $link;
$query->multiple = false;
return $query;
return $this->createRelationQuery($class, $link, false);
}

/**
Expand Down Expand Up @@ -410,13 +404,26 @@ public function hasOne($class, $link)
* @return ActiveQueryInterface the relational query object.
*/
public function hasMany($class, $link)
{
return $this->createRelationQuery($class, $link, true);
}

/**
* Creates query instance for `has-one` and `has-many` relation.
* @param string $class the class name of the related record
* @param array $link the primary-foreign key constraint
* @param boolean whether this query represents a relation to more than one record.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name $multiple missing.

* @return ActiveQueryInterface the relational query object.
* @since 2.0.12
*/
protected function createRelationQuery($class, $link, $multiple)
{
/* @var $class ActiveRecordInterface */
/* @var $query ActiveQuery */
$query = $class::find();
$query->primaryModel = $this;
$query->link = $link;
$query->multiple = true;
$query->multiple = $multiple;
return $query;
}

Expand Down