views:

370

answers:

1

Hello,

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?

Thank you, Frank Luke

+1  A: 

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

Have you tried...

echo $form->input('Person'.....
Simon
So simple, but correct.
Frank Luke