Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Models could not be loaded.

Have been experiencing diffulty loading the models. This is a two part question. Is there a way to ramp up the verbosity of the errors. The model errors are very generic and don't give the same pinpoint accuracy we get just using PHP.

[Sun May 17 08:19:33.508702 2015] [:error] [pid 10568] [client XXXX.XXX.XXX.XXX:51994] PHP Fatal error:  Uncaught exception 'Phalcon\\Mvc\\Model\\Exception' with message 'Model 'organisation' could not be loaded' in /var/www/xxxx/xxxx/index.php:12\nStack trace:\n#0 [internal function]: Phalcon\\Mvc\\Model\\Manager->load('organisation', true)\n#1 [internal function]: Phalcon\\Mvc\\Model\\Query->_prepareSelect()\n#2 [internal function]: Phalcon\\Mvc\\Model\\Query->parse()\n#3 [internal function]: Phalcon\\Mvc\\Model\\Query->execute(NULL, NULL)\n#4 /var/www/xxxx/xxxx/index.php(12): Phalcon\\Mvc\\Model\\Manager->executeQuery('SELECT * FROM o...')\n#5 [internal function]: {closure}()\n#6 /var/www/xxxx/xxxx/index.php(84): Phalcon\\Mvc\\Micro->handle()\n#7 {main}\n  thrown in /var/www/xxxx/xxxx/index.php on line 12

The Model File content is:

use Phalcon\Mvc\Model,
    Phalcon\Mvc\Model\Query;
use Phalcon\Loader;
use Phalcon\Mvc\Micro;
use Phalcon\DI\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;

$di = new FactoryDefault();

//Set up the database service
$di->set('db', function(){
    return new PdoMysql(array(
        "host"      => "######",
        "username"  => "#####",
        "password"  => "######",
        "dbname"    => "######"
    ));
});

//Create and bind the DI to the application
$app = new Micro($di);

class organisation extends Model
{
    public function getSource()
    {
        return 'sav1_organisations';
    }

    public function get_organisations($options = array())
    {
        $sql  = "SELECT * ";
        $sql .= "FROM sav1_organisations";

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

    }
}

// Use Loader() to autoload our model
$loader = new Loader();

$loader->registerDirs(array(
    __DIR__ . '/models/'
))->register();

called using:

//Retrieves all organisations
$app->get('/api/organisation', function() use ($app) {

    $phql = "SELECT * FROM organisation ORDER BY orgName";
    $organisation = $app->modelsManager->executeQuery($phql);

    $data = array();
    foreach ($organisation as $org) {
        $data[] = array(
            'id'    => $org->id,
            'name'  => $org->orgName,
        );
    }

    echo json_encode($data);



});

not sure where where the error is because the response from the server does not give enough detail (is it the password, namespace etc.)

Sorry to be a pain. Still learning. Hopefully this will be all for today :)



36.3k

Hi

Because Modelmnager use object map so you try replace organisation to Organisation and in directory model you need create a models name is Organisation.php

class Organisation extends Model
{
    public function getSource()
    {
        return 'sav1_organisations';
    }

    public function get_organisations($options = array())
    {
        $sql  = "SELECT * ";
        $sql .= "FROM sav1_organisations";

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

    }
}
edited May '15

No luck :(

directory structure is

app/index.php

app/models/Organisation.php

// Use Loader() to autoload our model
$loader = new Loader();

$loader->registerDirs(array(
    __DIR__ . '/models/'
))->register();

is that where the problem is. Maybe DIR . is not resolving correctly? Is there are way to force phalcon to show where it is looking for the model file?



3.1k
Accepted
answer

Turns out the problem was dir. Used teh explicit path to the model start all the back from /var/www/..... etc and that worked.