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

Using Model Manager to do Custom Query while inside a Model Function ?

I have user model in where i need to select some other related models, however my query is not a standard one, so i dont think i can use find() or query:: to get the result, so i am thinking to use "modelmanager" and its phql to do the job, however i get empty set as result why is that

SQL Query is

SELECT title FROM var_religion WHERE (15 & (1 << (id - 1))) ORDER BY id DESC

however when use following function insde user model it doesnt seems to be working any idea ?


         public function partner(){

        //$p_religions = VarReligion::find(
        //    array(
         //        "conditions" => " (:var: & (1 << (id - 1))) ",
         //        "bind"       => array("var"=> $this->religion),
         //        "order"     => "id DESC"
         //    )
         //);
         $phql = "SELECT title FROM var_religion WHERE (15 & (1 << (id - 1))) ORDER BY id DESC";
         $p_religions = $this->manager->executeQuery($phql);
         var_dump($p_religions);
        }

i get no output why ?? how do i do this query ?

According to this documentation about raw query

You can do:

public function partner() {
    $sql = "SELECT title FROM var_religion WHERE (15 & (1 << (id - 1))) ORDER BY id DESC";

    $yourModel = new YourModel();

    return new Resultset(null, $yourModel, $yourModel->getReadConnection()->query($sql));
}


13.4k
edited Nov '15

i get error as

Fatal error: Cannot instantiate abstract class Phalcon\Mvc\Model\Resultset

then i modified the code little bit and it worked perfectly had to add the resultset type as Simple

\Phalcon\Mvc\Model\Resultset\Simple

According to this documentation about raw query

You can do:

public function partner() {
   $sql = "SELECT title FROM var_religion WHERE (15 & (1 << (id - 1))) ORDER BY id DESC";

   $yourModel = new YourModel();

  return new Resultset(null, $yourModel, $yourModel->getReadConnection()->query($sql));
}

Sorry I forgot to mention that you need put

use \Phalcon\Mvc\Model\Resultset\Simple as Resultset;

at the top of code.