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

Using namespace & models in services

config/loader.php

$loader->registerNamespaces(array(
    'Models\Entities' => dirname(__DIR__) . '/models/Entities/',
    'Models\Services' => dirname(__DIR__) . '/models/Services/',
    'Models\Repositories' => dirname(__DIR__) . '/models/Repositories/'
))

$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,
        $config->application->modelsDir.'Entities/',
        $config->application->modelsDir.'Repositories/',
        $config->application->modelsDir.'Services/',
    )
);

$loader->register();

models/Entities/Admins.php

namespace Models\Entities;

use Phalcon\Mvc\Model\Validator\Email as Email;

class Admins extends \Phalcon\Mvc\Model
{...}

models/Servies/Auth.php

namespace Models\Services;

class Auth extends \Phalcon\Mvc\User\Component {

    public function login($username, $password){
        ....
        $result = Models\Entities\Admins::find($parameters);
        ...
    }

Error:

Fatal error: Class 'Models\Services\Models\Entities\Admins' not found in /webroot/app/models/Services/Auth.php on line 55

What is the correct way to use model in services ?



1.0k
Accepted
answer
edited Dec '14

Sorry, I missed a '\'

$result = \Models\Entities\Admins::find($parameters);

it works~

edited Dec '14

You can actually name only op directory of your namespace like this:

$loader->registerNamespaces(array(
    // ... 
    'Library' => __DIR__ . '/../library/',
));

Then you can reach what's under that directory.

$user = new \Library\Models\Users();