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

PHQL bind types


$phql =  "
                        select L.id_localite, L.code_postal, L.nom_localite, D.nom_district
                        from Localites as L
                        inner join Districts D
                        on L.district_id = D.id_district
                        where L.code_postal like ':cp:%'
                        order by L.code_postal
                        limit :max:
                        ";

                        $results = $this->modelsManager->executeQuery($phql, array(
                       'cp' => $code_postal,
                       'max' => $max,    ));          

How can i bind types to be interpreted as integer with phql ? It's not well explained in the doc.



4.7k
Accepted
answer

Try using createQuery then bind.


$phql =  "
select L.id_localite, L.code_postal, L.nom_localite, D.nom_district
from Localites as L
inner join Districts D
on L.district_id = D.id_district
where L.code_postal like ':cp:%'
order by L.code_postal
limit :max:
";

$results = $this->modelsManager->createQuery($phql);

$bind = array(
'cp' => $code_postal,
'max' => $max);

$types = array(
'cp' => Phalcon\Db\Column::BIND_PARAM_STR,
'max' => Phalcon\Db\Column::BIND_PARAM_INT
);

$results = $query->execute($bind, $types);

Hi, you can use types as third parameter in executeQuery():

$results = $this->modelsManager->executeQuery($phql, array(
        'cp' => $code_postal,
        'max' => $max,    
    ),
    array(
        'cp' => Phalcon\Db\Column::BIND_PARAM_STR,
        'max' => Phalcon\Db\Column::BIND_PARAM_INT
    );
);

Third paramter is not documented, but it works.



3.1k

Sorry for replying late. It works indeed. It should be added to the doc.

Thanks