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

How to use `Models::find`?

$submodules = Modules::find( array( 'moduleid in (:mods:) and parentid = :parentid:', 'bind' => array( 'mods' => '1,2', 'parentid' => 1 ), 'bindtype' => array( 'mods' => Column::BIND_PARAM_STR, 'parentid' => Column::BIND_PARAM_INT ) ) ); I hope the sql that is "select * from modules where moduleid in (1,2) and parentid = 1" but result is not correct.

help plz.



98.9k

Escaping :mods: will produce '1,2' therefore: select * from modules where moduleid in ('1,2') and parentid = 1

You need to use a bound parameter for every value in the "in":

$submodules = Modules::find(array(
    'moduleid in (:firstMod:, :secondMod:) and parentid = :parentId:',
    'bind' => array(        
        'firstMod' => 1,
        'secondMod' => 2,
        'parentId' => 1
    )
));


12.8k

thanks,but i means the SQL sames like moduleid in ( $variable ) and current variable may be come from the form's post or get.

$variable = $this->request->getPost('variable');

$submodules = Modules::find(array( 'moduleid in (:variable:) and parentid = :parentId:', 'bind' => array(
'variable' => $variable, 'parentId' => 1 ) ));