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

Problem with bind types in Phalcon\Mvc\Model\Query\Builder

Hi.

I found a very strange behavior. Table 'templates' in my DB has 2 records.

When I try fetch them using code:

Templates::query()
 ->where('template_status = 1')
->andWhere('delete_datetime IS NULL')
 ->andWhere('id_template_format = :idFormat:', array('idFormat' => 1), array('idFormat' => \Phalcon\Db\Column::BIND_PARAM_STR))    
 ->andWhere('id_template_type = :idType:', array('idType' => 1), array('idType' => \Phalcon\Db\Column::BIND_PARAM_INT))
 ->execute();

I get 2 records - correctly.

However, if I use this in Templates model:

$this->getModelsManager()->createBuilder()
                ->from('ApiMerge\Models\VproducterTemplates')
                ->andWhere('id_template_format = :idFormat:', array('idFormat' => 1), array('idFormat' => \Phalcon\Db\Column::BIND_PARAM_INT))    
                ->andWhere('id_template_type = :idType:', array('idType' => 1), array('idType' => \Phalcon\Db\Column::BIND_PARAM_STR))
                ->getQuery()
                ->execute();

I don't get any records.

Furthermore, when I use:

(...)
->andWhere('id_template_format = :idFormat:', array('idFormat' => 1), array('idFormat' => \Phalcon\Db\Column::BIND_PARAM_STR))    
 ->andWhere('id_template_type = :idType:', array('idType' => 1), array('idType' => \Phalcon\Db\Column::BIND_PARAM_STR))
(...)

I get only 1 record.

I did not find any information about this type of error. I'll be very grateful for any help.

When posting, you need a blank space between text, and your 3 backticks in order for the code formatting to kick in. Adding "php" after the backticks enables syntax highlighting as well.

To respond to your question: I've never used the query builder like this, but I do notice in your second code block you don't call the where() method, just the andWhere(). Also, you call getQuery() before execute() in the second code block. Might that be causing some issue?

My real question for you though, is why are you using the query builder when you could just be using the ORM much more easily? For example, I believe your query could be rewritten like:

$Templates = \ApiMerge\Models\VproducerTemplates::find([
    'conditions'=>'template_status = 1 AND delete_datetime IS NULL AND id_template_format = :format: AND id_template_type = :type:',
    'bind'=>[
        'format'=>1,
        'type'=>1
    ]
);

To me, this seems simpler to read, and less hassle to write.



3.8k

I make quite sophisticated API. In order to automate certain tasks (eg. Reading parameter sequence responsible for sorting, limit, etc.) I have to get objects \ Phalcon \ Mvc \ Model \ Complex and \ Phalcon \ Mvc \ Model \ Row. They contain fields corresponding to the specified models.

It is interesting that the self :: query () called in the model returns a properly working with bind types.

I think that this is a bug in the Phalcon Framework.