I am having trouble submitting/saving data to the CakePHP Model. I constructed the form as usual as I always do, but this time I am getting notices and warning an also data is not getting saved. Here is the form I have constructed and method in Controller:

educationaldetails.ctp

<?php
if (empty($Education)):

    echo $this->Form->create('Candidate', array('class' => 'dynamic_field_form'));

    echo $this->Form->input('CandidatesEducation.0.candidate_id', array(
        'type' => 'hidden',
        'value' => $userId
    ));

    echo $this->Form->input('CandidatesEducation.0.grade_level', array(
        'options' => array(
            'Basic' => 'Basic',
            'Masters' => 'Masters',
            'Doctorate' => 'Doctorate',
            'Certificate' => 'Certificate'
        )
    ));

    echo $this->Form->input('CandidatesEducation.0.course');

    echo $this->Form->input('CandidatesEducation.0.specialization');

    echo $this->Form->input('CandidatesEducation.0.university');

    echo $this->Form->input('CandidatesEducation.0.year_started', array(
        'type' => 'year'
    ));

    echo $this->Form->input('CandidatesEducation.0.year_completed', array(
        'type' => 'year'
    ));

    echo $this->Form->input('CandidatesEducation.0.type', array(
        'options' => array(
            'Full' => 'Full',
            'Part-Time' => 'Part-Time',
            'Correspondence' => 'Correspondence'
    )));

    echo $this->Form->input('CandidatesEducation.0.created_on', array(
        'type' => 'hidden',
        'value' => date('Y-m-d H:i:s')
    ));

    echo $this->Form->input('CandidatesEducation.0.created_ip', array(
        'type' => 'hidden',
        'value' => $clientIp
    ));

    echo $this->Form->button('Submit', array('type' => 'submit', 'class' => 'submit_button'));

    echo $this->Form->end();

endif;

CandidatesController.php

public function educationaldetails() {
        $this->layout = 'front_common';

        $this->loadModel('CandidatesEducation');

        $Education = $this->CandidatesEducation->find('first', array(
            'conditions' => array(
                'candidate_id = ' => $this->Auth->user('id')
            )
        ));

        $this->set('Education', $Education);

        // Checking if in case the Candidates Education is available then polpulate the form
        if (!empty($Education)):
            // If Form is empty then populating the form with respective data.
            if (empty($this->request->data)):
                $this->request->data = $Education;
            endif;
        endif;

        if ($this->request->is(array('post', 'put')) && !empty($this->request->data)):

            $this->Candidate->id = $this->Auth->user('id');
            if ($this->Candidate->saveAssociated($this->request->data)):
                $this->Session->setFlash('You educational details has been successfully updated', array(
                    'class' => 'success'
                ));
                return $this->redirect(array(
                            'controller' => 'candidates',
                            'action' => 'jbseeker_myprofile',
                            $this->Auth->user('id')
                ));
            else:
                $this->Session->setFlash('You personal details has not been '
                        . 'updated successfully, please try again later!!', array(
                    'class' => 'failure'
                ));
            endif;

        endif;
}

Here is the screenshot enter image description here of errors I am getting, Not able to figure out what is happening while other forms are working correctly. looks like something something wrong with view?

2

There are 2 best solutions below

0
On BEST ANSWER

This error is coming out because you are not passing the proper arguments to the session->setFlash() method, you are doing like this:

   $this->Session->setFlash('You personal details has not been updated successfully, please try again later!!', array(
        'class' => 'failure'
    ));

In docs it is mentioned like:

   $this->Session->setFlash('You personal details has not been updated successfully, please try again later!!', 
       'default', array(
           'class' => 'failure'
    ));
7
On

Hi so if i understand :

array(
    'CandidatesEducation' => array(
        (int) 0 => array(
            'candidate_id' => '5',
            'grade_level' => 'Basic',
            'course' => 'Masters of Computer Application',
            'specialization' => '',
            'university' => 'Mumbai University',
            'year_started' => array(
                'year' => '2011'
            ),
            'year_completed' => array(
                'year' => '2014'
            ),
            'type' => 'Full',
            'created_on' => '2014-11-27 15:44:36',
            'created_ip' => '127.0.0.1'
        )
    )
),

Is your data , and you just need to save this in your candidates_educations table , so in this case i would do :

$this->Candidate->CandidatesEducation->save($this->request->data);

and your $data had to look :

array(
    'CandidatesEducation' => array(
            'candidate_id' => '5',
            'grade_level' => 'Basic',
            'course' => 'Masters of Computer Application',
            'specialization' => '',
            'university' => 'Mumbai University',
            'year_started' => array(
                'year' => '2011'
            ),
            'year_completed' => array(
                'year' => '2014'
            ),
            'type' => 'Full',
            'created_on' => '2014-11-27 15:44:36',
            'created_ip' => '127.0.0.1'
    )
);

the saveAssociated is used when you create your Model AND Model associated , not only your Associated model.

And : i m not sure for year_started and year completed rm of data, it depends of your table schema ; what's the type of year_completed and year_started ?