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 with namespaces module app

Hi guys,

currently facing a problem when executing this statement:

    $skills = \Namespace\Models\Employeeskill::query()
            ->leftJoin('\Namespace\Models\Technology', '\Namespace\Models\Employeeskill.technologyId = \Namespace\Models\Technology.id')
            ->where('\Namespace\Models\Employeeskill.employeeId = :userid:', ['userid' => $this->session->get('userid')])
            ->columns([
                '\Namespace\Models\Employeeskill.level',
                '\Namespace\Models\Technology.name'
                ])
            ->orderBy('\Namespace\Models\Technology.name DESC')
            ->execute();

I am getting this error:

Unknown model or alias 'Namespace\Models\Technology' (11), when preparing: SELECT \Namespace\Models\Employeeskill.level, \Namespace\Models\Technology.name FROM [Namespace\Models\Employeeskill] LEFT JOIN [\Namespace\Models\Technology] ON \Namespace\Models\Employeeskill.technologyId = \Namespace\Models\Technology.id WHERE \Namespace\Models\Employeeskill.employeeId = :userid: ORDER BY \Namespace\Models\Technology.name DESC

Namespaces are correct and were working before, except for this statement.

Any ideas?



12.9k

I know that seems to be a quick answer, but....

  • Have you tried removing the leading backslashes \Namespace\Models\ModelName to Namespace\Models\ModelName?
  • Are you sure that all the Namespaces and Model names are written correctly?
edited Jun '17

Yeah I tried removing the leading backslashes, won't work either.

And I tripled checked if everything is correctly written, it is.

Also I recognized that my model relationships aren't working too...... setup some hasMany etc. doesn't work -.-

Model Location:

    $this->belongsTo('id', '\Namespace\Models\Employee', 'location_id');

Model Employee

    $this->hasMany('location_id', '\Namespace\Models\Location', 'id');

Controller

    namespace Namespace\Modules\Frontend\Controllers;

    $user = \Namespace\Models\Employee::findFirst(array(
            'alias = :user:',
            'bind' => ['user' => (is_null($username)) ? $this->session->get('username') : $username]
            ));

    $user->location->name; // NULL
    $user-getLocation(); // return als NULL


17.1k
Accepted
answer

I am using now pure SQL Statements with modelsManager->createQuery.

Works like a charm

edited Dec '18

I know it's old thread, but I will post an answer how to do this (I found this thread when I got same problem). My models have namespace app\models and this is how I do inner join:

  // SELECT * FROM AdSenseAccountChannel INNER JOIN ApiKey ON ApiKey.id = AdSenseAccountChannel.apiKeyId WHERE ApiKey.value = $apiKeyValue
  return self::query()
  ->innerJoin("app\models\ApiKey", "app\models\ApiKey.id = app\models\AdSenseAccountChannel.apiKeyId")
  ->andWhere('app\models\ApiKey.value = :apiKeyValue:', ['apiKeyValue' => $apiKeyValue])
  ->execute();

Or shorter form:

return self::query()
  ->innerJoin("app\models\ApiKey", "ApiKey.id = app\models\AdSenseAccountChannel.apiKeyId", "ApiKey")
  ->andWhere('ApiKey.value = :apiKeyValue:', ['apiKeyValue' => $apiKeyValue])
  ->execute();

Hope it helps someone :)!