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 to Use Models name instead of full path in Query?

use parking\Models\Rescuer as Rescuer;

.
.
.
$data = Bill::query()
            ->columns('parking\Models\Rescuer.id as rescuerId,parking\Models\Rescuer.cityId,parking\Models\Rescuer.name,parking\Models\Bill.number')
            ->leftJoin("parking\\Models\\Rescuer",
                       "parking\\Models\\Rescuer.id = parking\\Models\\Bill.rescuerId")
            ->where("number = :number:")
            ->bind(array("number" => $number))
            ->limit(1)
            ->execute()->toArray();

When I try to write Models name in Query Without Full path,

Phalcon showing Model Not found Message,

Unknown model or alias 'Rescuer' (11), when preparing

And it can not identify models.my Queries are too longو If I want more I use this method.

How to bypass it?

You can't. Php(and either phalcon) don't see your uses in strings. You can use modelsManager's registerNamespaceAlias:

$modelsManager->registerNamespaceAlias('m','parking\Models');

and then just use m:Rescuer

edited Apr '16

You can't. Php(and either phalcon) don't see your uses in strings. You can use modelsManager's registerNamespaceAlias:

$modelsManager->registerNamespaceAlias('m','parking\Models');

and then just use m:Rescuer

how to (create/Access to) $modelsManager Variable? (inside Models/Controllers)

i Registered it into $di

$di->set('modelsManager', function() {
      return new \Phalcon\Mvc\Model\Manager();
 });

But


$this->modelsManager->registerNamespaceAlias(...);

Not affected

Sorry,i haven't enough experience in phalcon :|

edited Apr '16
$di->set('modelsManager', function() {
    $modelsManager = new \Phalcon\Mvc\Model\Manager();
    $modelsManager->registerNamespaceAlias(...);
    return $modelsManager;
});

I'm sorry , But Not working too...


Unknown model or alias 'm:Rescuer' (11), when preparing: SELECT m:Rescuer.id as rescuerId,parking

What phalcon version you are using ? Try to change set to setShared

edited Apr '16

What phalcon version you are using ? Try to change set to setShared

Phalcon Version:

Version 2.0.10
Build Date  Apr 9 2016 16:28:31
Powered by Zephir   Version 0.9.2a-dev

I try it:

$di->setShared('modelsManager', function() {
    $modelsManager = new \Phalcon\Mvc\Model\Manager();
    $modelsManager->registerNamespaceAlias('m','parking\Models');
    return $modelsManager;
});

But not working... :(



145.0k
Accepted
answer
edited Apr '16

Change your code to:

$data = Bill::query()
            ->columns('Rescuer.id as rescuerId,Rescuer.cityId,Rescuer.name,number')
            ->leftJoin("m:Rescuer",null,"Rescuer") // phalcon will figure out condition if you have proper relations
            ->where("number = :number:")
            ->bind(array("number" => $number))
            ->limit(1)
            ->execute()->toArray();

Also ::query() is mostly for creating queries in an object-oriented manner over a single model. While it supports joins etc you should use modelsManager instead beacause it's providing alias for current model which it's not available when using query() so you can't access id from Bill(perhaps using full namespace). So i would recommend to use(perhaps only if you need to select same columns from both models):

$data = $modelsManager->createBuilder() // $modelsManager got from di
            ->columns('Rescuer.id as rescuerId,Rescuer.cityId,Rescuer.name,number')
            ->from(['Bill'=>'m:Bill']) // this way you can access Bill.id above
            ->leftJoin("m:Rescuer",null,"Rescuer") // phalcon will figure out condition if you have proper relations
            ->where("number = :number:")
            ->bind(array("number" => $number))
            ->limit(1)
            ->getQuery()
            ->execute()->toArray();

Also you dont need Rescuer. prefix before column if there is only one column with this name.

edited Apr '16

Change your code to:

$data = Bill::query()
           ->columns('Rescuer.id as rescuerId,Rescuer.cityId,Rescuer.name,number')
           ->leftJoin("m:Rescuer",null,"Rescuer") // phalcon will figure out condition if you have proper relations
           ->where("number = :number:")
         .
         .
         .
         .
         .

Also you dont need Rescuer. prefix before column if there is only one column with this name.

$modelmanager not have bind method in Example2,

Thank you,

But phalcon should use default Model namespace from configuration file!

I wrote this ,just I looking for Basic way to less writing...

$modelmanager not have bind method in Example2,

Oh sorry. Just second parameter in where method is bind arguments.