Skip to content

[NFR]: Criteria support for aliases #16807

Open

Description

@ALameLlama

When you do Model::query() I would like a way to set an alias, this works fine when you're using it like in the docs but if you include something like a ->leftJoin() you'll start to run into issues

Phalcon\Mvc\Model\Exception
The column 'account_id' is ambiguous

and because there doesn't seem to be a way to create an alias you need to do something like this

Model::query()
                ->columns($columns)
                ->where(sprintf('%s.account_id = :id:', Model::class), ['id' => $user->account_id])

It would be nice if there was a ->alias() option or an additional param in ::query(alias: 'model') to be able to rename this instead of needing to do the full class name.

Relevant file
https://github.com/phalcon/cphalcon/blob/master/phalcon/Mvc/Model/Criteria.zep

Activity

  1. added
    new feature request
    on Sep 3, 2025
  2. raicabogdan commented on Sep 4, 2025

    @raicabogdan
    SponsorContributor

    The builder join methods should already accept aliases in the last argument. Can you provide a full query example you are using?

  3. ALameLlama commented on Sep 4, 2025

    @ALameLlama
    ContributorAuthor

    Sorry, I should have been more clear, I don't want to alias the join since that already can already be done. I want to alias the model that the ::query() is called on.

    Model::query()
        ->columns($columns)
        ->where(sprintf('%s.account_id = :id:', Model::class), ['id' => $user->account_id])
    	->leftJoin(SomeOtherModel::class, sprintf('some_other_model.service_id = %s.id', Model::class), 'some_other_model')
    
  4. raicabogdan commented on Sep 8, 2025

    @raicabogdan
    SponsorContributor

    Well anyway, I suggest using the query builder instead, and you will be able to set aliases as you want. Although I do agree that having an alias into the model::query() method would be nice and possibly quite simple if recall the code correctly, but we're so close to v6 and phql is the one pending that I'm reluctant to adding new feature to the existing code base at this time.

    @niden perhaps we can push this to v6 and leave it for later to implement as a new feature?

  5. changed the title [-][NFR]: Criteria Support for aliases[/-] [+][NFR]: `Criteria` class support for aliases[/+] on Sep 8, 2025
  6. changed the title [-][NFR]: `Criteria` class support for aliases[/-] [+][NFR]: `Criteria` support for aliases[/+] on Sep 8, 2025
  7. ALameLlama commented on Sep 8, 2025

    @ALameLlama
    ContributorAuthor

    @raicabogdan There is a better example and yes we could use the query builder, it would just be nice to be able to use ::query() on the model

    $services = Service::query()
        ->columns($columns)
        ->where(sprintf('%s.account_id = :id:', Service::class), ['id' => $user->account_id])
        ->inWhere('status', [Service::STATUS_ACTIVE])
        ->inWhere('server_id', collect($servers)->pluck('id')->toArray())
        ->leftJoin(Bulk::class, sprintf('bulk.hosting_id = %s.id', Service::class), 'bulk')
        ->leftJoin(Service\Meta::class,
            sprintf('hoid.hosting_id = %s.id AND key = "%s"', Service::class, Service\Meta::KEY_HOID), 'hoid')
        ->andWhere('bulk.hosting_id IS NULL')
        ->execute();
    
  8. added
    5.0
    on Jun 28, 2026
  9. self-assigned this
    on Jun 28, 2026
  10. added theissue type on Jun 28, 2026
  11. moved this from Backlog to Active Sprint in Phalcon v5on Jul 3, 2026
  12. added and removed
    5.0
    on Jul 5, 2026
  13. added this to Phalcon v7 and removed this from Phalcon v5on Jul 5, 2026
  14. niden commented on Jul 5, 2026

    @niden
    SponsorMember

    This will require interface changes. Porting to v7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

7.0new feature request

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions