Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



5.8k
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)



2.4k
Accepted
answer

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