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

Querybuilder Making Dynamic Query Problem

Hi I'm gonna use querybuilder to create dynamic query based on requests and have it on my resultset

                $builder = $this->modelsManager->createBuilder()
                                ->columns($columns)
                                ->from(array('ParentDB'=>'NS\Models\ParentDB'))
                                ->innerJoin('NS\Models\ChildDB','ParentDB.Id=ChildDB.parentId','ChildDB');
                if ( $condition ){
                        $builder->where( $someCondition )->getQuery()->execute( $params );
                }else{
                        $builder->getQuery()->execute();
                }

my goal is creating query dynamiclly for example join other tables if needed or add another where conditions and perhaps params.. should I use PHQL instead?



8.4k

thanks, but it isn't lookalike query builder. and I wanna know why is it like that..! is it a bug or i missused it. then.. should i use PHQL as you did?

how I use it

https://forum.phalcon.io/discussion/8951/multilanguage-i18n-databasemodels



85.5k

i dont know the diffrence to be honest, but raw queries are more readable. I always use models, when there is no cheeky stuff. When it comes to dynamic table name, or when it starts taking me hours to make it work with model, i just go for this method.

I dont think there is any difference, just use what's easy and elegant for you.