S
S
Sergey Beloventsev2016-05-10 09:37:41
Yii
Sergey Beloventsev, 2016-05-10 09:37:41

How to issue a query in mySQL in Activerecord?

Actually, there is such a request

SELECT id, name_film, created_at
    FROM `fl_films`
    UNION
    SELECT id, name_serial, created_at
    FROM `fl_serial`
    ORDER BY created_at DESC
    LIMIT 0 , 20

how can I arrange it in ActiveRecord, so that later I can use the links between the models of these tables.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Beloventsev, 2016-05-10
@Sergalas

It is difficult to solve the problem, but so the model is solved

<?php
    
    namespace common\models;
    use Yii;
    
    /**
     * This is the model class for table "fl_actours".
     *
     * @property integer $id
     * @property string $name
     * @property string $biography
     */
    class FilmsFordate extends \yii\db\ActiveRecord
    {
        public static function tableName()
        {
            return 'fl_filmsFordate';
        }
    }?>

Controller
Yii::$app->db->createCommand(" CREATE OR REPLACE VIEW fl_filmsFordate(id,name,created_at) AS  SELECT id, name_film, created_at FROM `fl_films`
    UNION SELECT id, name_serial, created_at FROM `fl_serial` ORDER BY created_at DESC LIMIT 0 , 20");
            $last= FilmsFordate::find()->all();

True, it is necessary to update the connection, and everything works like that. This article helped a lot . Thanks to all.

M
Maxim Fedorov, 2016-05-10
@qonand

use query builder https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question