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!