use andWhere() in AR::find() to work properly with default scope

fixes #1469
This commit is contained in:
Carsten Brandt
2013-12-12 12:07:28 +01:00
parent 3fba6dc314
commit 43f19e8aee
5 changed files with 33 additions and 4 deletions

View File

@@ -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()

View File

@@ -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();

View File

@@ -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.');
}