I would like to implement add entities in one View, for example add a new song (unknown in DB) in a new song request. I don’t know how to access and implement multiple model’s data in one template.
songs have id, title, artist, album etc.
song requests have request_id, song_id (foreign key), date of request, user_id etc.
One way would be to make multiple hasOne relations and foreignKeys to add Form->control() for unknown song.title, song.artist etc.( Creating Inputs for Associated Data).
The other way (the correct one I guess) would be to implement the add() form of Songs if the user clicks on an “add new song” button.
In pseudo-code for add.php of song requests:
<?= $this->Form->create($order) ?> <fieldset> <legend><?= __('Add Order') ?></legend> <?php ... echo $this->Form->button('add new song',
echoing the actual add() template code of Songs
$this->Form->button(‘confirm new song’, [‘controller’=>‘Songs’, ‘action’=>‘add’, new song data fields (are no possible direct arguments for the add function)]);
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
The problem is that I want to retrieve the actual add form of Songs, while staying at the same page and same location (song request add template). Is it even possible to have form elements for successive models within the fieldset and before the submit button of the first model form?
I could also implement the add() controller code of songs into add() of song requests controller and retrieve data fields of one form. I don’t know which solution is DRYer, especially since it would not automatically take validation of the fields for songs into account.