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

Model PHQL Query error, model could not be loaded

Hi i am new for phalcon and i really like it so far and i am trying to learn it step by step, but i get an error i can't figure out. i'm try to run a simple select query on my table but i am getting this error

Phalcon\Mvc\Model\Exception: Model 'pictures' could not be loaded
File=/vagrant/app/models/Gallery.php
Line=31
#0 [internal function]: Phalcon\Mvc\Model\Manager->load('pictures') #1 [internal function]: Phalcon\Mvc\Model\Query->_prepareSelect() #2 [internal function]: Phalcon\Mvc\Model\Query->parse() #3 /vagrant/app/models/Gallery.php(31): Phalcon\Mvc\Model\Query->execute() #4 /vagrant/app/controllers/GalleryController.php(27): Gallery->get_pictures(Array) #5 /vagrant/app/controllers/GalleryController.php(9): galleryController->id() #6 [internal function]: galleryController->indexAction() #7 [internal function]: Phalcon\Dispatcher->dispatch() #8 /vagrant/public/index.php(68): Phalcon\Mvc\Application->handle() #9 {main}

my model is.

<?php
use Phalcon\Mvc\Model,
    Phalcon\Mvc\Model\Query;

class Gallery extends Phalcon\Mvc\Model
{
    public function getSource()
    {
        return 'pictures';
    }

    public function get_pictures($options = array())
    {
        $sql  = "SELECT p.id, p.filename ";
        $sql .= "FROM pictures p WHERE ";
        $sql .= "p.status = 'active' ";

         $query = new Phalcon\Mvc\Model\Query($sql, $this->getDI());
        return $query->execute();

    }
}

Before the code above have i tried with alias as seen at the top. Any idea why it does this?



5.7k

When writing queries using PHQL, it doesn't use table names in the FROM clause. It actually uses the Model name. The Model name is the name of your Object. In your case, this is "Gallery". Your getSource() return value is how the "Gallery" model knows what table to reference, in your case the table is "pictures". If you change the line php $sql .= "FROM pictures p WHERE "; to php $sql .= "FROM Gallery p WHERE "; It should work

thanks Steven, got stuck for an hours until i find this post, this needs to be mentioned more!

While Phalcon is a great framework, it has been really challenging to find good documentation. Issues like this are not easy to solve and not good documentation makes it hard. For example, the first place this kind of requirement can be mentioned would be the main documentation: https://docs.phalcon.io/en/latest/reference/phql.html. Any beginner will first read through that page and have no clue of how to solve this issue

yes, Steven you are a god in Phalcon querys!

thanks Steven, got stuck for an hours until i find this post, this needs to be mentioned more!