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

How I can alias namespace for model in PHQL?

I have a model class: App\Frontend\Models\Users, when I use PHQL, the statement is too long! I

$phql = 'select u.id  from App\Frontend\Models\Users  as u';

Then I found a method from API Documnet:

modelsManager->registerNamespaceAlias( $alias , $namespece);

but can someone tell me how to use it? I can't find any information about it from the document!

I have tried the follow statements, all of them trigger class not found error:

$this->modelsManager->registerNamespaceAlias('ns','App\Frontend\Models');
$phql = 'select u.id  from ns\Users  as u';
$this->modelsManager->registerNamespaceAlias('users','App\Frontend\Models\Users');
$phql = 'select u.id  from users  as u';

here is an example of Alias in Phql:

        $foo = $this->modelsManager->createBuilder()
            ->columns('m.*, g.name As gate_name')
            ->from(['m' => 'App\Model\Merchants'])
            ->join('App\Model\Gateways', 'm.fk_gateway_id = g.id', 'g')
            ->orderBy('m.id')
            ->where('m.fk_member_id = :memberID:', ['memberID' => $this->auth->getIdentity()['id']]);

check this link:

https://docs.phalcon.io/en/latest/reference/phql.html#creating-queries-using-the-query-builder



3.5k
edited Mar '15

No. I's not my need,thanks. In your example, every Builder Instance must create with a alias part. So , 10 Builders need 10 times

From(['alias'=>'The\Full\Classname']).

I think it's so long so bored.

Why not register a model class alias just once time then use it in anywhere?

here is an example of Alias in Phql:

      $foo = $this->modelsManager->createBuilder()
           ->columns('m.*, g.name As gate_name')
          ->from(['m' => 'App\Model\Merchants'])
           ->join('App\Model\Gateways', 'm.fk_gateway_id = g.id', 'g')
           ->orderBy('m.id')
          ->where('m.fk_member_id = :memberID:', ['memberID' => $this->auth->getIdentity()['id']]);

check this link:

https://docs.phalcon.io/en/latest/reference/phql.html#creating-queries-using-the-query-builder



3.5k

In fact, I want to know the function of modelsManager->registerNamespaceAlias( $alias , $namespece);



5.2k
edited Mar '15

I have the same question.

// wrong: throws exception :
// Fatal error: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Model 'PrintTask' could not be loaded' in ...
$this->modelsManager->registerNamespaceAlias('PrintTask','\Apps\Prints\Model\PrintTask');
$phql  = "SELECT PrintTask.id AS id FROM PrintTask";
$rows = $this->modelsManager->executeQuery($phql);

// right: but not elegant 
$phql  = "SELECT \Apps\Prints\Model\PrintTask.id AS id FROM \Apps\Prints\Model\PrintTask";  
$rows = $this->modelsManager->executeQuery($phql);

the problem is $this->modelsManager->registerNamespaceAlias didn't work! need help @phalcon

I don't know but using namespace in Phalcon 2.0 didn't work now :(



473
edited May '15

@neo20

$phql  = "SELECT p.id AS id FROM \Apps\Prints\Model\PrintTask AS p";


3.5k

@Andres Gutierrez

Thanks.