protected static findByCondition ( mixed $condition ) : yii\db\ActiveQueryInterface | ||
$condition | mixed | please refer to [[findOne()]] for the explanation of this parameter |
리턴 | yii\db\ActiveQueryInterface | the newly created [[ActiveQueryInterface|ActiveQuery]] instance. |
protected static function findByCondition($condition)
{
$query = static::find();
if (!ArrayHelper::isAssociative($condition)) {
// query by primary key
$primaryKey = static::primaryKey();
if (isset($primaryKey[0])) {
$pk = $primaryKey[0];
if (!empty($query->join) || !empty($query->joinWith)) {
$pk = static::tableName() . '.' . $pk;
}
$condition = [$pk => $condition];
} else {
throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.');
}
}
return $query->andWhere($condition);
}
/** * 查找单个对象记录,支持主键缓存获取 * @param mixed $condition * @return mixed|null|static */ public static function findByCondition($condition) { if (isset($condition[static::$pk])) { $cache_key = self::getCacheKey($condition[static::$pk], false); Yii::trace('from cache object:' . $cache_key, __METHOD__); if (self::allowFromCache($condition)) { $row = Yii::$app->cache->get($cache_key); if ($row) { return $row; } } } $row = parent::findByCondition($condition); if ($row && isset($condition[static::$pk]) && self::allowFromCache($condition)) { if (Yii::$app->cache->exists($cache_key)) { Yii::$app->cache->set($cache_key, $row, Yii::$app->params['ttl']); } else { Yii::$app->cache->add($cache_key, $row, Yii::$app->params['ttl']); } } return $row; }