S
S
Sergey Erin2019-11-15 21:03:27
Yii
Sergey Erin, 2019-11-15 21:03:27

How to translate a query into SQL format?

Good day! I'm just starting with Yii2, don't judge too harshly. I'm trying to deal with Active Record. Tell me how to execute this query directly with sql code. And not through Active Record. Thanks in advance!

$geoNames = GeoNameTable::find()
    ->select([
        'geoname.*',
        'geoname2.geonameid as regionGeonameid',
        'geoname2.name as regionName',
        'geoname2.latitude as regionLatitude',
        'geoname2.longitude as regionLongitude',
        'geoname2.fclass as regionFclass',
        'geoname2.fcode as regionFcode',
        'geoname2.country as regionCountry',
        'geoname2.admin1 as regionAdmin1',
        'geoname2.admin2 as regionAdmin2',
        'geoname2.admin3 as regionAdmin3',
        'geoname2.admin4 as regionAdmin4',
        'geoname2.population as regionPopulation',
        'geoname2.elevation as regionElevation',
        'geoname2.gtopo30 as regionGtopo30',
        'geoname2.timezone as regionTimezone'
    ])
    ->with('alternateNames')
    ->with('countryInfo')
    ->join('LEFT JOIN', GeoNameTable::tableName() . ' as geoname2', 'geoname2.admin1 = geoname.admin1')
    ->where([
        'geoname.fclass' => 'P',
        'geoname2.fcode' => 'ADM1'
    ]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-11-15
@artalexs

Good evening.
You can use this .
You can see what query ActiveRecord generates like this

$query = Orders::find()->joinWith('items');

print_r($query->createCommand()->getRawSql());

As a result, you will get something like
SELECT `orders`.* FROM `orders` LEFT JOIN `order_items` ON `orders`.`id` = `order_items`.`order_id`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question