M
M
Maxim Grechushnikov2015-05-10 16:06:11
Yii
Maxim Grechushnikov, 2015-05-10 16:06:11

Yii2: How to pull cities by condition?

Got something wrong, guys. there are 2 models. City and Club (there are many clubs in the city. Club 1 to the city)
in short, you need to display all cities from known clubs.
made by the city

public function getClubs(){
    return $this->hasMany(Club::className(), ['city_id'=>'id']);
  }

asking
$_cities = City::find()->joinWith('clubs')->where(['LIKE', 'name_ru', $term])->all();

displays all the cities that fall under the where condition
that I forgot to specify for the join, so that it displays only specific cities of famous clubs?
there are only 3 clubs. in ekb, moscow and petersburg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Grechushnikov, 2015-05-10
@maxyc_webber

$_cities = City::find()->joinWith('clubs', true, 'INNER JOIN')->where(['LIKE', 'net_city.name_ru', $term])->all();

A
Alexey Ivanov, 2015-05-11
@Sphinx123

Two tables connected via hasMany:
a) cart --- Cart
b) goods --- Goods
$result = (new \yii\db\Query())
->select([])
->from([ 'cart'])
->innerJoin(['goods'])
->where(['cart_ip' => $_SERVER['REMOTE_ADDR']])
->andWhere('`cart_good_id` = `goods_id`')
-> groupBy(['cart_good_id'])
->all();
return $this->render('index',['result' => $result]);
<?php foreach ( $result as $row):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question