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

Unknown model or alias 'ErrorLogHistory' (11), when preparing... making me crazy

Hi guys,

I'm trying to join two tables by using the createBuilder, but I keep getting this error:

Unknown model or alias 'ErrorLogHistory' (11), when preparing: SELECT * FROM [m:ErrorLogHistory], [m:ErrorLog] INNER JOIN [m:ErrorLog] ON ErrorLogHistory.log = m:ErrorLog.id

QueryBuilder code is as follows:

<?php
$res = $this->modelsManager->createBuilder()
    ->addFrom("m:ErrorLogHistory")
    ->addFrom("m:ErrorLog")
    ->columns("*")
    ->innerJoin("m:ErrorLog", "m:ErrorLogHistory.log = m:ErrorLog.id")
    ->getQuery()
    ->execute();

Services:

<?php
$di->setShared('modelsManager', function () {
    $modelsManager = new ModelManager();
    $modelsManager->registerNamespaceAlias("m", "\\Rugbrod\TrackPHP\API\Models");
    return $modelsManager;
});

Relationship: ErrorLog:

<?php
public function initialize()
{
     $this->hasMany('id', 'Rugbrod\TrackPHP\API\Models\ErrorLogHistory', 'log', ['alias' => 'ErrorLogHistory']);    
}

ErrorLogHistory:

<?php
public function initialize()
{
     $this->belongsTo('log', __NAMESPACE__.'\\ErrorLog', 'id', ['alias' => 'ErrorLog']);
 }

Phalcon version : 3.3.1 OS: Ubuntu 16.04 PHP Version: 7.1.13

Any idea please?

Hi @ahmad you can use alias in your builder. I've never seen your syntax m:ErrorLogHistory I don't know if works or not

use Rugbrod\TrackPHP\API\Models\ErrorLogHistory;
use Rugbrod\TrackPHP\API\Models\ErrorLog;

$res = $this->modelsManager->createBuilder()
    ->from([
        "history" => ErrorLogHistory::class,
        "log" => ErrorLog::class
    ])
    ->columns("*") //optional
    ->innerJoin(ErrorLog::class, "history.log = log.id")
    ->getQuery()
    ->execute();

Good luck