0
votes

I have a cakephp application in which the Payments table has many applications,

I am trying to save with the code below, but the payment data is not being saved, only the application data is being saved.

public function add()
{
    $application = $this->Applications->newEntity();
    if ($this->request->is('post')) {
        $application = $this->Applications->patchEntity($application, $this->request->getData(),
            [ 'associated'=>['Payments']  ]);
        //debug($application);
        //exit;
        if ($this->Applications->save($application)) {
            $this->Flash->success(__('The application has been saved.'));
                //debug($application);
                //exit();
            return $this->redirect(['action' => 'view', $application['id']]);
        }
        $this->Flash->error(__('The application could not be saved. Please, try again.'));
    }
    $tasks = $this->Applications->Tasks->find('list', ['limit' => 200]);
    $this->set(compact('application', 'tasks'));
}

Please what could be wrong?

\src\Controller\ApplicationsController.php (line 58)

[
    'tasks_id' => '5',
    'refnumber' => '1519062454350860',
    'payment' => [
        (int) 0 => [
            'refnumber' => '1519062454350860'
        ]
    ],
    'surname' => 'asdf',
    'othernames' => 'Opeoluwa Clement',
    'gender' => 'male',
    'phone' => '08060595019',
    'email' => '[email protected]',
    'canid' => 'asdfasd',
    'examinationpassed' => 'Post Fellowship Certificate',
    'monthofexaminationpassed' => 'April',
    'yearofexaminationpassed' => '1950',
    'transcriptaddress' => 'Lagos State University',
    'status' => 'Opened',
    'applicationletter' => [
        'tmp_name' => 'C:\wamp\tmp\php69B1.tmp',
        'error' => (int) 0,
        'name' => 'OAGCOurse.jpg',
        'type' => 'image/jpeg',
        'size' => (int) 189739
    ],
    'postinglog' => [
        'tmp_name' => 'C:\wamp\tmp\php69E1.tmp',
        'error' => (int) 0,
        'name' => 'OAGCOurse.jpg',
        'type' => 'image/jpeg',
        'size' => (int) 189739
    ],
    'certificate' => [
        'tmp_name' => 'C:\wamp\tmp\php6A11.tmp',
        'error' => (int) 0,
        'name' => 'OAGCOurse.jpg',
        'type' => 'image/jpeg',
        'size' => (int) 189739
    ],
    'evidenceofpayment' => [
        'tmp_name' => 'C:\wamp\tmp\php6A41.tmp',
        'error' => (int) 0,
        'name' => 'OAGCOurse.jpg',
        'type' => 'image/jpeg',
        'size' => (int) 189739
    ],
    'annualdues' => [
        'tmp_name' => 'C:\wamp\tmp\php6A70.tmp',
        'error' => (int) 0,
        'name' => 'OAGCOurse.jpg',
        'type' => 'image/jpeg',
        'size' => (int) 189739
    ]
]

Warning (2): mb_strlen() expects parameter 1 to be string, array given [CORE\src\Validation\Validation.php, line 742]

\src\Controller\ApplicationsController.php (line 59)

object(App\Model\Entity\Application) {

    'tasks_id' => (int) 3,
    'surname' => 'clement',
    'othernames' => 'Opeoluwa Clement',
    'gender' => 'female',
    'phone' => '08060595019',
    'email' => '[email protected]',
    'canid' => 'asdfasd',
    'examinationpassed' => 'Part 1/Membership',
    'transcriptaddress' => 'Lagos State University',
    'status' => 'Opened',
    'applicationletter' => [
        'tmp_name' => 'C:\wamp\tmp\php7E70.tmp',
        'error' => (int) 0,
        'name' => 'fav.png',
        'type' => 'image/png',
        'size' => (int) 13893
    ],
    'postinglog' => [
        'tmp_name' => 'C:\wamp\tmp\php7E90.tmp',
        'error' => (int) 0,
        'name' => 'fav.png',
        'type' => 'image/png',
        'size' => (int) 13893
    ],
    'certificate' => [
        'tmp_name' => 'C:\wamp\tmp\php7ED0.tmp',
        'error' => (int) 0,
        'name' => 'fav.png',
        'type' => 'image/png',
        'size' => (int) 13893
    ],
    'evidenceofpayment' => [
        'tmp_name' => 'C:\wamp\tmp\php7EFF.tmp',
        'error' => (int) 0,
        'name' => 'OAGCOurse.jpg',
        'type' => 'image/jpeg',
        'size' => (int) 189739
    ],
    'annualdues' => [
        'tmp_name' => 'C:\wamp\tmp\php7F4F.tmp',
        'error' => (int) 0,
        'name' => 'OAGCOurse.jpg',
        'type' => 'image/jpeg',
        'size' => (int) 189739
    ],
    '[new]' => true,
    '[accessible]' => [
        'surname' => true,
        'othernames' => true,
        'gender' => true,
        'email' => true,
        'phone' => true,
        'examinationpassed' => true,
        'canid' => true,
        'transcriptaddress' => true,
        'applicationletter' => true,
        'postinglog' => true,
        'certificate' => true,
        'evidenceofpayment' => true,
        'annualdues' => true,
        'tasks_id' => true,
        'status' => true,
        'modified' => true,
        'created' => true,
        'task' => true
    ],
    '[dirty]' => [
        'tasks_id' => true,
        'surname' => true,
        'othernames' => true,
        'gender' => true,
        'phone' => true,
        'email' => true,
        'canid' => true,
        'examinationpassed' => true,
        'transcriptaddress' => true,
        'status' => true,
        'applicationletter' => true,
        'postinglog' => true,
        'certificate' => true,
        'evidenceofpayment' => true,
        'annualdues' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Applications'

}
1
Please post the data of "$this->request->getData()" - bikash.bilz
@bikash.bilz pastebin.com/ejFEdqsz , i am surprised , the associated data is showing in the debug, but why is it not getting saved? - Clement Opeoluwa Adetunji
You say your Payments table hasMany Applications. But what association does Applications have to Payments? hasMany? hasOne? belongsTo? Or no such association at all? - Greg Schmidt

1 Answers

0
votes

As per your association, you have made in the Application model. The alias is 'Payments' not 'Payment', so Change 'payment' to 'payments' in your form input field. like

echo $this->Form->input('payments.0.refnumber', ['value' => '1519062454350860']);

so the getData(); will return the array like

'tasks_id' => '5',
    'refnumber' => '1519062454350860',
    'payments' => [               //note it's not payment
        (int) 0 => [
            'refnumber' => '1519062454350860'
        ]
    ],

This willl solve your problem

EDIT:-

$application = $this->Applications->patchEntity($application, $this->request->getData(),
            [
                 'validation' => false, 
                 'associated'=> [
                             'Payments' => ['validation' => false]
                 ]  
           ]);