This->query output

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

This->query output

floepi

Hi all,

i was wondering why cake gives me a weird output array for my query

Query:
SELECT
                        jobName,
                        ujc.id,
                        roleName,
                        userName,
                        disciplineName

                FROM user_job_connections as ujc
                LEFT JOIN jobs on jobs.id = ujc.job_id
                LEFT JOIN roles on roles.id = ujc.role_id
                LEFT JOIN users on users.id = ujc.user_id
                LEFT JOIN disciplines on disciplines.id = ujc.discipline_id
                WHERE userName="'.$userName.'"'



Output:
Array
(
    [0] => Array
        (
            [jobs] => Array
                (
                    [jobName] => hub
                )

            [ujc] => Array
                (
                    [id] => 1
                )

            [roles] => Array
                (
                    [roleName] => Producer
                )

            [users] => Array
                (
                    [userName] => philip-f
                )

            [disciplines] => Array
                (
                    [disciplineName] => Lighting
                )

        )

I rather would like the output to be:

Array
(
    [0] => Array
        (
            [jobName] => hub
            [id] => 1
            [roleName] => Producer
            [userName] => philip-f
            [disciplineName] => Lighting
        )

Can somebody tell me why cake creates subarrays for each joint. Makes
displaying the data in the view quite difficult.

Do i have to write my own query function which does not format the
results in such a way? Or run a formatting function after i get the
results.

Any help appreciated.

Thanks

Phil


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: This->query output

lloydhome

Phil,

For most things it is better to have the data the way it is returned.
I would encourage you to work within the framework and not against it.
That said, maybe you have some logic that is legacy like I do.  I am
porting a clumsy mess of code into Cake but it cannot be done all at
once.  I have old methods that want the data in a flat associative
array as you describe.  In my AppModel class I put:

  /**
   * Totally flatten an array.
   */
  function & array_flatten(&$array) {
    if (is_array($array)) {
      $newarray = array();
      foreach ($array as $k=>$v) {
        if (is_array($v)) {
          $newarray += $this->array_flatten($v);
        } else
          $newarray[$k] = $v;
      }
      return $newarray;
    }
    return $array;
  }



Then to use:

$newarray = array();
foreach ($results as $v) {
  $newarray[] = $this->User->array_flatten($v);
}


HTH,
DL


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: This->query output

floepi

Hi,

thanks for your reply. You are right - i seem to work against the
framework due to my working habbits. I need to get used to cake first i
think.
Even so - do you know how to create your own query function. It looks
really confusing to me.

Cheers

Phil

On Nov 7, 1:33 pm, "lloydhome" <[hidden email]> wrote:

> Phil,
>
> For most things it is better to have the data the way it is returned.
> I would encourage you to work within the framework and not against it.
> That said, maybe you have some logic that is legacy like I do.  I am
> porting a clumsy mess of code into Cake but it cannot be done all at
> once.  I have old methods that want the data in a flat associative
> array as you describe.  In my AppModel class I put:
>
>   /**
>    * Totally flatten an array.
>    */
>   function & array_flatten(&$array) {
>     if (is_array($array)) {
>       $newarray = array();
>       foreach ($array as $k=>$v) {
>         if (is_array($v)) {
>           $newarray += $this->array_flatten($v);
>         } else
>           $newarray[$k] = $v;
>       }
>       return $newarray;
>     }
>     return $array;
>   }
>
> Then to use:
>
> $newarray = array();
> foreach ($results as $v) {
>   $newarray[] = $this->User->array_flatten($v);
>
> }HTH,
> DL


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---