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

Models in components (namespaces)

Hi, is this possible to use models in components without registering and using (model) namespaces in controllers?



22.6k

I'm not sure really what you mean, how would you use a model without registering its namespace? That would make it effectively non-existent.

If you use an auto-loader, you don't need to use namespaces at all unless you want to.

//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    __DIR__ . '/controllers/',
    __DIR__ . '/models/',
))->register();


29.8k
//autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    __DIR__ . '/controllers/',
    __DIR__ . '/models/',
))->register();

// component
$user = Users::findFirst(...);

Result is Fatal error: Class "projectname\component\Users" not found in (component dir)

In controllers models are ok.

Everything has a namespace - unless you declare it, the namespace is just \. So, if you're in a controller with a declared namespace, and you want to use a model without a declared namespace, you need to specify the root namespace:

namespace \MyProject\Controllers;

class UpdateController extends BaseController(){

    public function somethingOrOther(){

        $User = \Users::findFirst(...);
    }
}


29.8k

I've got 'phalcon project simple' from devtools. How to use model in component? $user = Users::findFirst(...); is returning: Fatal error: Class "projectname\component\Users" not found in (component dir)

$user = \Users::findFirst(...); is returning:

Fatal error: Class "Users" not found in (component dir)

I've got no experience with the devtools, so I don't know how it sets things up. Can you post the config & bootstrap file - at least the parts that relate to the autoloader?



29.8k
<?php

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir
    )
);

$loader->registerNamespaces(array(
    'Lib' => $config->application->libraryDir
));

$loader->register();

Can you also please post the entirety of the component file in which you're encountering the error?



29.8k
<?php
namespace Lib\Auth;

use Phalcon\Mvc\User\Component;

class Auth extends Component
{
    public function check()
    {
        $user = Users::findFirst();
    }
}


125.8k
Accepted
answer

So when you put:

namespace Lib\Auth;

At the top of the file, then reference Users, PHP interprets that to mean

$users = Lib\Auth\Users::findFirst();

which is why it's looking in the component directory. What I don't get is why, when you use the root namespace, it doesn't look in your other auto loaded directories. Can you post your Users model?



29.8k

$user = \Users::findFirst(...); is ok, I just forgot to remove namespace from model, sorry and thank you very much