cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

ModelsManager can't load model

Open Riu opened this issue 13 years ago • 4 comments

I use modules and namespaces in my app. When I try get my data from db with query builder and models manager I have problem.

Let's declare namespace:

use Someapp\Somemodule\Models\Somemodel as Somemodel;

When I use standard query it's work:

Somemodel::find();

But when I use models manager I have problem ang get msg: The model 'Somemodel' could not be loaded:

$this->modelsManager->createBuilder()->from('Somemodel')->getQuery()->execute();

And now is funny moment:

$this->modelsManager->createBuilder()->from('Someapp\Somemodule\Models\Somemodel')->getQuery()->execute();

It's work :) Well... this is strange and I don't know - it's problem with models manager, namespaces in modules or what?

Riu avatar Feb 05 '13 09:02 Riu

In this case "Somemodel" is local alias. Models Manager doesn't know about it.

lantian avatar Feb 05 '13 11:02 lantian

Hi Riu, this is not a bug, aliases are resolved in compile time, aliases in strings cannot be resolved by PHP, so you need to write the full namespace as it is the real class name

http://php.net/manual/en/language.namespaces.importing.php

ghost avatar Feb 06 '13 02:02 ghost

Well, I understand that, but it's very chic solution and perhaps it should be some information about that in Phalcon docs.

Riu avatar Feb 06 '13 20:02 Riu

good, I'm going to add a note in docs, thanks

ghost avatar Feb 07 '13 23:02 ghost