transactions - Automatically adding additional Records into HABTM table in CakePhp -
iam confused problem. describe using habtm first time in cakephp,also not familiar 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 media_org table $this->mediaorg->save($this->request->data) $media_org_id=$this->mediaorg->id; //next in controller saving values contact_person table $this->contactperson->save($this->request->data) $mcontact_person_id=$this->contactperson->id; //next saving id's 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 working fine. dont know happends in common table contact_person_media_org , data adding 3 tomes. first time adding correct id's , next each time addiing mobile number , land number of contact person media_org_id when debug using getdatasource(), can find param passing common table , adding it. dont know how happening
(int) 6 => array('query' => 'insert `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 can find begin , commit keywords there. happening..if can me..pls pls me. iam stucking
you should use saveassociated(). overall, process should generate 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 add rows habtm relation table (media_organization_contact_persons). now, works when have rows added database, , want add connections. if want add data tables in same time, or whatever answer doesn't cover you, can read this article.
Comments
Post a Comment