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

return DI::getDefault()->getModelsManager()->createBuilder()
                    ->innerJoin("Autoriza","a.autoriza =","t")
                    ->columns(array("alt_alt" => "a.altera",
                        "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"))

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 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.


follows the var '$permissao':

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

I do not think that would interfere in something.


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