column() public méthode

Executes the query and returns the first column of the result.
public column ( Connection $db = null ) : array
$db Connection the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used.
Résultat array the first column of the query result. An empty array is returned if the query results in nothing.
Exemple #1
0
 public static function getFollowTagIdsByUid($uid)
 {
     //        $sql = "select tag_id from tag_follow where author_id=:author_id";
     //        $tag_ids = Yii::$app->db->createCommand($sql, [
     //            ':author_id' => $uid
     //        ])->queryColumn();
     //        return $tag_ids;
     $query = new Query();
     $query->select('tag_id')->from('tag_follow')->where(['author_id' => $uid]);
     return $query->column();
 }
 public static function find($module, $params = [], $page = 30)
 {
     $relation = ['country' => ['translations'], 'city' => ['country', 'country.translations', 'airports', 'airports.translations', 'translations'], 'direction' => ['iata', 'from', 'from.translations', 'from.country', 'from.country.translations', 'to', 'to.translations', 'to.country', 'to.country.translations'], 'airport' => ['translations', 'country', 'country.translations', 'city', 'city.translations'], 'airline' => []];
     if (!empty($params['relation'])) {
         $relation = array_merge($relation, $params['relation']);
         unset($params['relation']);
     }
     $model = 'common\\models\\Received' . ucfirst($module);
     if (!class_exists($model)) {
         throw new ErrorException('no module > ' . $module);
     }
     $query = new Query();
     $query->select(["{$module}.id"])->from([$module => "received_{$module}"]);
     if ($module == 'direction') {
         $query->addSelect(["{$module}.popularity"]);
         $query->orderBy(['direction.popularity' => SORT_DESC]);
         $query->groupBy(['direction.city_to_code', 'direction.city_from_code']);
     }
     foreach ($params as $key => $value) {
         $postfix = '';
         if ($key == 'from' || $key == 'to') {
             $postfix = "_{$key}";
             $key = 'city';
         }
         if (is_array($value) && $value) {
             $query->leftJoin([$key . $postfix => "received_{$key}"], "{$module}.{$key}{$postfix}_code = {$key}{$postfix}.code");
             foreach ($value as $_key => $_value) {
                 if (is_array($_value) && $_value) {
                     $query->leftJoin([$_key . $postfix => "received_{$_key}"], "{$key}{$postfix}.{$_key}_code = {$_key}{$postfix}.code");
                     foreach ($_value as $__key => $__value) {
                         $query->andFilterWhere(["{$_key}{$postfix}.{$__key}" => $__value]);
                     }
                 } else {
                     $query->andFilterWhere(["{$key}{$postfix}.{$_key}" => $_value]);
                 }
             }
         } else {
             $query->andFilterWhere(["{$module}.{$key}{$postfix}" => $value]);
         }
     }
     $total = $query->count();
     $id = $query->column();
     if ($page > 1) {
         if (!empty($_GET['page'])) {
             $_page = $_GET['page'];
         } else {
             $_page = 0;
         }
         $id = array_slice($id, $_page > 1 ? ($_page - 1) * $page : 0, $page);
         //            VarDumper::dump($id);
     }
     /** @var $model \yii\db\ActiveRecord */
     $query = $model::find()->where(['id' => $id])->asArray();
     if ($module == 'direction') {
         $query->orderBy(['received_direction.popularity' => SORT_DESC]);
     }
     $query->with($relation[$module]);
     if ($page == 1) {
         return $query->all();
     }
     $pagination = new Pagination(['totalCount' => $total, 'pageSize' => $page, 'pageSizeParam' => false]);
     if ($page == 1) {
         $page = 0;
     }
     $query->offset(0)->limit($page);
     return [$query->all(), $pagination];
 }
Exemple #3
0
 private function loadMessages($className)
 {
     if (!is_subclass_of($className, \yii\db\ActiveRecord::className())) {
         throw new \Exception("'{$className}' does not extend \\yii\\db\\ActiveRecord.");
     }
     $attributeNames = $this->getTranslatableAttributes($className);
     $messages = [];
     foreach ($attributeNames as $attributeName) {
         $query = new Query();
         $query->select($attributeName)->distinct()->from($className::tableName());
         $messages = array_merge($messages, $query->column());
     }
     return $messages;
 }