0
votes

I have 3 tables:

  • Bookings
  • Payments
  • Sessions

In my form which is part of the Bookings table, while I have several attributes exclusive to Payments (eg. Amount, Depositpaid, Status), only one has a form input - everything else is Sessions related:

echo $this->Form->hidden('payments.amount',['type'=>'number', 'value'=>'0', 'step'=>'0.01', 'class'=>'currency form-control']);

In the BookingsController, I have the following:

$booking = $this->Bookings->newEntity([
            'associated' => ['Payments', 'Sessions']
            ]]);
        if ($this->request->is('post')) {
            $data = $this->request->data;
            $booking = $this->Bookings->patchEntity($booking, $data, [
                'associated' => ['Payments', 'Sessions']
            ]);
...

$save = $this->Bookings->save($booking);

...

Upon clicking submit on the form, data in both Bookings and Sessions is saved. In the post data, there are two arrays: Session array and Payments array. In the variables data inside the Booking array, there is an Associated array with two elements: 0 Payments, 1 Sessions.

However, in the SQL log, there is no Insert Into for Payments table. When doing a debug of $booking (the patchEntity one), Payments is an empty array as follows:

'payments' => [],

I also did a debug of $save that comes after patchEntity, and obtained the same result for 'payments'.

I checked my Payments table model and the corresponding structure in MySQL when it came to not nulls and validation.

Below is the Payments model (PaymentsTable) regarding validation:

public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        $validator
            ->allowEmpty('paymentmethod','create');

        $validator
            ->decimal('amount')
            ->requirePresence('amount', 'create')
            ->notEmpty('amount');

        $validator
            ->requirePresence('status', 'create')
            ->notEmpty('status');

        $validator
            ->integer('depositpaid')
            ->allowEmpty('depositpaid', 'create');

        $validator
            ->allowEmpty('uniqid', 'create');

        return $validator;
    }

In my MySQL database, I set attribute 'status' to automatically be set to 'notpaid' upon the creation of a new data entry, and also set attribute 'amount' to have a default value of '0.00'. The only not null fields are 'id', 'booking_id' (foreign key that links to the Booking table's id), 'amount' and 'status'.

So I guess my question is: Is the reason I'm not getting this associated table saved because I did not put in form inputs (hidden or otherwise) for the remainder of the Payments attributes or is there something I'm missing?

Update:

Okay I added a 0 to the form input (payments.0.amount), and this time the submit did not succeed. Upon debugging patchEntity - 'debug($booking)' once more, I find this error in the payments array:

'[errors]' => [
                'status' => [
                    '_required' => 'This field is required'
                ]

This is even though I stated status to be set to automatically put in a default string. Do I need to do something like $this->request->data['status'] in the controller? And if so, how does it vary from an attribute for non-associated data (eg. $this->request->data['attribute'] is used for non-associated data)?

I've already tried:

$this->request->data['status'] = 'notpaid';
$this->request->data['payments']['status'] = 'notpaid';
$this->request->data['payments'][0]['status'] = 'notpaid';
$this->request->data['payments']['0']['status'] = 'notpaid';

All without success and receiving the same error in the patchEntity debug. The method that does work is adding in another form input for 'status' (the attribute that says it is required), but although I've solved my issue, I'm still left wondering how to input associated data from the controller.

1
Assuming that a booking hasMany payments, it might be as simple as changing your input field from payments.amount to payments.0.amount.Greg Schmidt
Okay I added a 0 to it, and this time the submit did not succeed. Error found upon debugging as shown in the updated post.mistaq
Ended up doing it the roundabout method, and just added another hidden form input for status. Though I am still curious as to how to properly input associated data from the controller...haven't figured it out yet.mistaq
It looks like you don't understand the format for the request data very well, based on all the different ways you tried to get the status in there. Did you try debug($this->request->data) to see what it already looks like? That should point the way to how to add the status. Also, if you don't actually need the status to be present when you're creating the record, you could just remove the ->requirePresence('status', 'create') part of your validation.Greg Schmidt

1 Answers

0
votes

I fixed it by just adding in another hidden form input, although I would still like to know how to input association data through the controller instead of the view.