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
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
8 changes: 4 additions & 4 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ Yii Framework 2 Change Log
- Bug #13592: Fixes Oracle’s `yii\db\oci\Schema::setTransactionIsolationLevel()` (sergeymakinen)
- Bug #13594: Fixes insufficient quoting in `yii\db\QueryBuilder::prepareInsertSelectSubQuery()` (sergeymakinen)
- Enh #8641: Enhanced `yii\console\Request::resolve()` to prevent passing parameters, that begin from digits (silverfire)
- Enh #13144: Refactored `yii\db\Query::queryScalar()` (Alex-Code)
- Enh #13221: Make `\yii\db\QueryTrait::limit()` and `\yii\db\QueryTrait::offset()` methods work with `\yii\db\Expression` (Ni-san)
- Enh #13278: `yii\caching\DbQueryDependency` created allowing specification of the cache dependency via `yii\db\QueryInterface` (klimov-paul)
- Enh #13407: Added URL-safe base64 encode/decode methods to `StringHelper` (andrewnester)
- Enh #13467: `yii\data\ActiveDataProvider` no longer queries models if models count is zero (kLkA, Kolyunya)
- Enh #13523: Plural rule for pasta (developeruz)
- Enh #13550: Refactored unset call order in `yii\di\ServiceLocator::set()` (Lanrik)
- Enh #13576: Added support of `srcset` to `yii\helpers\Html::img()` (Kolyunya)
- Enh #13577: Implemented `yii\db\mssql\QueryBuilder::resetSequence()` (boboldehampsink)
- Enh #13582: Added tests for all `yii\db\QueryBuilder::resetSequence` implementations, fixed SQLite implementation (boboldehampsink)
- Enh #13407: Added URL-safe base64 encode/decode methods to `StringHelper` (andrewnester)
- Enh #13221: Make `\yii\db\QueryTrait::limit()` and `\yii\db\QueryTrait::offset()` methods work with `\yii\db\Expression` (Ni-san)
- Enh #13144: Refactored `yii\db\Query::queryScalar()` (Alex-Code)
- Enh #13642: Allows override query relation in descendant class (leandrogehlen)


2.0.11.2 February 08, 2017
Expand Down Expand Up @@ -1782,4 +1783,3 @@ Yii Framework 2 Change Log

- [Smarty View Renderer](https://github.com/yiisoft/yii2-smarty)
- [Twig View Renderer](https://github.com/yiisoft/yii2-twig)

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 $multiple whether this query represents a relation to more than one record.
* @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