Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon\Mvc\Model\Manager with namespace model class

Hi,

I try to use the Phalcon\Mvc\Model\Manager in this way :

        $query = $this->modelsManager->createQuery(
                "SELECT 
                    t.KeyName,
                    l.ID,
                    ifnull(tv.Value, '') as Value
                FROM Translation t
                left join TranslationValue tv on tv.TranslationID = t.ID
                left join Locale l on l.ID = tv.LocaleID
                where LocaleID = :id: or
                (tv.Value is null or l.ID is null)");

        $translation = $query->execute(array(

            'id' => $locale->getFirst()->ID
        ));

When the execute method is execute, I'm getting this message :

Model 'Translation' could not be loaded.

The model class Translation is in a namespace => namespace NearbyMe\Backend\Models;

Does the Phalcon\Mvc\Model\Manager can introspect a class within a namespace ?

If a use $Transaction::find() there is no problem, but I don't have my children the way I need them.



1.5k
Accepted
answer
edited Oct '14

Hi!

I've never used the Model Manager yet. But I think you need to add the namespace like so...

<?php

$query = $this->modelsManager->createQuery(/** ... previous code ...*/ FROM My\Namespace\Translation t left join TranslationValue tv /** ... */);


16.7k

That did the trick.

Thank you !

On side note. This is not Phalcon related. It's how namespaces works in PHP. From PHP Docs: "Importing is performed at compile-time, and so does not affect dynamic class, function or constant names." so you can't write:

use NS\ClassName;

$a = 'ClassName';
$b = new $a();

You must write

use NS\ClassName;

$a = 'NS\ClassName';
$b = new $a();