# Problem save data from multiple select and association

**URL:** https://discourse.cakephp.org/t/problem-save-data-from-multiple-select-and-association/6916
**Category:** Need Help
**Created:** [November 23, 2019, 8:45pm UTC](https://discourse.cakephp.org/t/problem-save-data-from-multiple-select-and-association/6916 "2019-11-23T20:45:45Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)
#### Post date: [November 23, 2019, 8:45pm UTC](https://discourse.cakephp.org/t/problem-save-data-from-multiple-select-and-association/6916/1 "2019-11-23T20:45:45Z")

</div>

Please help. I have 3 tables, I need to save the data to the table **ZboziZarazeni** from the select field, but the patch entities will not get the data … can you please help me?

In table **Zbozi** can be saved. Table **ZboziZarazeni** has two primary key, maybe problem? Only one column refers to the table **Zbozi** , two column refers to the table **Struktura**

Thank you!

**Zbozi TABLE:**

```
public function initialize(array $config)
	{
		parent::initialize($config);

		$this->setTable('zbozi');
		$this->setDisplayField('id');
		$this->setPrimaryKey('id');

		$this->hasMany('ZboziZarazeni', [
			'foreignKey' => 'zbozi_id'
		]);

	}

```

**ZboziZarazeni TABLE:**

```
		public function initialize(array $config)
	{
		parent::initialize($config);

		$this->setTable('zbozi_zarazeni');
		$this->setDisplayField('zbozi_id');
		$this->setPrimaryKey(['zbozi_id', 'struktura_id']);

		$this->belongsTo('Zbozi', [
			'foreignKey' => 'zbozi_id',
			'joinType' => 'INNER'
		]);
		$this->belongsTo('Struktura', [
			'foreignKey' => 'struktura_id',
			'joinType' => 'INNER'
		]);

	}

```

**Struktura TABLE:**

```
		public function initialize(array $config)
	{
		parent::initialize($config);

		$this->setTable('struktura');
		$this->setDisplayField('id');
		$this->setPrimaryKey('id');

		$this->hasMany('ZboziZarazeni', [
			'foreignKey' => 'struktura_id'
		]);

	}

```

**Zbozi CONTROLLER**

```
if ($this->request->is(['patch', 'post', 'put'])) {
$Zbozi = $this->Zbozi->get($id, [
	'contain' => ['ZboziZarazeni']
]);
$Zbozi = $this->Zbozi->patchEntity($Zbozi, $this->request->getData(), [
	'associated' => ['ZboziZarazeni']
]);
if ($this->Zbozi->save($Zbozi)) {
	$this->Flash->success('OK');
	return $this->redirect(['action' => 'display']);
}
$this->Flash->error('ERROR');

```

}

**Form**

```
	echo $this->Form->control('zbozi_zarazeni.struktura_id._ids', [
	'label' => 'Zařazení',
	'required' => true,
	'type' => 'select',
	'multiple' => true,
	'options' => $ZarazeniTree,
	'value' => $ZarazeniTreeSelected,
	'escape' => false
]);
```
