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.

FindFirst and placeholder

Is there any neat way to bind variable in findFirst method?

What do you mean? What exactly do you want to do?



12.6k
edited Nov '14

Any thing similar to :

Users::findFirst('name=?0',['John']); //this does not work`.


12.6k

What about Users::findFirst('name=?0 and 'age<?1',['John',30]); //this does not work`.



1.3k
Accepted
answer

use this:

Users::find(array(
                'name = :name: and age < :age:'
                'bind' => array(
                    'name' => 'John',
                    'age' => 30
                )
));
edited Nov '14

or this way:

$builder = new \Phalcon\Mvc\Model\Query\Builder();

$builder
    ->from('App\Models\Users')
    ->where('name = :name: AND age < :age:', array('name' => 'John', 'age' => 30));
    ->getQuery()->execute();

or

User::query()->where("name = :name: and age < :age:")->bind(["name" => 'John', "age"  => 30])->execute();

This also works with findFirst()

use this:

Users::find(array( 'name = :name: and age < :age:' 'bind' => array( 'name' => 'John', 'age' => 30 ) ));

@quasipickle ehm, yes, i have written this already :-) take a look 3 posts above

This also works with findFirst()

use this:

Users::find(array( 'name = :name: and age < :age:' 'bind' => array( 'name' => 'John', 'age' => 30 ) ));



12.6k
edited Nov '14

@SwenZanon

Replace Users::find with Users::findFirst