We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to make a query based on a database view ?

The standard way to make a query against a database table is to create a model and create a function within it , for example :

<pre> <code> <?php

use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Query;

class Client extends Model {

function lireParCritere($critere) {

    $sSQL = "
            SELECT c.clt_id as clt_id,c.clt_cin_pass,c.clt_nom,c.clt_prenom,c.clt_tel,
            c.clt_adresse,c.clt_comment,CONCAT_WS(
                                                    ' ',
                                                    c.clt_nom,
                                                    c.clt_prenom
                                                ) AS noms 
            FROM client as c WHERE 1 = 1 ";

    if(isset($critere["clt_id"]) && $critere["clt_id"] != "") {
        $sSQL .= "AND c.clt_id = '" . $critere["clt_id"] . "' ";    
    }

    $sSQL .= " ORDER BY noms";

    $query = new Query($sSQL,$this->getDI());

    $ret = $query->execute();

    return $ret;

}

} </code> </pre>

What is the way to make a query against a database view ?



77.7k
Accepted
answer

You can create a model for a view too, but ofc errors will be thrown if you try to insert/update.

phalcon-devtools is also capable of generating models for views.