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.

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.



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



1.7k

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

Thanks