dousheyan0375 2017-05-18 15:42
浏览 23

如何选择包含关联的字段作为主要实体的字段?

I have Cakephp 3 in a project and I'm doing api rest to get a JSON to get data in mobile devices. I have two tables in associated with a foreign key like this:

  MySql tables 
  ----------------------
  Table Tickets:
  |id|code|price_id|

  Table Prices
  |id|price|

  ----------------------

In TicketsTable CakePHP:

$this->belongsTo('Prices', [
        'foreignKey' => 'price_id',
        'joinType' => 'INNER'
    ]);

In controller when I make REST api:

$this->loadModel('Tickets');
        $entradas = $this-> Tickets->find('all')
            ->contain('Prices')
            ->select(['Tickets.code','Prices.price'])
            ->limit('200')
            ->toArray();

Then this array, parsed to JSON return this:

"result":{  
      "tickets":[  
         {  
            "code":"08998112773",
            "prices":{  
               "prices.price":1
            }
         },
         {  
            "code":"07615265880",
            "prices.prices":{  .........

And I want to return this JSON:

   "result":{  
          "tickets":[  
             {  
                "code":"08998112773",
                "price":1
             },
             {  
                "code":"07615265880",
                "price":1  .........

That is, that prices do not insert into a new array and that the name of the table does not appear in the field name.

Thanks a lot!!!!

  • 写回答

1条回答 默认 最新

  • douwang6635 2017-05-18 18:03
    关注

    You could use use Cake\Collection\Collection::map() to create a new array:

    $tickets = [
        'result' => [
            'tickets' => [
                [
                    'code' => '123',
                    'prices' => [
                        'prices.price' => '2'
                    ]
                ],
                [
                    'code' => '312423',
                    'prices' => [
                        'prices.price' => '4'
                    ]
                ]
            ]
        ]
    ];
    
    $collection = new Collection($tickets['result']['tickets']);
    
    $new = $collection->map(function ($value, $key) {
        return [
            'code' => $value['code'], 
            'price' => $value['prices']['prices.price']
        ];
    });
    
    $result = $new->toArray();
    
    debug(json_encode(['result' => ['tickets' => $new]], JSON_PRETTY_PRINT)); 
    die;
    

    The output is:

    {
        "result": {
            "tickets": [
                {
                    "code": "123",
                    "price": "2"
                },
                {
                    "code": "312423",
                    "price": "4"
                }
            ]
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度