如何减少_joinData在Cake 3.x中的字段数量? [英] How to reduce the amount of fields for _joinData in Cake 3.x?

查看:145
本文介绍了如何减少_joinData在Cake 3.x中的字段数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况



使用 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,
批准:[
{
id:
name:Jaime Jen,
company:Apple Company,
_joinData:{
contact_id:63,
id:335,
event_ticket_sale_id :06f39ba3-9a17-47c6-9374-24b49fb64665,
创建:2016-01-20T13:43:44 + 0800,
修改:2016-01-20T13:43:44+ 0800
}
}
]



/ h1>

我也想控制从 _joinData

$ p

理想情况下,我想从_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,
批准:[
{
id:63,
name:Jaime Jen,
公司:Apple Company,
_joinData:{
id:335,
contact_id:63,
event_ticket_sale_id:06f39ba3-9a17-47c6-9374-24b49fb64665,
}
}
]



我实际上甚至不需要_joinData如果我可以帮助它。

解决方案

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

  $ eventTicketSales-> each(function($ row){
foreach($ row [' Approvings'] as $ approving){
unset($ approving ['_ joinData']);
}
});

或者,因为你似乎想在JSON表示中应用这个,你可以标记 _joinData 实体的属性隐藏,因为在将实体转换为数组或JSON时通常需要将该属性丢弃

  class Approving extends Entity {
// ...

protected $ _hidden = [
'_joinData'
];

// ...
}

/ p>


Situation

Using Cake 3.2.4

I have a EventTicketSales table

that

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

When I do a pagination like this:

       $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',
        ];

I get the following data:

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"
        }
    }
]

What I want

I would like to also control the amount of data I retrieve from _joinData

Ideally I want as few fields from _joinData as I can possibly retrieve.

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",
        }
    }
]

I actually don't even need the _joinData if I can help it.

解决方案

You could for example iterate the collection and remove the properties that you're not interested in, something like

$eventTicketSales->each(function ($row) {
    foreach ($row['approvings'] as $approving) {
        unset($approving['_joinData']);
    }
});

Or, since you seem to want to apply this in JSON representations, you could mark the _joinData property of the Approving entity as hidden, given that you'd generally want to ditch that property when converting entities to arrays or JSON

class Approving extends Entity {
    // ...

    protected $_hidden = [
        '_joinData'
    ];

    // ...
}

See also

这篇关于如何减少_joinData在Cake 3.x中的字段数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆