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

SubQuery column

Hi, i got a problem in my query with phalcon 2.0.2 subquery in column is not working..

Error

PhalconException: Syntax error, unexpected token SELECT, near to ' count(id_midias)

Query

$phl = "SELECT m.,mg., (SELECT id_midias from \Apps\Models\MidiasXGalerias where id_galerias = mg.id) as total FROM \Apps\Models\MidiasGalerias AS mg LEFT JOIN \Apps\Models\Midias AS m ON m.id = mg.id_midias WHERE mg.id IN (SELECT id_midias from \Apps\Models\MidiasXGalerias where id_galerias = mg.id), mg.tipo = 'midia'";

I am having the same, It seems phalcon does not allow to have subquery in coulmn selection. Reported it on https://github.com/phalcon/cphalcon/issues/10453



17.7k

using PDO(Postgres or Mysql... ) you can to make subquery

edited Jun '15

I have executed this query as raw sql and get resultset with simple resultset type that work very well for me.

        // Base model
          $bm = new BaseModel();

          // Execute the query
         $rs = new Phalcon\Mvc\Model\Resultset\Simple(null, $bm, $bm->getReadConnection()->query($sql));