Search code examples
has-and-belongs-to-manycakephp-3.x

CakePHP 2x to 3x, HABTM with keepExisting


I have two models that are associated with a Has And Belongs To Many Association in Cake 2.x. I'm trying to port my application over to Cake 3.3. In general, I'm having an issue replicating the "keepExisting" functionality found in the Cake 2.x associations. Right now, I'm trying to use the "through" functionality, but I'm not sure I'm barking up the right tree.

I have the following Tables:

class ContestsTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('contests');
    $this->displayField('name');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsToMany('Events', [
        'through' => 'ContestsEvents'
    ]);
}
}

class EventsTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('events');
    $this->displayField('name');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsToMany('Events', [
        'saveStrategy' => 'append',
        'through' => 'ContestsEvents'
    ]);
}
}

class ContestsEventsTable extends Table
{
public function initialize(array $config) {
    $this->belongsTo('Contests');
    $this->belongsTo('Events');
}

}

When I save the initial contest with associated events, the save is fine and the association records are created in the join table. When I edit the contest record and change the list of the events, any of the selected events are added...but the old ones that are not selected anymore are not deleted. The second issue is that a new join record is created for already existing relationships. I would like to keep the already existing records rather than creating new ones. Later on in the app, I'll be using the join records id in an additional relationship...and I do not want the id to be regenerated each time the overlying contest record is updated.

I hope I'm making sense in describing what I'm trying to do. But in the simplest form, I'm trying to replicate the functionality found via the 'unique' => 'keepExisting' option included in Cake 2x, just can't crack the code on how to accomplish this in Cake 3.3?

Thanks in advance for your help.


Solution

  • OK, So here it goes. First, I think I wrote the question above out of frustration, so my apologies if it wasn't clear. I went on to working on other parts of my app, with this issue stuck in the back of my head. Well, I figured it out...so here's my answer:

    I was an idiot.

    Today, I took some time with a sandbox app and basically got the functionality to work on a sandbox table, but couldn't get it to work on the app's tables. Eventually I narrowed it down to the fact that it was a database issue. Yes...I somehow made the foreign_key fields in my join table VARCHAR and not INT. Yep...that will do it.

    Well, I guess my point is to log this for all eternity...Check your database fields and tables are set up correctly!!!