Reputation: 516
I'm stuck on how to save a BelongsToMany/Through association through the parent form.
I have a ProjectsTable that belongsToMany Characters through ProjectCharacters. I have a CharactersTable that belongsToMany Projects through ProjectCharacters.
My projects/edit/# page contains a second form after the regular projects/edit form that looks like this:
<?= $this->Form->create($project_character); ?>
<?php
echo $this->Form->input('character_id', ['options' => $character_options]);
echo $this->Form->hidden('project_id', ['value' => $project['id']]);
?>
<?= $this->Form->button(__('Add Character')) ?>
<?= $this->Form->end() ?>
This is just a project id and character id that I want to create a new record for in ProjectCharacters. but when I press save, I get the following error:
Record not found in table "projects" with primary key [NULL]
What do I need to do to save a ProjectCharacter association through this form?
Upvotes: 1
Views: 7294
Reputation: 9614
I would say your problem is here:
https://gist.github.com/sarahkeefe/a42d39efade836a675c8#file-projectscontroller-php-L156
You are trying to validate the ownership of a project for an action that does not receive any arguments (the add function). You need to conditionally execute that code or to always allow your add()
action to your users.
In the future, you can look at the error page, it has a stack trace, which is a list of functions that got called before your error happened. They usually indicate the place where your error can be found.
Upvotes: 1