How can i work with postgresql's views in models

Hello…

I created some views on postgresql to make easier the way to do some searches where several tables are related …

I would like to know how I can implement such views in the code to run me …

I’d appreciate your help…

Hi Trydents!

Given a parameterized view created in PostgreSQL like:




CREATE OR REPLACE VIEW vw_rockets as 

 SELECT id,

    rocket_name,

    merlin_qty,

    payload,

    launched_date

   FROM tbl_rocket;


ALTER TABLE vw_rockets

  OWNER TO postgres;

GRANT ALL ON TABLE vw_rockets TO postgres;



To create the model, you may use the excelent Gii tool passing the view name instead of table name and generate the model.

Some ways to access it in Yii2:




<?php

    $rockets = VwRockets::find()->where(['merlin_qty' => 9])->all();


// ... or


    $sql = "

        SELECT *

        FROM vw_rockets 

        WHERE rocket_name ilike 'falcon%'

        ORDER BY launched_date DESC, rocket_name

    ";

    $command = Yii::$app->db->createCommand($sql);

    $rockets = $command->queryAll();

    print_r($rockets);

?>



Salute!

Thanks…

but, if i want to make a search model from that view model a i got an error for no primary key.