0

Iam confused about a problem. i will describe it I am using HABTM first time in cakephp,also i am not too much familiar with cakephp 2.4.6

I have

     MediaOrg- model,     media_orgs - table name
     table fields- id,name
     public $hasAndBelongsToMany = array(
         'className' => 'ContactPerson',
         'joinTable' => 'contact_people_media_orgs',
         'foreignKey' => 'media_org_id',
         'associationForeignKey' => 'contact_person_id'
     );


     ContactPerson -model     contact_person- table name
     table_fields - id,name,designation,contact_number,ladline
     public $hasAndBelongsToMany = 'MediaOrg'


     ContactPeopleMediaOrg -model , table name-contat_people_media_orgs
     table_fields - contact_person_id ,media_org_id


            //now in controller saving values for media_org table
          $this->MediaOrg->save($this->request->data)
         $media_org_id=$this->MediaOrg->id;

      //next in controller saving values for contact_person table
          $this->ContactPerson->save($this->request->data)
         $mcontact_person_id=$this->ContactPerson->id;

 //next saving id's into many-to-many table
       $contact_person_mediaorg_table=array('contact_person_id'=>$contact_person_id,
               'media_org_id'=>$media_org_id );
 $this->ContactPeopleMediaOrg->save($contact_person_mediaorg_table);

everything is working fine. i dont know what happends in the common table contact_person_media_org , the data is adding 3 tomes. first time it adding correct id's and next each time it addiing the mobile number and land number of contact person with media_org_id when i debug it using getDataSource(), i can find that some param is passing to that common table and adding to it. i dont know how it happening

    (int) 6 => array('query' => 'INSERT INTO `go4ad`.`contact_people_media_orgs`                                                                    

     (`media_org_id`, `contact_person_id`) VALUES (?,?)',
        'params' => array(
            (int) 0 => '30',
            (int) 1 => '55555555'
        ),
        'affected' => (int) 0,
        'numRows' => (int) 0,
        'took' => (float) 1

Also i can find that some BEGIN and COMMIT keywords are there. Actually What is happening..if anyone can help me..pls pls help me. iam stucking

SibinF
  • 385
  • 1
  • 7
  • 25

1 Answers1

0

You should use saveAssociated(). Overall, your process should generate something like this:

$this->request->data = array(
    'MediaOrg' => array(
        'id' => $media_org_id,
    ),
    'ContactPerson' => array(
        0 => $first_person_id,
        1 => $second_person_id,
        2 => $thurd_person_id,
    ),
);
$this->MediaOrg->saveAssociated($this->request->data);

And it will add rows to your HABTM relation table (media_organization_contact_persons). Now, this works when you have the rows already added to the database, and you want to add the connections. If you want to add data to the tables in the same time, or whatever that this answer doesn't cover for you, you can read this article.

Скач от
  • 212
  • 2
  • 14
  • Read the instructions in the article I linked in my answer, that helped me a lot understanding how habtm relations work. – Скач от Mar 21 '14 at 11:39