# Populate select data options then saved from other tables with hasMany relation

**URL:** <https://discourse.cakephp.org/t/populate-select-data-options-then-saved-from-other-tables-with-hasmany-relation/6452>\
**Category:** Need Help\
**Created:** [August 2, 2019, 3:48am UTC](https://discourse.cakephp.org/t/populate-select-data-options-then-saved-from-other-tables-with-hasmany-relation/6452 "2019-08-02T03:48:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [August 2, 2019, 3:48am UTC](https://discourse.cakephp.org/t/populate-select-data-options-then-saved-from-other-tables-with-hasmany-relation/6452/1 "2019-08-02T03:48:17Z")

</div>

I have problem when save result select data options from other tables.  
below this orm hasMany

> table usertraces hasMany signs

```auto
table userTraces
+-----+------------+------------+
+ ID + Username + sign +
+-----+------------+------------+
+ 1 + John + swollen +
+ 2 + John + bloody +
+-----+------------+------------+

table signs
+-----+------------+
+ ID + sign +
+-----+------------+
+ 1 + swollen +
+ 2 + bloody +
+-----+------------+

method signsTable
$this->belongsTo('Usertraces', [
	     'foreignKey' => 'usertrace_id'
]);

method usertracesTable
$this->hasMany('Signs', [
	  'foreignKey' => 'usertrace_id'
]);

usertracesController
public function testkueri()
	{
	  $usertrace = $this->Usertraces->newEntity();
	  $sign = $this->set('signs', $this->Usertraces->Signs->find('list'));
	  $usertrace->Gejala = $this->set('signs', $this->Usertraces->Signs->find('list'));
	  if ($this->request->is('post')) {
		  $usertrace = $this->Usertraces->patchEntity($usertrace, $this->request->getData());
		  if ($this->Usertraces->save($usertrace)) {
			  $this->Flash->success(__('Deteksi gejala berhasil disimpan'));
			  return $this->redirect(['action' => 'testkueri']);
		  }
		  $this->Flash->error(__('Deteksi gejala gagal disimpan, coba lagi'));
	  }
	  $this->set(compact('sign'));
	  $this->set(compact('usertrace'));
	  $this->set('_serialize', ['usertrace']);
	}

testkueri.ctp
<?php
  echo $this->Form->create($usertrace);
  echo $this->Form->control('Username', array('type' => 'text'));
  echo $this->Form->control('sign', array('type' => 'select', 'options' => $signs));
  echo $this->Form->end();
?>

```

the query result populate select options was sucess work, but can’t saved in tables usertraces

```auto
table userTraces
+-----+------------+------------+
+ ID + Username + sign +
+-----+------------+------------+
+ 1 + John + null +
+ 2 + John + null +
+-----+------------+------------+

```

I hope someone help me fix the trouble, thanx for help…

---

<div class="post-metadata">

**Author:** ![dmuenstermann](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmuenstermann](https://discourse.cakephp.org/u/dmuenstermann)\
**Post date:** [August 6, 2019, 1:34pm UTC](https://discourse.cakephp.org/t/populate-select-data-options-then-saved-from-other-tables-with-hasmany-relation/6452/2 "2019-08-06T13:34:48Z")

</div>

What is inside $this-\>getRequest()-\>getData() ?  
I’m pretty sure there is no sign\_id inside and due to this we get to the next problem. CakePHP is much about conventions and it seems your relation inside the table classes is as misconfigured as the tables itself are.

Should morely look like:

```auto
table userTraces
+-----+------------+------------+
+ id + username + sign_id +
+-----+------------+------------+
+ 1 + John + 1 +
+ 2 + John + 2 +
+-----+------------+------------+

table signs
+-----+------------+
+ id + sign +
+-----+------------+
+ 1 + swollen +
+ 2 + bloody +
+-----+------------+

```

in my eyes. Then still requires to correct the belongsTo-part.  
If you corrected that and made sure the posted form-data is also correct, it should work as expected.
