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

Model Query Results - incorrect key?

Hello! I'm using PHP Version 7.1.6, Phalcon Version 3.2.0.

I have a query:

            $model = ComplainUser::query()
                ->columns([
                    'ComplainUser.*',
                    'UserWho.*',
                    'UserWhom.*',
                ])
                ->join('User', 'ComplainUser.who_id = UserWho.id', 'UserWho')
                ->join('User', 'ComplainUser.whom_id = UserWhom.id', 'UserWhom')
                ->orderBy('ComplainUser.date_add DESC')
                ->execute();

This is query result:

array (size=1)
  0 => 
    object(Phalcon\Mvc\Model\Row)[121]
      public 'complainUser' => 
        object(ComplainUser)[90]
          public 'id' => string '5' (length=1)
          public 'date_add' => string '2018-01-10 00:00:00' (length=19)
          ...
      public 'UserWho' => 
        object(User)[108]
          public 'id' => string '1' (length=1)
          public 'email' => string '[email protected]' (length=17)
          ...
      public 'UserWhom' => 
        object(User)[113]
          public 'id' => string '2' (length=1)
          public 'email' => string '[email protected]' (length=17)
          ...

It confuses me, that the first record has the key "complainUser", not "ComplainUser".

Is it an issue? I'd like to be sure that it will not cause problems in future (for example, if this behavior will be changed).

I know I can't set alias for my model in my query, so for now I'm going to make double check: both 'complainUser' and 'ComplainUser'.

Thank you for your responses.

How does your model definition look like for ComplainUser? Maybe the lowercase is coming from there (classname, getsource)?

Also, if you want to use aliases, you can refactor it to this:

$model = $this->modelsManager->createBuilder()
    ->from([
                    'ComplainUser' => 'ComplainUser.*',
                    'UserWho' => 'UserWho.*',
                    'UserWhom' => 'UserWhom.*',
                ])
    ->join('User', 'ComplainUser.who_id = UserWho.id', 'UserWho')
    ->join('User', 'ComplainUser.whom_id = UserWhom.id', 'UserWhom')
    ->orderBy('ComplainUser.date_add DESC')
    ->getQuery()
    ->execute();

If you need to access the models manager from a static context, you can do this:

$di = \Phalcon\Di\FactoryDefault::getDefault();
$di->get('modelsManager')->createBuilder()
    // ...