Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

cakephp - How to reduce the amount of fields for _joinData in Cake 3.x?

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 (CakeORMQuery $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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

    return $row;
});

Which could also be done in a result formatter of a query object:

$query->formatResults(function (CakeCollectionCollectionInterface $results) {
    return $results->map(function ($row) {
        foreach ($row['approvings'] as &$approving) {
            unset($approving['_joinData']);
        }

        return $row;
    });
});

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...