/** * Finds ActiveRecord instance(s) by the given condition. * This method is internally called by {@see \rock\db\ActiveRecord::findOne()} * and {@see \rock\db\ActiveRecord::findAll()}. * * @param mixed $condition please refer to {@see \rock\db\ActiveRecord::findOne()} for the explanation of this parameter * @return ActiveQueryInterface * @throws DbException if there is no primary key defined * @internal */ 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 DbException('"' . get_called_class() . '" must have a primary key.'); } } return $query->andWhere($condition); }