尝试将 saveall() 放入两个具有多个条目的单独表中

发布于 2024-11-03 15:18:45 字数 3641 浏览 0 评论 0原文

嘿大家。我似乎一辈子都无法让这个该死的 saveall() 函数发挥作用。我告诉过它很神奇,但我一定没有足够的纤原体或任何花哨的东西来让它闪闪发光。

所以我有四个不同的表——问题、评论、员工和答案。问题有多种答案,答案有一条评论。答案属于员工。以下是我的模型中的设置:

应答模型

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' => ''
    )
);

评论模型

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

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

当我为数据运行 saveall() 时,我收到一条错误消息我的评论的answer_id 不是数字。这是我的数据树的设置,就像我试图保存的那样(使用 $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
                )

        )

任何人都可以帮我弄清楚这里出了什么问题?我感谢您的帮助。

编辑:我已更新 $data 数组,使其键从零开始,增量为 1(即, array(0=>array(),1=>array(),2=>array()); 等) 这已经消除了验证错误,但数据仍然没有节省。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-11-10 15:18:45

尝试这个:

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

由于某种原因,cakephp 有时不会自动保存相关表的所有记录,即使您定义了它们的关系。我仍在试图找出原因。

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.

因为看清所以看轻 2024-11-10 15:18:45

SaveAll 只是不保存其他相关表。我们完全按照您所说的去做,我们自己定义它们,我们甚至手动链接 id。虽然有点痛苦,但这是最好的选择。

SaveAll just doesn't save other related tables. We do exactly what you said, we define them ourselves, we even link the id's manually. It's sorta a pain but it's the best option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文