Cakephp db query result without table names, just the result, as in MySQL?

I use cakephp and I have a model

$db = $this->getDataSource();
$result = $db->fetchAll(
        'SELECT table1.id, 
            table1.title, 
            table1.buy_url, 
            table2.image_file as image, 
            table3.category_id as maincategory, 
            (table4.user_id = "71") AS isfavorite
        FROM table1
        INNER JOIN ...
        LEFT JOIN ...
        LEFT JOIN ...
        where ...);

    return $result;

I get this result:

{
  "table1": {
    "id": "132",
    "title": "Awesome",
  },
  "table2": {
    "image": "image_25398457.jpg"
  },
  "table3": {
    "maincategory": "3"
  },
  "table4": {
    "isfavorite": "1"
  }
}

but I do not want to show the table names, I would prefer to get the result as follows:

{
    "id": "132",
    "title": "Awesome",
    "image": "image_25398457.jpg"
    "maincategory": "3"
    "isfavorite": "1"
}   

How can i achieve this?

Thank!

+4
source share
1 answer

From what I see, the results are grouped by table name.

The easiest option:

$merged = call_user_func_array('array_merge', $result);

Another variant:

$db = $this->getDataSource();
$result = $db->fetchAll(
    'SELECT * FROM (
        SELECT table1.id, 
            table1.title, 
            table1.buy_url, 
            table2.image_file as image, 
            table3.category_id as maincategory, 
            (table4.user_id = "71") AS isfavorite
        FROM table1
        INNER JOIN ...
        LEFT JOIN ...
        LEFT JOIN ...
        where ... '
    ) as final_table
);

return $result;

That is why you will only have something like:

{
   "final_table" : {
       "id": "132",
       "title": "Awesome",
       "image": "image_25398457.jpg"
       "maincategory": "3"
       "isfavorite": "1"
   }
} 
+1
source

All Articles