Cannot save associated table

I studied the posts here, also the documentation in:

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

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.

debug($father->getErrors();
1 Like

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.

1 Like

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…

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.

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.