0

I am having an issue where CakePHP is ignoring saving a field I specify and instead creating extra records. I am using CakePHP 2.3.6.

My table looks like this:

CREATE TABLE `events_guests` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `guest_id` int(11) NOT NULL,
  `event_id` int(11) NOT NULL,
  `promoter_id` int(11) DEFAULT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `attended` varchar(15) NOT NULL DEFAULT 'No',
  PRIMARY KEY (`id`)
)

Heres the code

public function addGuest($event_id, $promoter_id = null) {
if ($this->request->is('post')) {
                $this->Guest->create();
                $event_data = array('event_id' => $event_id);
                $data = $this->request->data;
                $data['Event'] = $event_data;
                if($promoter_id) {
                    $data['Event']['promoter_id'] = $promoter_id;
                }
                if ($this->Guest->saveAssociated($data)) {
                    $this->Session->setFlash(__('The guest has been added to the guestlist'), 'flash/success');
                } else {
                $this->Session->setFlash(__('The guest could not be saved. Please, try again.'), 'flash/error');
                }

Here is my data that I am trying to save:

Array (
  [Guest] => Array
    (
      [first_name] => Joe
      [last_name] => Schmoe
    )
  [Event] => Array
    (
       [event_id] => 1
       [promoter_id] => 2
    )
)

My Models follow:

class Guest extends AppModel {
public $hasAndBelongsToMany = array(
        'Event' => array(
            'className' => 'Event',
            'joinTable' => 'events_guests',
            'foreignKey' => 'guest_id',
            'associationForeignKey' => 'event_id',
            'unique' => 'true',
        ), 
        'Promoter' => array(
            'className' => 'Promoter',
            'joinTable' => 'events_guests',
            'foreignKey' => 'guest_id',
            'associationForeignKey' => 'promoter_id',
            'unique' => 'true',
        )
    );
}

And

class Event extends AppModel {
public $hasAndBelongsToMany = array(
        'Guest' => array(
            'className' => 'Guest',
            'joinTable' => 'events_guests',
            'foreignKey' => 'event_id',
            'associationForeignKey' => 'guest_id',
            'unique' => 'keepExisting',
            'order' => 'last_name',
        ),
        'Promoter' => array(
            'className' => 'Promoter',
            'joinTable' => 'events_promoters',
            'foreignKey' => 'event_id',
            'associationForeignKey' => 'promoter_id',
            'unique' => 'keepExisting',
        )
    );
}

The results from this are 2 records for EventsGuests, neither with a promoter_id.

One record receives event_id = 1 AS EXPECTED

Other record receives event_id = 2, which is actually the promoter_id

I have tried using a mix of saveAssociated and saveAll

Any help is greatly appreciated! Thank you!

Bryan Zwicker
  • 642
  • 1
  • 6
  • 24

1 Answers1

0

Reference: Cakephp save extra attribute in HABTM relation

For your problem, don't use HABTM, because, HABTM is normally use for connect 2 tables.

So, if you have more than 2 fields, create a model and use it like belongsTo and hasMany relationships.

class Event extends AppModel {

  $hasMany = array(
    'EventGuest' => array(
      'className' => 'EventGuest',
      'foreignKey' => 'event_id'    
    )
  );

}

class Guest extends AppModel {

  $hasMany = array(
    'EventGuest' => array(
      'className' => 'EventGuest',
      'foreignKey' => 'guest_id'    
    )
  );

}

class Promoter extends AppModel {

  $hasMany = array(
    'EventGuest' => array(
      'className' => 'EventGuest',
      'foreignKey' => 'promoter_id'    
    )
  );

}

Now, your relationship model.

class EventGuest extends AppModel {

  $belongsTo = array(
    'Event' => array(
      'className' => 'Event',
      'foreignKey' => 'event_id'
    ),
    'Guest' => array(
      'className' => 'Guest',
      'foreignKey' => 'guest_id'
    ),
    'Promoter' => array(
      'className' => 'Promoter',
      'foreignKey' => 'promoter_id'
    )
  );

}

And continue use your code.

public function addGuest($event_id, $promoter_id = null) {
  if ($this->request->is('post')) {
    $this->Guest->create();
    $event_data = array('event_id' => $event_id);
    $data = $this->request->data;
    $data['Event'] = $event_data;
    if($promoter_id) {
      // EDITED
      $data['Promoter']['promoter_id'] = $promoter_id;
    }

    if ($this->Guest->saveAssociated($data)) {
      $this->Session->setFlash(__('The guest has been added to the guestlist'), 'flash/success');
    } else {
      $this->Session->setFlash(__('The guest could not be saved. Please, try again.'), 'flash/error');
    }
  }
}
Community
  • 1
  • 1
Patrick Maciel
  • 4,874
  • 8
  • 40
  • 80
  • If my question help you and solve you problem, don't forget to vote and mark to solved. – Patrick Maciel Oct 03 '13 at 17:51
  • Thanks for the reply, Patrick. That helped slightly, still a bit stuck. Now it still creates 2 records, but the first record has the `event_id` set with a null `promoter_id` and the second record has a null `event_id` and the correct `promoter_id` I am still working with it, will update soon – Bryan Zwicker Oct 03 '13 at 17:57
  • You can edit your question and put your relationships array of models? Just curiously. – Patrick Maciel Oct 03 '13 at 19:03
  • Yes, I have just added the model associations. Still have not got this working. Thank you very much for looking at this with me! – Bryan Zwicker Oct 03 '13 at 20:28
  • @BryanZwicker I update my answer, see and tell me if works now. I think is the better solution. – Patrick Maciel Oct 03 '13 at 21:08