Marco C
Marco C

Reputation: 111

How to set default fields in multi-model form with CakePHP 3?

I have a form where I send two models for adding info from two different tables.

Places and Events. When Event is in one place which is not in database, I let to user put Place info to add. This works well for my Add Event.

Now I want to make the Edit-version for Events, but data is not filled with defaults.

My form is like:

<?php
echo $this->Form->create($event);
echo $this->Form->input('Place.address');
echo $this->Form->input('Place.lat');
echo $this->Form->input('Place.lon');
echo $this->Form->input('Event.place_id');
echo $this->Form->button(__('Validar'));
echo $this->Form->end();
?>

In my EventsController I have something like this:

    $place = $this->Places->newEntity();
    $event = $this->Events->newEntity();

    if ($this->request->is('post')) {
        $place = $this->Places->patchEntity($place, $this->request->data['Place']);
        $event = $this->Events->patchEntity($event, $this->request->data['Event']);

    $this->set('place', $place);
    $this->set('event', $event);

Normally with one model, fields have all default filled with database.

How can I do for make defaults with two models?

I want to avoid the default => $place->place_id option for each field.

Thank you

Upvotes: 1

Views: 105

Answers (1)

Valentin Rapp
Valentin Rapp

Reputation: 472

your question is unclear. Why do you create new entities in an edit form? Why dont you set defaults in your sql?

Furthermore you should associate the models and work with associations. One event hasOne place. place belongsTo event see the book for more information.

Upvotes: 1

Related Questions