Commit 7b13c370 by Carsten Brandt

fixes #3681, disambiguate PK in findOne and findAll

parent 48f6e380
...@@ -41,6 +41,7 @@ Yii Framework 2 Change Log ...@@ -41,6 +41,7 @@ Yii Framework 2 Change Log
- Bug #3578: Fixed postgreSQL column type detection, added missing types (MDMunir, cebe) - Bug #3578: Fixed postgreSQL column type detection, added missing types (MDMunir, cebe)
- Bug #3591: Fixed incomplete obsolete filling in i18n `MessageController::saveMessagesToDb()` (advsm) - Bug #3591: Fixed incomplete obsolete filling in i18n `MessageController::saveMessagesToDb()` (advsm)
- Bug #3601: Fixed the bug that the refresh URL was not generated correctly by `Captcha` (qiangxue, klevron) - Bug #3601: Fixed the bug that the refresh URL was not generated correctly by `Captcha` (qiangxue, klevron)
- Bug #3681: Fixed problem with AR::findOne() when a default scope joins another table so that PK name becomes ambigous (cebe)
- Bug #3715: Fixed the bug that using a custom pager/sorter with `GridView` may generate two different pagers/sorters if the layout configures two pagers/sorters (qiangxue) - Bug #3715: Fixed the bug that using a custom pager/sorter with `GridView` may generate two different pagers/sorters if the layout configures two pagers/sorters (qiangxue)
- Bug #3716: `DynamicModel::validateData()` does not call `validate()` if the `$rules` parameter is empty (qiangxue) - Bug #3716: `DynamicModel::validateData()` does not call `validate()` if the `$rules` parameter is empty (qiangxue)
- Bug #3751: Fixed postgreSQL schema data for enum values, do not add values if there are none (makroxyz) - Bug #3751: Fixed postgreSQL schema data for enum values, do not add values if there are none (makroxyz)
......
...@@ -9,6 +9,7 @@ namespace yii\db; ...@@ -9,6 +9,7 @@ namespace yii\db;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector; use yii\helpers\Inflector;
use yii\helpers\StringHelper; use yii\helpers\StringHelper;
...@@ -149,6 +150,48 @@ class ActiveRecord extends BaseActiveRecord ...@@ -149,6 +150,48 @@ class ActiveRecord extends BaseActiveRecord
} }
/** /**
* @inheritdoc
* @return static ActiveRecord instance matching the condition, or `null` if nothing matches.
*/
public static function findOne($condition)
{
$query = static::find();
if (ArrayHelper::isAssociative($condition)) {
// hash condition
return $query->andWhere($condition)->one();
} else {
// query by primary key
$primaryKey = static::primaryKey();
if (isset($primaryKey[0])) {
return $query->andWhere([static::tableName() . '.' . $primaryKey[0] => $condition])->one();
} else {
throw new InvalidConfigException(get_called_class() . ' must have a primary key.');
}
}
}
/**
* @inheritdoc
* @return static[] an array of ActiveRecord instances, or an empty array if nothing matches.
*/
public static function findAll($condition)
{
$query = static::find();
if (ArrayHelper::isAssociative($condition)) {
// hash condition
return $query->andWhere($condition)->all();
} else {
// query by primary key(s)
$primaryKey = static::primaryKey();
if (isset($primaryKey[0])) {
return $query->andWhere([static::tableName() . '.' . $primaryKey[0] => $condition])->all();
} else {
throw new InvalidConfigException(get_called_class() . ' must have a primary key.');
}
}
}
/**
* Updates the whole table using the provided attribute values and conditions. * Updates the whole table using the provided attribute values and conditions.
* For example, to change the status to be 1 for all customers whose status is 2: * For example, to change the status to be 1 for all customers whose status is 2:
* *
......
...@@ -91,9 +91,10 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -91,9 +91,10 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
*/ */
private $_related = []; private $_related = [];
/** /**
* @inheritdoc * @inheritdoc
* @return static ActiveRecord instance matching the condition, or null if nothing matches. * @return static ActiveRecord instance matching the condition, or `null` if nothing matches.
*/ */
public static function findOne($condition) public static function findOne($condition)
{ {
...@@ -114,7 +115,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -114,7 +115,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
/** /**
* @inheritdoc * @inheritdoc
* @return static[] an array of ActiveRecord instance, or an empty array if nothing matches. * @return static[] an array of ActiveRecord instances, or an empty array if nothing matches.
*/ */
public static function findAll($condition) public static function findAll($condition)
{ {
......
...@@ -101,6 +101,10 @@ class PageCache extends ActionFilter ...@@ -101,6 +101,10 @@ class PageCache extends ActionFilter
*/ */
public $view; public $view;
/**
* @inheritdoc
*/
public function init() public function init()
{ {
parent::init(); parent::init();
...@@ -128,7 +132,6 @@ class PageCache extends ActionFilter ...@@ -128,7 +132,6 @@ class PageCache extends ActionFilter
return true; return true;
} else { } else {
Yii::$app->getResponse()->content = ob_get_clean(); Yii::$app->getResponse()->content = ob_get_clean();
return false; return false;
} }
} }
...@@ -140,7 +143,6 @@ class PageCache extends ActionFilter ...@@ -140,7 +143,6 @@ class PageCache extends ActionFilter
{ {
echo $result; echo $result;
$this->view->endCache(); $this->view->endCache();
return ob_get_clean(); return ob_get_clean();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment