Hey,
first of all thank you.
Here you have a picture of the two tables:

important part of FarmsTable:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('farms');
$this->displayField('unique_name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('FirstFarms', [
'className' => 'Farms',
'foreignKey' => 'second_farm_id',
'targetForeignKey' => 'first_farm_id',
'joinTable' => 'connected_farms'
]);
$this->belongsToMany('SecondFarms', [
'className' => 'Farms',
'foreignKey' => 'first_farm_id',
'targetForeignKey' => 'second_farm_id',
'joinTable' => 'connected_farms'
]);
}
Important part of ConnectedFarmsTable:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('connected_farms');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('FirstFarms', [
'className' => 'Farms',
'foreignKey' => 'first_farm_id',
]);
$this->belongsTo('SecondFarms', [
'className' => 'Farms',
'foreignKey' => 'second_farm_id',
]);
}
edit-method of FarmsController:
public function edit($id = null) {
$farm = $this->Farms->get($id, [
'contain' => ['Bundlers', 'InspectionInstitutions','SecondFarms']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$farm = $this->Farms->patchEntity($farm, $this->request->data);
if ($this->Farms->save($farm)) {
$this->Flash->success(__('The farm has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The farm could not be saved. Please, try again.'));
}
}
$countries = $this->Farms->Countries->find('list', ['limit' => 200]);
$users = $this->Farms->Creators->find('list', ['limit' => 200]);
$secondFarms = $this->Farms->SecondFarms->find('list', ['limit' => 200])->where(array('not' => array('SecondFarms.id' => $id)));
$bundlers = $this->Farms->Bundlers->find('list', ['limit' => 200]);
$inspectionInstitutions = $this->Farms->InspectionInstitutions->find('list', ['limit' => 200]);
$this->set(compact('farm', 'countries', 'users', 'bundlers', 'inspectionInstitutions','secondFarms'));
$this->set('_serialize', ['farm']);
}
important part of Farms edit.ctp:
<div class="farms form large-9 medium-8 columns content">
<?= $this->Form->create($farm) ?>
<fieldset>
<legend><?= __('Edit Farm') ?></legend>
<?php
echo $this->Form->input('lfbis_number');
echo $this->Form->input('ggn_number');
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('street');
echo $this->Form->input('zip');
echo $this->Form->input('city');
echo $this->Form->input('country_id', ['options' => $countries]);
echo $this->Form->input('phone');
echo $this->Form->input('mail');
echo $this->Form->input('second_farms._ids', ['options' => $secondFarms]);
/If I use this input, I only get the farms which are connected as second farms, but I also want to show the farms connected as first farms, f.e. if the actual farm is in the second farms ids connected with other first farms - so it’s also possible to connect a first farm A to a second farm B and create the opposite connection (first farm B to second farm A), but this shouldn’t be possible/
echo $this->Form->input('bundlers._ids', ['options' => $bundlers]);
echo $this->Form->input('inspection_institutions._ids', ['options' => $inspectionInstitutions]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>