E
E
Evgeny Startsev2015-06-27 11:52:42
Yii
Evgeny Startsev, 2015-06-27 11:52:42

How to set SQL query in Yii2 via AD?

There is a request like

SELECT n.id, n.name, SUBSTRING(n.`text`, 1, 256 ) AS `text`, cat.id as cat_id, cat.name as cat_name
FROM news n
LEFT JOIN category as cat on (cat.id = n.category_id)
WHERE category_id = 1

I would like it not in its pure form, but through ActiveRecords.
I got it like this
$query = News::find()
      ->select([
            '{{%news}}.`id`',
            '{{%news}}.`name`', 
            'SUBSTRING({{%news}}.`text`, 1, 256 ) AS `text`', 
            '{{%news}}.`date`', 
            '{{%category}}.`id` AS cat_id',
            '{{%category}}.`name` AS `cat_name`'
     			])
      ->leftJoin('{{%category}}', '{{%category}}.id = {{%news}}.category_id')
      ->where($where['where'], $where['arr']);

And another point - how to place this request in the model and not in the controller

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question