We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

Binding parameters to QueryBuilder

Hi,

I'm trying to bind some parameters with Phalcon\Mvc\Model\Query\Builder::where method.

This is my code:

$builder = $this->modelsManager->createBuilder()
    ->addFrom('\Namespace\Model', 'm')
    ->where('m.id=:id:', array('id' => $id));

But it produces the following error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':id' at line 1

What am I doing wrong?

edited Oct '14

Unfortunately 'where' has no second parameter. To bind values use:

$builder = $this->modelsManager->createBuilder()
    ->addFrom('\Namespace\Model', 'm')
    ->where('m.id=:id:')
    ->getQuery()
    ->execute(array('id' => $id))

Sorry, I forgot to mention that I'm using Phalcon 1.1.0.

In Phalcon 1.1.0 there are two more parameters on where method: http://docs.phalcon.io/en/1.1.0/api/Phalcon_Mvc_Model_Query_Builder.html

It seems that implementation is missing https://github.com/phalcon/cphalcon/blob/1.1.0/ext/mvc/model/query/builder.c#L529 while andWhere (and other) has this implementation



98.8k
Accepted
answer

Hi, I added a fix for this in 1.1.0, can you recompile again please?

Problem solved! :) Thank you @Phalcon