Hi, is this possible to use models in components without registering and using (model) namespaces in controllers?
|
May '14 |
6 |
1427 |
0 |
//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(...);
}
}
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)
<?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();
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?