user195257
user195257

Reputation: 3316

CakePHP save field to associated table

I have my 'PatientCase' model with a hasOne relationship to my 'PatientCaseOrder' model. (This table simply stored the patientCase id along with an integer position)

I am using an ajax function to update the position fields in 'PatientCaseOrder' using the function below in my PatientCase controller

public function update_position(){
        Configure::write('debug', 0);
        $this->autoRender = false;
        $this->loadModel('PatientCaseOrder');
        $list = $_POST['list'];
        $errs = false;
        if($list){
            foreach($list as $position){
                $id = $position[0];
                $pos = $position[1];
                $this->PatientCase->id = $id;

                if(! $this->PatientCase->PatientCaseOrder->saveField('position', $pos))
                     $errs = true;
            }
        }

        echo json_encode ($errs);
    }

I am passing to it an array containing the PatientCaseId and position. The code produces a 500 server error, where am i going wrong, or am i taking the wrong approach to this?

NOTE: I previously had the position field in the PatientCase model, and this line of code worked with the above segment of code

$this->PatientCase->saveField('position', $pos)

Upvotes: 0

Views: 284

Answers (1)

Tim Joyce
Tim Joyce

Reputation: 4517

You need to change your controller function for better debugging:

change to Configure::write('debug', 2);

add $this->layout = 'ajax';

and change : if(! $this->PatientCase->PatientCaseOrder->saveField('position', $pos)) $errs = true;

to:

pr($this->PatientCase->PatientCaseOrder->saveField('position', $pos)); die;

and then in your ajax function log the callback

console.log(returned_data);

and check for errors.

Upvotes: 1

Related Questions