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

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?



22.1k
edited Nov '14

Any thing similar to :

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


22.1k

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



2.4k
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 ) ));



22.1k
edited Nov '14

@SwenZanon

Replace Users::find with Users::findFirst