Cakephp db查询结果没有表名,只是MySQL中的简单结果?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Cakephp db查询结果没有表名,只是MySQL中的简单结果?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用cakePHP,我有一个模特

$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;

我得到这样的结果:

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

但是我不想显示表格的名称,我更愿意通过以下方式获得结果:

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

我怎么能得到这个?

谢谢 !

@L_406_5@

从我所看到的,结果按表名分组.

简单的选择是:

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

另一种选择是:

$db = $this->getDataSource();
$result = $db->fetchAll(
    'SELECT * From (
        SELECT table1.id,(table4.user_id = "71") AS isfavorite
        FROM table1
        INNER JOIN ...
        LEFT JOIN ...
        LEFT JOIN ...
        where ... '
    ) as final_table
);

return $result;

这就是为什么你只有这样的东西:

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

脚本宝典总结

以上是脚本宝典为你收集整理的Cakephp db查询结果没有表名,只是MySQL中的简单结果?全部内容,希望文章能够帮你解决Cakephp db查询结果没有表名,只是MySQL中的简单结果?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。