phpcakephpcakephp-model

Trying to saveall() into two separate tables with several entries


Hey all. I can't for the life of me seem to get this blasted saveall() function to work. I told it was magical, but I must not have enough midichlorians or whatever fancy stuff it takes to get it sparkin'.

So I've got four different tables--Questions, Comments, Employees, and Answers. Questions have many answers, and answers have one comment. Answers belong to Employees. Here's what's set up in my models:

Answer Model

var $hasOne = array(
    'Comment' => array(
        'className' => 'Comment',
        'foreignKey' => 'answer_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

var $belongsTo = array(
    'Question' => array(
        'className' => 'Question',
        'foreignKey' => 'question_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Employee' => array(
        'className' => 'Employee',
        'foreignKey' => 'employee_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

Comment Model

var $validate = array(
    'answer_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
        ),
    ),
);

var $belongsTo = array(
    'Answer' => array(
        'className' => 'Answer',
        'foreignKey' => 'answer_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

When I run saveall() for my data, I'm getting an error saying that my answer_id for my comments are not numeric. Here's what my data tree is set up like that I'm trying to save (using $this->Answer->saveall($this->data);:

[Answer] => Array
        (
            [1] => Array
                (
                    [question_id] => 1
                    [employee_id] => 14
                    [result] => 7
                    [unique_hash] => t9279kn7vh
                )

            [3] => Array
                (
                    [question_id] => 3
                    [employee_id] => 14
                    [result] => 6
                    [unique_hash] => t9279kn7vh
                )

            [4] => Array
                (
                    [question_id] => 4
                    [employee_id] => 14
                    [result] => 3
                    [unique_hash] => t9279kn7vh
                )

            [5] => Array
                (
                    [question_id] => 5
                    [employee_id] => 14
                    [result] => 2
                    [unique_hash] => t9279kn7vh
                )

            [6] => Array
                (
                    [question_id] => 6
                    [employee_id] => 14
                    [result] => 7
                    [unique_hash] => t9279kn7vh
                )

        )

    [Comment] => Array
        (
            [1] => Array
                (
                    [user_id] => 14
                    [text] => test1
                )

            [3] => Array
                (
                    [user_id] => 14
                    [text] => test2
                )

            [4] => Array
                (
                    [user_id] => 14
                    [text] => test3
                )

            [5] => Array
                (
                    [user_id] => 14
                    [text] => test4
                )

            [6] => Array
                (
                    [user_id] => 14
                    [text] => test5
                )

        )

Can anyone help me figure out what's wrong here? I appreciate the help.

EDIT: I've updated the $data array to have its keys start at zero with increments of one (i.e., array(0=>array(),1=>array(),2=>array()); etc.) and this has taken away the validation error, but the data still isn't saving.


Solution

  • try this one:

    $this->Answer->saveall($this->data['Answer']);
    $this->Comment->saveall($this->data['Comment']);
    

    for some reason, cakephp sometimes don't automatically save all records of related tables even if you defined their relationships. I'm still trying to figure out why.