F
F
FunnyDropee2019-07-08 14:56:13
Yii
FunnyDropee, 2019-07-08 14:56:13

How to write SQL query for yii2 framework?

Actually the code itself in PHP, how to make it understandable by the framework.

$sql = "select famelia, imya, otchestvo, data, model from get_cartridge, cartridge, otvetstveniy where otvetstveniy.id = get_cartridge.id_otvet and cartridge.id = get_cartridge.id_get_cartridge";
Попытка сделать экшн и вывод на страницу 

public function actionList()
      {
        $query = new Query;
        $query->select(['famelia','imya','otchestvo','data','model'])->from('`get_cartridge`','`cartridge`','`otvetstveniy`',)->where('`otvetstveniy`.`id` = `get_cartridge`.`id_otvet`');
        $command = $query->createCommand();
        $resp = $command->queryAll();
        return $this->render('list', [
           'resp' => $resp
       ]);
          }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2019-07-08
@FunnyDropee

Cartridge::find()->joinWith('GetCartridge')->joinWith('Otvetstveniy')->all();

Pre- create models Cartridge, GetCartridge, Responsibility and relationships between them .

I
irishmann, 2019-07-08
@irishmann

This is an example of what not to do. You have a framework, so use it 100%. Create models, specify dependencies. Much easier to work. Read documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question