开发者

HABTM data not saving (cakephp)

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 $hasAndBelongsT开发者_如何学JAVAoMany = 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?


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

Have you tried...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜