1

I have below table structure.

Mandator table

class MandatorTable extends Table
{
public function initialize(array $config)
    {
        $this->table('mandators');

        $this->belongsToMany('Seminar', [
            'foreignKey' => 'mandator_id',
            'targetForeignKey' => 'seminar_id',
            'joinTable' => 'mandators_seminars'
        ]);
    }
}

Semiar table

class SeminarTable extends Table
{
    public function initialize(array $config)
    {
        $this->table('seminars');

        $this->belongsToMany('Mandator', [
            'foreignKey' => 'seminar_id',
            'targetForeignKey' => 'mandator_id',
            'joinTable' => 'mandators_seminars'
        ]); 
    }   
}

both table are belong to 'mandators_seminars' table

mandator_id, seminar_id

When I save data it's save in seminar table but not in 'mandators_seminars' table

Query

$seminartable = $this->Seminar->newEntity();

$this->request->data['mandator'][0] = 1;

$seminardata = $this->Seminar->patchEntity($seminartable, $this->request->data);
$this->Seminar->save($seminardata)

Request data

Array
(
    [bookable] => test
    [released] => aaa
    [linkable] => bb
    [name] => ccc
    [internalnote] => ddd
    [abstract] => ttt
    [description] => ddd
    [Category] => 14
    [mandator] => Array
        (
            [0] => 1
        )

    [mandator_owner_id] => 1
)
Durgesh
  • 97
  • 3
  • 8

1 Answers1

0

Look you have two table Mandator and Similar as singular , but your connecting table is plural. Firstly check this. If there is still a problem check this CakePHP Through Association

As you can see your association should be like this:

$this->belongsToMany('Mandator', [
        'foreignKey' => 'seminar_id',
        'targetForeignKey' => 'mandator_id',
        'through' => 'PluginName.MandatorsSeminars'
        'joinTable' => 'mandators_seminars',
        'className' => 'PluginName.Mandator'
    ]); 

And one more tip: table should be called as plural.

MatosKW
  • 15
  • 1
  • 2
  • 10
  • It's not working after changes table Mandator and Seminar to plural. – Durgesh Jun 28 '19 at 11:16
  • So try create association to MandatorsSeminars table in Mandators and after save entity , create and save entity in MandatorsSeminars, or check if in Mandators you have association to MandatorsSeminars. – MatosKW Jun 28 '19 at 11:34