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

Assigning alias to a model

Let's say I have a model class inside MyApp\Models namespace. Now when I want to filter my data on some specific column, I have to do someting like the following:

MyApp\Models\Customer::find("MyApp\Models\Customer.name = 'Some cool name'");

Is there some way to avoid typing "MyApp\Models\Customer.name" and just use some table alias, so that I could just put "c.name" instead?

Only name must work:

MyApp\Models\Customer::find("name = 'Some cool name'");

Yeah, it does. But what if the column is ambiguous, in case the table is joined with another table. The following will not work because I would have to specify "MyApp\Models\Customer.id = 123" instead of "id = 123":

    MyApp\Models\Customer::query()
        ->join('MyApp\Models\Company',  'c.id = MyApp\Models\Customer.company_id', 'c')
        ->where('id = 123')
        ->execute();


34.6k
Accepted
answer
edited May '15

You can use the query builder instead:

$robots = $this->modelsManager->createBuilder()
    ->from(['r' => 'Some\Robots'])
    ->where('r.name = "Ultron"')
    ->getQuery()
    ->execute();

That was what I was looking for. Thanks :)