Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 517
Mihai Petcu Avatar asked Feb 26 '16 17:02

Mihai Petcu


1 Answers

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();
like image 183
Fazal Rasel Avatar answered Nov 01 '22 05:11

Fazal Rasel