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

query inside model with dynamic condition

hi all developer i have query like this ini zend framework 1

function atDaftarJoinPeran($peran_id,$modul_id=null,$parent=null,$enable=null,$app_id=null) {
    $s = $this->select();
            (cr_hak_akses.MODUL_ID=cr_menu.MODUL_ID or cr_hak_akses.MODUL_ID=\'all\') and
            (cr_hak_akses.KONTROLER_ID=cr_menu.KONTROLER_ID or cr_hak_akses.KONTROLER_ID=\'all\') and
            (cr_hak_akses.AKSI_ID=cr_menu.AKSI_ID or cr_hak_akses.AKSI_ID=\'all\')
    if ($parent) {
        $s->where('PARENT_MENU_ID = ?',$parent);
    } else {
        $s->where('PARENT_MENU_ID IS NULL');
    if ($modul_id) {
    if ($enable) {
    if ($app_id) {

    $rs = $this->fetchAll($s);
    return $rs;

how can i convert that query into phalcon and place it in model not in controller.

in zend i can make query with multi condition simply by inject $s->where clause or $s->join before query execution (fetch).

thanks before

See https://docs.phalcon.io/en/latest/reference/phql.html#creating-queries-using-the-query-builder


$s = $this->getModelsManager()->createBuilder();

And use $s->andWhere()


thanks for the answer,

i mean how can i split into multiple statement, not in 1 statement

this is single statement and work

        $user = $this->modelsManager->createBuilder()
            ->where('user_aktif = :x:', array('x' => 1))
        foreach ($user as $x) {
            echo $x->user."<br>";

this is multi statement and doesnt work

        //this is not working
        $user = $this->modelsManager->createBuilder()->from('Sistem\Model\CrUser');
        $user->where('user_aktif = :x:', array('x' => 1));
        foreach ($user as $x) {
            echo $x->user."<br>";

how can i make multi statement query builder work?

thanks before


ok i make it work

        $builder = $this->modelsManager->createBuilder();
        $builder->where('user_aktif = :x:', array('x' => 1));
        $builder->orWhere('peran_id = :y:',array('y'=>'opr'));

        $user = $builder->getQuery()->execute();
        foreach ($user as $x) {
            echo $x->user."<br>";

before "from" must splitted