如何减少Cake 3.x中_joinData的字段数量?

时间:2016-03-12 12:28:28

标签: cakephp cakephp-3.0 contains

情况

使用Cake 3.2.4

我有EventTicketSales

  $this->belongsToMany('Approvings', [
        'className' => 'Contacts',
        'joinTable' => 'event_ticket_sales_approvings',
        'targetForeignKey' => 'contact_id',
        'saveStrategy' => 'replace',
    ]);

当我做这样的分页时:

       $this->paginate['contain'] = [
            'Approvings' => function (\Cake\ORM\Query $query) {
                return $query->select([
                    'Approvings.id',
                    'Approvings.name',
                    'Approvings.company',
                    'EventTicketSalesApprovings.event_ticket_sale_id'
                ]);
            }
        ];
        $this->paginate['fields'] = [
            'EventTicketSales.id', 'EventTicketSales.event_id', 'EventTicketSales.billing_amount_in_sgd', 
            'EventTicketSales.payment_terms', 'EventTicketSales.invoice_number', 
            'EventTicketSales.due_date',
        ];

我得到以下数据:

id: "06f39ba3-9a17-47c6-9374-24b49fb64665",
event_id: 7,
billing_amount_in_sgd: 7680.03,
payment_terms: "45 days",
invoice_number: "9191",
due_date: "2016-03-05T00:00:00+0800",
approvings: [
    {
        id: 63,
        name: "Jaime Jen",
        company: "Apple Company",
        _joinData: {
            contact_id: 63,
            id: 335,
            event_ticket_sale_id: "06f39ba3-9a17-47c6-9374-24b49fb64665",
            created: "2016-01-20T13:43:44+0800",
            modified: "2016-01-20T13:43:44+0800"
        }
    }
]

我想要什么

我还希望控制从_joinData

检索的数据量

理想情况下,我希望尽可能少的来自_joinData的字段。

id: "06f39ba3-9a17-47c6-9374-24b49fb64665",
event_id: 7,
billing_amount_in_sgd: 7680.03,
payment_terms: "45 days",
invoice_number: "9191",
due_date: "2016-03-05T00:00:00+0800",
approvings: [
    {
        id: 63,
        name: "Jaime Jen",
        company: "Apple Company",
        _joinData: {
            id: 335,
            contact_id: 63,
            event_ticket_sale_id: "06f39ba3-9a17-47c6-9374-24b49fb64665",
        }
    }
]

如果我能提供帮助,我实际上甚至不需要_joinData。

1 个答案:

答案 0 :(得分:6)

例如,您可以迭代集合并删除您不感兴趣的属性,例如

Text

或者,既然你似乎想在JSON表示中应用它,你可以将$eventTicketSales->each(function ($row) { foreach ($row['approvings'] as $approving) { unset($approving['_joinData']); } }); 实体的_joinData属性标记为隐藏,因为你通常想要抛弃该属性将实体转换为数组或JSON

Approving

另见