HABTM 数据未保存 (cakephp)

发布于 2024-08-12 06:12:45 字数 1610 浏览 10 评论 0原文

我有两个与 HABTM 相关的模型(文档和人员)。

class Person extends AppModel {
    var $name = 'Person';
    var $hasAndBelongsToMany = array(
        'Document' => array(
            'className' => 'Document',
            'joinTable' => 'documents_people',
            'foreignKey' => 'person_id',
            'associationForeignKey' => 'document_id',
            'unique' => false
       )
   );

class Document extends AppModel {
    var $name = 'Document';
    var $hasAndBelongsToMany = array(
        'Person'=>array(
            'className' => 'Person',
            'joinTable' => 'documents_people',
            'foreignKey' => 'document_id',
            'associationForeignKey' => 'person_id',
            'unique' => false
       )
   );

我有文档的添加视图,其中为与文档相关的每个人填充了一个复选框。

    echo $form->input('People', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$people, 'label' => 'People: '));

这是来自控制器的线路,应该进行保存。

$this->Document->create();
if ($this->Document->saveAll($this->data)) {

我注意到数据没有保存到 Documents_people 表中。所以,我转储了$this->data。

文档部分如下所示:

[Document] => Array
    (
        [file_name] => asdasd
        [tags] => habtm
        [People] => Array
            (
                [0] => 6
                [1] => 12
                [2] => 15
            )

        [image] => img/docs/2009-11-19-233059Jack.jpg
    )

这些是我想要与此文档关联的人员的 ID。然而,没有任何内容被传输到documents_people。我做错了什么?

I have two models related HABTM (documents and people).

class Person extends AppModel {
    var $name = 'Person';
    var $hasAndBelongsToMany = array(
        'Document' => array(
            'className' => 'Document',
            'joinTable' => 'documents_people',
            'foreignKey' => 'person_id',
            'associationForeignKey' => 'document_id',
            'unique' => false
       )
   );

class Document extends AppModel {
    var $name = 'Document';
    var $hasAndBelongsToMany = array(
        'Person'=>array(
            'className' => 'Person',
            'joinTable' => 'documents_people',
            'foreignKey' => 'document_id',
            'associationForeignKey' => 'person_id',
            'unique' => false
       )
   );

I have the add view of documents populated with one checkbox for each person that will be related to the document.

    echo $form->input('People', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$people, 'label' => 'People: '));

This is the line from the controller that is supposed to be doing the saving.

$this->Document->create();
if ($this->Document->saveAll($this->data)) {

I noticed that the data was not getting saved into the documents_people table. So, I dumped $this->data.

The document portion looks like this:

[Document] => Array
    (
        [file_name] => asdasd
        [tags] => habtm
        [People] => Array
            (
                [0] => 6
                [1] => 12
                [2] => 15
            )

        [image] => img/docs/2009-11-19-233059Jack.jpg
    )

Those are the ids of the people I want associated with this document. However, nothing is transferred to documents_people. What have I done wrong?

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

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

发布评论

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

评论(1

单身情人 2024-08-19 06:12:45

也许你的 $this->data 数组应该有一个“Person”部分,而不是复数“People”

你试过吗...

echo $form->input('Person'.....

Perhaps your $this->data array should have a 'Person' section, not a plural 'People'

Have you tried...

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