Commit 86f06677 by Qiang Xue

Refactored AR findOne() and findAll().

parent df6a9a08
...@@ -150,45 +150,33 @@ class ActiveRecord extends BaseActiveRecord ...@@ -150,45 +150,33 @@ class ActiveRecord extends BaseActiveRecord
} }
/** /**
* @inheritdoc * Finds ActiveRecord instance(s) by the given condition.
* @return static ActiveRecord instance matching the condition, or `null` if nothing matches. * This method is internally called by [[findOne()]] and [[findAll()]].
* @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter
* @param boolean $one whether this method is called by [[findOne()]] or [[findAll()]]
* @return static|static[]
* @throws InvalidConfigException if there is no primary key defined
* @internal
*/ */
public static function findOne($condition) protected static function findByCondition($condition, $one)
{ {
$query = static::find(); $query = static::find();
if (ArrayHelper::isAssociative($condition)) {
// hash condition if (!ArrayHelper::isAssociative($condition)) {
return $query->andWhere($condition)->one();
} else {
// query by primary key // query by primary key
$primaryKey = static::primaryKey(); $primaryKey = static::primaryKey();
if (isset($primaryKey[0])) { if (isset($primaryKey[0])) {
return $query->andWhere([static::tableName() . '.' . $primaryKey[0] => $condition])->one(); $pk = $primaryKey[0];
if (!empty($query->join) || !empty($query->joinWith)) {
$pk = static::tableName() . '.' . $pk;
}
$condition = [$pk => $condition];
} else { } else {
throw new InvalidConfigException(get_called_class() . ' must have a primary key.'); throw new InvalidConfigException(get_called_class() . ' must have a primary key.');
} }
} }
}
/** return $one ? $query->andWhere($condition)->one() : $query->andWhere($condition)->all();
* @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.');
}
}
} }
/** /**
......
...@@ -98,19 +98,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -98,19 +98,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
*/ */
public static function findOne($condition) public static function findOne($condition)
{ {
$query = static::find(); return static::findByCondition($condition, true);
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([$primaryKey[0] => $condition])->one();
} else {
throw new InvalidConfigException(get_called_class() . ' must have a primary key.');
}
}
} }
/** /**
...@@ -119,19 +107,33 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -119,19 +107,33 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
*/ */
public static function findAll($condition) public static function findAll($condition)
{ {
return static::findByCondition($condition, false);
}
/**
* Finds ActiveRecord instance(s) by the given condition.
* This method is internally called by [[findOne()]] and [[findAll()]].
* @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter
* @param boolean $one whether this method is called by [[findOne()]] or [[findAll()]]
* @return static|static[]
* @throws InvalidConfigException if there is no primary key defined
* @internal
*/
protected static function findByCondition($condition, $one)
{
$query = static::find(); $query = static::find();
if (ArrayHelper::isAssociative($condition)) {
// hash condition if (!ArrayHelper::isAssociative($condition)) {
return $query->andWhere($condition)->all(); // query by primary key
} else {
// query by primary key(s)
$primaryKey = static::primaryKey(); $primaryKey = static::primaryKey();
if (isset($primaryKey[0])) { if (isset($primaryKey[0])) {
return $query->andWhere([$primaryKey[0] => $condition])->all(); $condition = [$primaryKey[0] => $condition];
} else { } else {
throw new InvalidConfigException(get_called_class() . ' must have a primary key.'); throw new InvalidConfigException(get_called_class() . ' must have a primary key.');
} }
} }
return $one ? $query->andWhere($condition)->one() : $query->andWhere($condition)->all();
} }
/** /**
......
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