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

Group custom fields of joined table to model object (as if SELECT joinTable.*)

If I select certain fields from joined table as:

<?php
        return $this->getModelsManager()->createBuilder()
                    ->columns('user.*, profile.name, profile.surname')
                    ->addFrom('User', 'user')
                    ->leftJoin('Profile', '', 'profile')
                    ->where("user.id = :id:", array('id' => $id))
                    ->getQuery()->execute();
?>

... i get:

<?php
object(Phalcon\Mvc\Model\Row)[105]
  public 'profile_name' => string 'Name' (length=4)
  public 'profile_surname' => string 'Surname' (length=6)
  public 'user' => 
    object(User)[242]
      protected 'id' => int 1
      protected 'login' => string 'login' (length=10)
?>

But I want to group profile fields into model as User model, like:

<?php
object(Phalcon\Mvc\Model\Row)[105]
  public 'user' => 
    object(User)[242]
      protected 'id' => int 1
      protected 'login' => string 'login' (length=10)
  public 'profile' => 
    object(Profile)[242]
      protected 'name' => string 'Name' (length=4)
      protected 'surname' => string 'Surname' (length=6)
      protected 'other1' => string '' (length=0)
      protected 'other2' => string '' (length=0)
      protected 'other3' => string '' (length=0)
?>

How can I do this?

hi, this is topical question for me. Can anyone help me?



7.9k
edited Jul '14

i cannot provide a straight answer,
i used to have problems using createBuilder mostly because i have not a full grasp of createBuilder functions
i managed to get over with workarounds :

either by creating a view (in mySQL) where i make the joins of different tables and then making a model to query the view
or by using raw SQL where the queries were complex (GROUPS, SUMS, AVGs and such)



3.6k
Accepted
answer

Thanks, I think that best way is to use table.* for all joined tables. But it leads to retrieving unnecessary data.