From 43f19e8aee0fe3d10e90f7c34d46340606e979f0 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Thu, 12 Dec 2013 12:07:28 +0100 Subject: [PATCH] use andWhere() in AR::find() to work properly with default scope fixes #1469 --- extensions/yii/elasticsearch/ActiveRecord.php | 4 ++-- framework/CHANGELOG.md | 1 + framework/yii/db/ActiveRecord.php | 14 ++++++++++++++ framework/yii/db/ActiveRecordInterface.php | 14 ++++++++++++++ framework/yii/db/BaseActiveRecord.php | 4 ++-- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/extensions/yii/elasticsearch/ActiveRecord.php b/extensions/yii/elasticsearch/ActiveRecord.php index 3c8b4a8..9cc6549 100644 --- a/extensions/yii/elasticsearch/ActiveRecord.php +++ b/extensions/yii/elasticsearch/ActiveRecord.php @@ -67,7 +67,7 @@ class ActiveRecord extends BaseActiveRecord { $query = static::createQuery(); if (is_array($q)) { - if (count($q) == 1 && (array_key_exists(ActiveRecord::PRIMARY_KEY_NAME, $q))) { + if (count($q) == 1 && (array_key_exists(ActiveRecord::PRIMARY_KEY_NAME, $q)) && $query->where === null) { $pk = $q[ActiveRecord::PRIMARY_KEY_NAME]; if (is_array($pk)) { return static::mget($pk); @@ -75,7 +75,7 @@ class ActiveRecord extends BaseActiveRecord return static::get($pk); } } - return $query->where($q)->one(); + return $query->andWhere($q)->one(); } elseif ($q !== null) { return static::get($q); } diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e1f3899..53067a2 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -8,6 +8,7 @@ Yii Framework 2 Change Log - Enh #1293: Replaced Console::showProgress() with a better approach. See Console::startProgress() for details (cebe) - Enh #1406: DB Schema support for Oracle Database (p0larbeer, qiangxue) - Enh #1437: Added ListView::viewParams (qiangxue) +- Enh #1469: ActiveRecord::find() now works with default conditions (default scope) applied by createQuery (cebe) - New extension #1438: [MongoDB integration](https://github.com/yiisoft/yii2-mongodb) ActiveRecord and Query (klimov-paul) 2.0.0 alpha, December 1, 2013 diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 7f7e6f3..cc67224 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -150,9 +150,23 @@ class ActiveRecord extends BaseActiveRecord /** * Creates an [[ActiveQuery]] instance. + * * This method is called by [[find()]], [[findBySql()]] to start a SELECT query. * You may override this method to return a customized query (e.g. `CustomerQuery` specified * written for querying `Customer` purpose.) + * + * You may also define default conditions that should apply to all queries unless overridden: + * + * ```php + * public static function createQuery() + * { + * return parent::createQuery()->where(['deleted' => false]); + * } + * ``` + * + * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the + * default condition. Using [[Query::where()]] will override the default condition. + * * @return ActiveQuery the newly created [[ActiveQuery]] instance. */ public static function createQuery() diff --git a/framework/yii/db/ActiveRecordInterface.php b/framework/yii/db/ActiveRecordInterface.php index 4965a26..556384b 100644 --- a/framework/yii/db/ActiveRecordInterface.php +++ b/framework/yii/db/ActiveRecordInterface.php @@ -94,9 +94,23 @@ interface ActiveRecordInterface /** * Creates an [[ActiveQueryInterface|ActiveQuery]] instance. + * * This method is called by [[find()]] to start a SELECT query. * You may override this method to return a customized query (e.g. `CustomerQuery` specified * written for querying `Customer` purpose.) + * + * You may also define default conditions that should apply to all queries unless overridden: + * + * ```php + * public static function createQuery() + * { + * return parent::createQuery()->where(['deleted' => false]); + * } + * ``` + * + * Note that all queries should use [[Query::andWhere()]] and [[Query::orWhere()]] to keep the + * default condition. Using [[Query::where()]] will override the default condition. + * * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. */ public static function createQuery(); diff --git a/framework/yii/db/BaseActiveRecord.php b/framework/yii/db/BaseActiveRecord.php index 01e1925..dae7134 100644 --- a/framework/yii/db/BaseActiveRecord.php +++ b/framework/yii/db/BaseActiveRecord.php @@ -114,12 +114,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface { $query = static::createQuery(); if (is_array($q)) { - return $query->where($q)->one(); + return $query->andWhere($q)->one(); } elseif ($q !== null) { // query by primary key $primaryKey = static::primaryKey(); if (isset($primaryKey[0])) { - return $query->where([$primaryKey[0] => $q])->one(); + return $query->andWhere([$primaryKey[0] => $q])->one(); } else { throw new InvalidConfigException(get_called_class() . ' must have a primary key.'); }