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

Several Models

I got index.php, Models/empresas.php

on index->

try {

$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    'models/'
))->register();

$di = new \Phalcon\DI\FactoryDefault();

//Set up the database service
$di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "username",
        "password" => "password",
        "dbname" => "db"
    ));
});

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

$app->setDI($di);

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

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

    $data = array();
    foreach ($empresas as $empresa) {
        $data[] = array(
            'ID' => $empresa->ID,
            'Nombre' => $empresa->Nombre,
        );
    }

    echo json_encode($data);
});

//Retrive all semanas from empresa
$app->get('/api/empresas/suc/{suc_id}/year/{year}/week/{week}', function($suc_id,$year,$week) use ($app) {

    $phql = "SELECT * FROM sec_configuracion WHERE year = :year: AND week = :week:";

    $empresas = $app->modelsManager->executeQuery($phql);

    $data = array();
    foreach ($empresas as $k => $v) {
        $data[] = array(
            'ID' => $empresa->ID,
            'Nombre' => $empresa->Nombre,
        );
    }

    echo json_encode($data);
});

$app->handle();

} catch(\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }

on Models at empresas.php

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

class secconfiguracion extends Model
{
    public function getSource()
    {
        return "sec_configuracion";
    }

    public function validation()
    {
        $this->validate(new Uniqueness(
            array(
                "field"   => "ID",
                "message" => "El id de 'Configuración' debe ser único"
            )
        ));

        if ($this->validationHasFailed() == true) {
            return false;
        }
    }
}

class empresas extends Model
{

    public function validation()
    {
        $this->validate(new Uniqueness(
            array(
                "field" => "ID",
                "message" => "El id de la empresa debe ser único"
            )
        ));

        if ($this->validationHasFailed() == true) {
            return false;
        }
    }

}

When I acces to -> https://www.sys21.com.mx/lanomina/api/empresas/

I get -> <code>[{"ID":"1","Nombre":"Del Campo"},{"ID":"2","Nombre":"Rodriguez"}]</code> but when I access to...

https://www.sys21.com.mx/lanomina/api/empresas/suc/1/year/2015/week/7

I get -> <code>PhalconException: Model 'sec_configuracion' could not be loaded</code>



560
Accepted
answer
edited Apr '15

The answere is very simple I was using just one file for all models, the framework was specting 1 file ofr each model.

A user at IRC chat explained this to me :D

PS: thanks @Gevox