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

Models Manager

so i'm trying to use alias but the following code dose not work for some reason , any ideias ?

Services.php

$di['modelsmanager'] = function (){
    $manager = new ModelsManager();

    $manager->registerNamespaceAlias("Users", "Manager\Models\Users");
    $manager->registerNamespaceAlias("Logs", "Manager\Models\Logs");

    return $manager;
};

IndexController


public function testaction()
  {
    $this->view->disable();
    var_dump($this->modelsmanager->getNamespaceAliases());

    $Logs = Logs::query()
    ->columns([
      "Logs.user",
      "Logs.description"
    ])
    ->execute();

    var_dump($Logs);
  }

Results:


array (size=2)
  'Users' => string '\Manager\Models\Users' (length=21)
  'Logs' => string '\Manager\Models\Logs' (length=20)
Unknown model or alias 'Logs' (11), when preparing: SELECT Logs.user, Logs.description FROM [Manager\Models\Logs]
edited Aug '16

It doesn't work like this. This is only NAMESPACE ALIAS, NOT MODEL ALIAS. It should be like this for example:

$manager->registerNamespaceAlias("M", "Manager\Models");
 $Logs = Logs::query()
    ->columns([
      "M:Logs.user",
      "M:Logs.description"
    ])
    ->execute();

But better to use query builder:

$this->modelsManager->createBuilder()
->from(['L'=>'M:Logs'])
->columns(['L.user', 'L.description'])
->getQuery()
->execute();

Also for such a simple queries use ::find()

Also if coulumn name is not repeated between models then you don't need to write anything before it.