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

Phalcon Query Builder, Having with IF statement OR alias Statment, dosen't work at all

return DI::getDefault()->getModelsManager()->createBuilder()
                    ->from(array('a'=>'Ativos'))
                    ->innerJoin("Autoriza","a.autoriza = t.id","t")
                    ->columns(array("alt_alt" => "a.altera",
                        "a.dia",
                        "t.*",
                        "filtro" => "if(t.data_aprovar is null or t.data_aprovar = '0000-00-00', a.dia, t.data_aprovar)"
                    ))
                    ->where("t.data_aprovado = '0000-00-00'")
                    ->andWhere("a.usuario $permissao")
                    ->andWhere("t.inativo IS NULL")
                    ->having("filtro <= now()")
                    ->orderBy(array("t.retorna_erro DESC", "filtro ASC"))
                    ->getQuery()->execute();

Return "Syntax error, unexpected token HAVING" I already try to remove alias and check all the tables and fields, but Phalcon doesn't work with if statement in Mysql.

Does anyone knows how to find a solution here??

You do not forget about comparison sign?

->andWhere("a.usuario $permissao")

mysql doc:

IF(expr1,expr2,expr3)

If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used.



3.9k

follows the var '$permissao':

$permissao = " in (15,20,49)";

I do not think that would interfere in something.



3.9k

@Xieyongmin exactly this, but doesn't work at all...