0
votes

I am trying to create a query like

SELECT *
FROM course
where course.id IN(
    SELECT DISTINCT(course_id)
FROM course_timing tim
WHERE tim.date >= CURDATE()
    )

I have set the model manger

 $di->set('modelsManager', function() {
      return new Phalcon\Mvc\Model\Manager();
    });

But when i try to use

       $phql = "SELECT * FROM CourseTiming";
       $rows = $this->modelsManager->executeQuery($phql);

I am getting

Model 'CourseTiming' could not be loaded

Or is there any way to run the above query using Model::find etc

1
Do you have CourseTiming Model? - jodator

1 Answers

5
votes

PHQL uses models classes (not db tables names) in the query, so you should create CourseTiming model to access your db table via PHQL:

<?php

class CourseTiming extends Phalcon\Mvc\Model
{
    public $id;
    ....

    public function getSource()
    {
        return 'course_timing'; // name of db table here
    }
}

Don't forget to register models directory in the Phalcon loader:

$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    'pathToModelsDir',
    ...
));
$loader->register();

Another way is to access db via Phalcon PDO (you don't need to create CourseTiming model in this case).

index.php:

$di->setShared('db', function() {
    $db = new Phalcon\Db\Adapter\Pdo\Mysql(array(
        'host'     => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'dbname'   => 'my_db',
    ));
    return $db;
});

controller:

$records = $this->db->fetchAll("SELECT * FROM course_timing", Phalcon\Db::FETCH_ASSOC);