Unable to register multiple records at the same time with cakephp3

Asked 2 years ago, Updated 2 years ago, 53 views

I recently started using cakephp3.
I'm trying to create a schedule that can be created and edited, and I'm trying to create a movement in the add action to register the schedule from April 1st to 30th, 2015 from a screen lined with 30 input fields for April 2015.

The part of the form is written according to the rules, and the value passed to the add action is

'Schedules' =>[
    (int) 0 = > [
        'day' =>'2015-04-01',
        'event' = > 'Ahhhhhhhhhhhhhhhhhhhhhh
        'start' = > '11',
        'end' = > '12'
    ],
    (int) 1 = > [
        'day' =>'2015-04-02',
        'event' = > 'aaaa',
        'start' = > '22',
        'end' = > '23'
    ],……

It's shaped like this, but it's as follows.

public function add(){
        $schedulestable=TableRegistry::get('Schedules');
    if($this->request->is('post'))){
        $schedules=$scheduled->newEntities($this->request->data());
        foreach($schedules as$schedule){
            $schedulestable->save($schedule);
        }
    }
    $this->set('schedules',$schedules);
}

Attempting to save by converting it to an entity does not work and nothing is saved.
By the way

public function add(){
        $schedulestable=TableRegistry::get('Schedules');
        $schedules=$scheduled->newEntity();
    if($this->request->is('post'))){
        $schedules = $schedulesstable->patchEntities($schedules, $this->request->data());
        foreach($schedules as$schedule){
            $schedulestable->save($schedule);
        }
    }
    $this->set('schedules',$schedules);
}

I tried writing it first and then writing it down, but it still didn't work.In both cases, the $schedules value just before turning the loop is

[
(int) 0 = > object(App\Model\Entity\Schedule) {

    'new' = > true,
    'accessible' = > [
        '*' =>true
    ],
    'properties' = > [
        (int) 0 = > [
            'day' =>'2015-04-01',
            'event' = > 'Ahhhhhhhhhhhhhhhhhhhhhh
            'start' = > '11',
            'end' = > '12'
        ],
        (int) 1 = > [
            'day' =>'2015-04-02',
            'event' = > 'aaaa',
            'start' = > '22',
            'end' = > '23'
        ],……

I suspect that the reason is that the array has only one element, and the loop immediately after it turns up in this upper tier (actually *debug($schedule)* in foreach results like that), but I don't know how it works.Of course, I tried $schedules[0] to spin it with foreach, but it didn't work.
By the way, when I post to the add action, no errors, no warnings are displayed, and no data is saved, so nothing happens.

How can I solve this problem?Or is the cause different from what I think?Please let me know

php cakephp

2022-09-30 20:24

1 Answers

CakePHP3 does not treat multiple entities unless the associative array key to be passed to newEntities is numeric.

$schedules=$scheduled->newEntities($this->request->data('Schedules'));

as shown in

Internally, the value passed to newEntities() is passed to \Cake\ORM\Marshaller::many()

public function many(array$data,array$options=[])
{
    $output=[];
    foreach($data as$record){
        $output[] = $this->one($record,$options);
    }
    return$output;
}

Because it is processed as shown in , you will find that the key to the request data is not working if it remains Schedules.


2022-09-30 20:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.