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

Tutorial 3, Model 'Robots' could not be loaded

Hi, I re-created the Tutorial 3: Creating a Simple REST API and tested it in my local WAMP server, the script works perfectly, but when I upload the files to my server (Where I have Phalcon installed after a complicated process for me lol), I receive this when calling the first API function /robots to retrieve all the robots:

On my WAMP:

[{"id":"1","name":"Robotina"},{"id":"2","name":"Wall-e"}]

On my server:

Fatal error: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Model 'Robots' could not be loaded' in /home/faroing/public_html/api/index.php:28
Stack trace: #0 [internal function]: Phalcon\Mvc\Model\Manager->load('Robots') 
Stack trace: #1 [internal function]: Phalcon\Mvc\Model\Query->_prepareSelect()
Stack trace: #2 [internal function]: Phalcon\Mvc\Model\Query->parse()
Stack trace: #3 [internal function]: Phalcon\Mvc\Model\Query->execute(NULL, NULL)
Stack trace: #4 /home/faroing/public_html/api/index.php(28): Phalcon\Mvc\Model\Manager->executeQuery('SELECT * FROM R...')
Stack trace: #5 [internal function]: {closure}()
Stack trace: #6 /home/faroing/public_html/api/index.php(66): Phalcon\Mvc\Micro->handle()
Stack trace: #7 {main} thrown in /home/faroing/public_html/api/index.php on line 28

Maybe is something wrong with the installation in my server?

Thanks!



58.4k

Hi

You can upload your code??



60
edited Jun '15

The same problem

Fatal error: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Model 'robots' could not be loaded' in C:\xampp\htdocs\index.php:20 Stack trace: #0 [internal function]: Phalcon\Mvc\Model\Manager->load('robots', true) #1 [internal function]: Phalcon\Mvc\Model\Query->_prepareSelect() #2 [internal function]: Phalcon\Mvc\Model\Query->parse() #3 [internal function]: Phalcon\Mvc\Model\Query->execute(NULL, NULL) #4 C:\xampp\htdocs\index.php(20): Phalcon\Mvc\Model\Manager->executeQuery('SELECT * FROM r...') #5 [internal function]: {closure}() #6 C:\xampp\htdocs\index.php(194): Phalcon\Mvc\Micro->handle() #7 {main} thrown in C:\xampp\htdocs\index.php on line 20

Index.php <?php header('Content-Type: text/html; charset=utf-8'); $di = new \Phalcon\DI\FactoryDefault();

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

$app = new \Phalcon\Mvc\Micro($di);

$app->get('/api/robots', function() use ($app) { $phql = "SELECT * FROM robots ORDER BY name"; $robots = $app->modelsManager->executeQuery($phql);

$data = array();
foreach( $robots as $robot){
    $data[] = array(
        'id' => $robot->id,
        'name' => $robot->name,
    );
}

echo json_encode($data);

});

$app->get('/api/robots/search/{name}', function($name) use ($app) {

$phql = "SELECT * FROM robots WHERE name LIKE :name: ORDER BY name";
$robots = $app->modelsManager->executeQuery($phql, array(
    'name' => '%' . $name . '%'
));

$data = array();

foreach ($robots as $robot){
    $data[] = array(
        'id' => $robot->id,
        'name' => $robot->name,
    );
}

echo json_encode($data);

});

$app->get('/api/robots/{id:[0-9]+}', function($id) use ($app) {

$phql = "SELECT * FROM robots WHERE id = :id:";
$robot = $app->modelsManager->executeQuery($phql, array(
    'id' => $id
))->getFirst();

$response = new Phalcon\Http\Response();

if ($robot == false) {
    $response->setJsonContent(array('status' => 'NOT-FOUND'));
} else {
    $response->setJsonContent(array(
        'status' => 'FOUND',
        'data' => array(
            'id' => $robot->id,
            'name' => $robot->name
        )
    ));
}

return $response;

});

$app->post('/api/robots', function() use ($app) {

$robot = $app->request->getJsonRawBody();

$phql = "INSERT INTO robots (name, type, year) VALUES (:name:, :type:, :year:)";

$status = $app->modelsManager->executeQuery($phql, array(
    'name' => $robot->name,
    'type' => $robot->type,
    'year' => $robot->year
));

$response = new Phalcon\Http\Response();

if ($status->success() == true) {

    $response->setStatusCode(201, "Created");

    $robot->id = $status->getModel()->id;

    $response->setJsonContent(array('status' => 'OK', 'data' => $robot));

} else {

    $response->setStatusCode(409, "Conflict");

    $errors = array();
    foreach ($status->getMessages() as $message) {
        $errors[] = $message->getMessage();
    }

    $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors));
}

return $response;

});

$app->put('/api/robots/{id:[0-9]+}', function($id) use($app) {

$robot = $app->request->getJsonRawBody();

$phql = "UPDATE robots SET name = :name:, type = :type:, year = :year: WHERE id = :id:";
$status = $app->modelsManager->executeQuery($phql, array(
    'id' => $id,
    'name' => $robot->name,
    'type' => $robot->type,
    'year' => $robot->year
));

$response = new Phalcon\Http\Response();

if ($status->success() == true) {
    $response->setJsonContent(array('status' => 'OK'));
} else {

    $response->setStatusCode(409, "Conflict");

    $errors = array();
    foreach ($status->getMessages() as $message) {
        $errors[] = $message->getMessage();
    }

    $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors));
}

return $response;

});

$app->delete('/api/robots/{id:[0-9]+}', function($id) use ($app) {

$phql = "DELETE FROM robots WHERE id = :id:";
$status = $app->modelsManager->executeQuery($phql, array(
    'id' => $id
));

$response = new Phalcon\Http\Response();

if ($status->success() == true) {
    $response->setJsonContent(array('status' => 'OK'));
} else {

    $response->setStatusCode(409, "Conflict");

    $errors = array();
    foreach ($status->getMessages() as $message) {
        $errors[] = $message->getMessage();
    }

    $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors));

}

return $response;

});

$app->notFound(function () use ($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders(); echo 'Очень жаль, но такой страницы не существует'; });

$app->handle(); ?>

Model/robots.php

<?php

use Phalcon\Mvc\Model, Phalcon\Mvc\Model\Message, Phalcon\Mvc\Model\Validator\InclusionIn, Phalcon\Mvc\Model\Validator\Uniqueness;

class robots extends Model {

public function validation()
{
    // Тип робота должен быть: droid, mechanical или virtual
    $this->validate(new InclusionIn(
        array(
            "field"  => "type",
            "domain" => array("droid", "mechanical", "virtual")
        )
    ));

    // Имя робота должно быть уникально
    $this->validate(new Uniqueness(
        array(
            "field"   => "name",
            "message" => "Имя робота должно быть уникально"
        )
    ));

    // Год не может быть меньше нулевого
    if ($this->year < 0) {
        $this->appendMessage(new Message("Год не может быть меньше нулевого"));
    }

    // Проверяет, были ли получены какие-либо сообщения при валидации
    if ($this->validationHasFailed() == true) {
        return false;
    }
}

} ?>

I would be grateful for any help!

I had the same issue. Managed to get it work by adding the following on the beginning of the index.php file:

use Phalcon\Http\Response; use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Message; use Phalcon\Mvc\Model\Validator\Uniqueness; use Phalcon\Mvc\Model\Validator\InclusionIn; use Phalcon\Loader; 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" => "localhost", "username" => "root", "password" => "", "dbname" => "robotics" ) ); });

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

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

$app = new Micro($di);



114

Which Index.php file??

I am having the same issue on my server on am application that was working on localhost

I had the same issue. Managed to get it work by adding the following on the beginning of the index.php file:

use Phalcon\Http\Response; use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Message; use Phalcon\Mvc\Model\Validator\Uniqueness; use Phalcon\Mvc\Model\Validator\InclusionIn; use Phalcon\Loader; 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" => "localhost", "username" => "root", "password" => "", "dbname" => "robotics" ) ); });

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

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

$app = new Micro($di);