Skip to content

Commit

Permalink
Fixed ambiguous column error in BaseActiveRecord::refresh()
Browse files Browse the repository at this point in the history
when the query adds a JOIN by default

fixes yiisoft#13757
  • Loading branch information
cebe committed Jul 11, 2017
1 parent d2781cd commit 0559a95
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------

- Chg #14321: `yii\widgets\MaskedInput` is now registering its JavaScript `clientOptions` initialization code in head section (DaveFerger)
- Bug #13757: Fixed ambiguous column error in `BaseActiveRecord::refresh()` when the query adds a JOIN by default (cebe, ivankff)
- Bug #14248: `yii\console\controllers\MessageController` no longer outputs colorized filenames when console does not support text colorization (PowerGamer1)
- Bug #14264: Fixed a bug where `yii\log\Logger::calculateTimings()` was not accepting messages with array tokens (bizley)
- Bug #14269: Fixed broken error page when calling an undefined method (cebe)
Expand Down
15 changes: 15 additions & 0 deletions framework/db/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ protected static function findByCondition($condition)
return $query->andWhere($condition);
}

/**
* @inheritdoc
*/
public function refresh()
{
$pk = [];
// disambiguate column names in case ActiveQuery adds a JOIN
foreach($this->getPrimaryKey(true) as $key => $value) {
$pk[static::tableName() . '.' . $key] = $value;
}
/* @var $record BaseActiveRecord */
$record = static::findOne($pk);
return $this->refreshInternal($record);
}

/**
* Updates the whole table using the provided attribute values and conditions.
*
Expand Down
12 changes: 12 additions & 0 deletions framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,18 @@ public function refresh()
{
/* @var $record BaseActiveRecord */
$record = static::findOne($this->getPrimaryKey(true));
return $this->refreshInternal($record);
}

/**
* Repopulates this active record with the latest data from a newly fetched instance.
* @param BaseActiveRecord $record the record to take attributes from.
* @return bool whether refresh was successful.
* @see refresh()
* @since 2.0.13
*/
protected function refreshInternal($record)
{
if ($record === null) {
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/data/ar/CustomerQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
class CustomerQuery extends ActiveQuery
{
public static $joinWithProfile = false;

public function init()
{
if (static::$joinWithProfile) {
$this->innerJoinWith('profile');
}
parent::init();
}

public function active()
{
$this->andWhere('[[status]]=1');
Expand Down
13 changes: 13 additions & 0 deletions tests/framework/db/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use yiiunit\data\ar\Cat;
use yiiunit\data\ar\Category;
use yiiunit\data\ar\Customer;
use yiiunit\data\ar\CustomerQuery;
use yiiunit\data\ar\Document;
use yiiunit\data\ar\Dog;
use yiiunit\data\ar\Item;
Expand All @@ -36,6 +37,7 @@ protected function setUp()
{
parent::setUp();
ActiveRecord::$db = $this->getConnection();
CustomerQuery::$joinWithProfile = false;
}

/**
Expand Down Expand Up @@ -1505,4 +1507,15 @@ public function testNoTablenameReplacement()
$this->assertEquals('Some {{updated}} name', $customer->name);
$this->assertEquals('Some {{%updated}} address', $customer->address);
}

/**
* Ensure no ambiguous colum error occurs if ActiveQuery adds a JOIN
*/
public function testAmbiguousColumnFindOne()
{
CustomerQuery::$joinWithProfile = true;
$model = Customer::findOne(1);
$this->assertTrue($model->refresh());
CustomerQuery::$joinWithProfile = false;
}
}

0 comments on commit 0559a95

Please sign in to comment.