Mihai Petcu
Mihai Petcu

Reputation: 368

How to set an alias on a Model::query() in Phalcon Framework

How to set an SQL alias for a Phalcon Model. A method alias() does not exist.

A sample of what i need:

$modelA = ModelA::query()
          ->alias('q')
          ->columns(['q.*','concat(q.id,r.id)) 
          ->join('ModelB', 'q.id = r.model_a_id', 'r', 'LEFT');

How can I create q alias?

Upvotes: 6

Views: 2444

Answers (1)

Fazal Rasel
Fazal Rasel

Reputation: 4526

Model query returns \Phalcon\Mvc\Model\Criteria. There is not method to set alias. You can get what are you trying with modelManager as-

    $modelA = $this->modelsManager->createBuilder()
        ->addFrom('ModelA', 'q')
        ->join('ModelB', 'a.id = r.model_a_id', 'r')
        ->columns(['q.*','concat(q.id,r.id))
        ->getQuery()
        ->execute();

Upvotes: 6

Related Questions