jeudi 25 mai 2017

CakePHP: Saving multiple entries to a model from a different controller

I have a database that has a 'Schools' table and a 'Students' table. Obviously, a school has many students, and this is represented in the DB.

After 'baking' in CakePHP, my add functions were all fine with their default forms. I could create students, and I could create schools. However I would like to be able to create two students whenever I create a school. So I set up my form like this in the school's 'add.ctp' file:

<?= $this->Form->create('school') ?>
<fieldset>
    <legend><?= __('Add School') ?></legend>
    <?php

    // create the school
    echo $this->Form->input('school_name');
    echo $this->Form->input('school_description');

    echo $this->Form->input('student.0.name');
    echo $this->Form->input('student.0.description');
    echo $this->Form->input('student.1.name');
    echo $this->Form->input('student.1.description');

    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

However no solution I can find is able to work for modifying the school's 'add' function in the SchoolsController.php file to be able to save the two students. How would I do this?




Aucun commentaire:

Enregistrer un commentaire