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

Uncaught Exception: Unknown model or alias

so i have this getAvgRatings method on my model "Animes"

    public function getAvgRatings(){
        return $this->modelsManager
        ->createBuilder()
        ->addFrom("\App\Models\Ratings")
        ->columns(['\App\Models\Animes.id','\App\Models\Animes.name','count(Ratings.id) as TotalRatings','format(Avg(Ratings.value))'])
        ->where('Ratings.anime_id = :animeid: And Ratings.anime_id is not Null', ['animeid'=> $this->id ])
        ->groupBy('Ratings.anime_id')->getQuery()->execute();
    }

but whenever i call it i get this error

Uncaught Phalcon\Mvc\Model\Exception: Unknown model or alias '\App\Models\Animes'
edited Nov '17

Model namespaces are implictly treated as full paths, try it without the leading backslash.

Also, you could set up a global alias for model namespaces:

// services.php

$di->setShared('modelsManager',function() use($di) {
    $manager = new ModelManager();
    $manager->setEventsManager($di->get('eventsManager'));
    $manager->registerNamespaceAlias('m','App\Models');
    return $manager;
});
// usage in controllers for eg:
$this->modelsManager ->createBuilder() ->addFrom("m:Ratings")->columns(['m:Animes.id','m:Animes.name','count(m:Ratings.id) as TotalRatings','format(Avg(m:Ratings.value))']) ->where('m:Ratings.anime.id = :animeid: And m:Ratings.anime_id is not Null', ['animeid'=> $this->id ]) ->groupBy('m:Ratings.anime.id')->getQuery()->execute();