diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index fa2e8806d5c..e8975542d16 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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 @@ -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) - diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index c2e6e617252..cf51a83c4b7 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -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); } /** @@ -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; }