# Cannot save associated table

**URL:** <https://discourse.cakephp.org/t/cannot-save-associated-table/8222>\
**Category:** Need Help\
**Created:** [August 15, 2020, 8:22am UTC](https://discourse.cakephp.org/t/cannot-save-associated-table/8222 "2020-08-15T08:22:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterVARGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/petervarga/32/1928_2.png) [@PeterVARGA](https://discourse.cakephp.org/u/PeterVARGA)\
**Post date:** [August 15, 2020, 8:22am UTC](https://discourse.cakephp.org/t/cannot-save-associated-table/8222/1 "2020-08-15T08:22:10Z")

</div>

I studied the posts here, also the documentation in:

- [https://book.cakephp.org/4/en/views/helpers/form.html#creating-inputs-for-associated-data](https://book.cakephp.org/4/en/views/helpers/form.html#creating-inputs-for-associated-data)
- [https://book.cakephp.org/4/en/orm/saving-data.html#saving-hasmany-associations](https://book.cakephp.org/4/en/orm/saving-data.html#saving-hasmany-associations)

But unfortunately I am apparently to stupid to save data into an associated table. I have this simple example:

```
CREATE TABLE `fathers` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) NOT NULL,
 `intval` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `fathers` (`id`, `name`, `intval`) VALUES (1, 'f1', 1);

CREATE TABLE `sons` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `father_id` int(11) NOT NULL,
 `name` varchar(25) NOT NULL,
 `floatval` float NOT NULL,
 PRIMARY KEY (`id`),
 KEY `father_id` (`father_id`),
 CONSTRAINT `sons_ibfk_1` FOREIGN KEY (`father_id`) REFERENCES `fathers` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

```

I want to display in the form for `fathers` additionally the associated fields of `sons`. This is how I do according to the above link from the official documentation:

```
echo $this->Form->control('name');
echo $this->Form->control('intval');

echo $this->Form->control('sons.0.id');
echo $this->Form->control('sons.0.name');
echo $this->Form->control('sons.0.intval');
echo $this->Form->control('sons.1.id');
echo $this->Form->control('sons.1.name');
echo $this->Form->control('sons.1.intval');

```

I baked the Controller and model by the cake binary and there isn’t any error message regarding the model so I would say they are correct.

In the `fathers` Controller in the `add()` & `edit()` functions I have - again according to the two above documentations - this code:

```
if ($this->request->is(['patch', 'post', 'put'])) {
	$data = $this->request->getData();
	$father = $this->Fathers->newEntity($data, ['associated' => ['Sons'] ]);

	$father->setDirty('Sons', TRUE);
	if ($this->Fathers->save($father)) {
		$this->Flash->success(__('The father has been saved.'));

		return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The father could not be saved. Please, try again.'));

```

This code doesn’t save the records and I get the flash error that it couldn’t be saved.

What am I doing wrong? Thx

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [August 15, 2020, 6:45pm UTC](https://discourse.cakephp.org/t/cannot-save-associated-table/8222/2 "2020-08-15T18:45:48Z")

</div>

You might be able to check the Entity after the `newEntity()` and `save()` calls to see if there are errors reported from the validation (runs when entity is patched) and rules (runs on save) processes.

```auto
debug($father->getErrors();

```

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [August 16, 2020, 2:53am UTC](https://discourse.cakephp.org/t/cannot-save-associated-table/8222/3 "2020-08-16T02:53:16Z")

</div>

One detail that just jumped out from your code:

`sons` table has the field `floatval` which cannot be null.

The form structure you show instead uses `intval`. If this is real (and not a typo in the post) this would likely be one error that would show up in the debug I suggested earlier.

---

<div class="post-metadata">

**Author:** ![PeterVARGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/petervarga/32/1928_2.png) [@PeterVARGA](https://discourse.cakephp.org/u/PeterVARGA)\
**Post date:** [August 16, 2020, 10:46am UTC](https://discourse.cakephp.org/t/cannot-save-associated-table/8222/4 "2020-08-16T10:46:42Z")

</div>

First, thank you very much.

_Even it’s so embarrassing I describe here what I have done wrong:_  
In this post I used a small MCVE because the original table contains 15 fields. In this MCVE I had the typo Don correctly pointed out. Changing the name to `floatval` fixed in this MCVE the problem.

But, I had still the issue with my original table containing these 15 fields. Thanks to the hint regarding `debug($father->getErrors());` and validation I instantly saw the reason why it always failed:

1. In the beginning I baked with the `cake` binary all controllers/models and templates for these father & son tables.
2. Then, I thought I am very smart and don’t want to enter each time I test a case all of these fields so I converted **in the database** all `NOT NULL` fields to `NULL` and thought that’s enough.
3. But, the model has been already baked with the validations and the `NOT NULL` fields were still in the validation flow of the model because I haven’t _re-baked_ the model.
4. After adding all of these `NOT NULL` fields into the form then it worked.

Again, thank you!

PS: There is now this question: What can be the reason CakePHP didn’t popup with the error message that a field needs a value. This would save a lot of time…

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [August 16, 2020, 4:31pm UTC](https://discourse.cakephp.org/t/cannot-save-associated-table/8222/5 "2020-08-16T16:31:18Z")

</div>

The automatic hinting of form fields with their errors requires strictly following all the norms of form creation. I don’t know all the details for insuring it always works but [here is the section that detail the pattern](https://book.cakephp.org/4/en/views/helpers/form.html#displaying-and-checking-errors).

Also, I have discovered several times that the error I was struggling to track down WAS displayed on the page but formatted in grey so it blended in with the other page content. Most embarrassing.
