Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 ?



42.3k
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.