ajt
ajt

Reputation: 662

cant edit data after using a find but needed a get

I cant edit data from a table of rows from a find command as I get an unknown method 'isNew'. I understand I didnt use a get command to retrieve the data but I used a find instead. The data returned is correct (array given below) but it is in the wrong format . I used a query and need to convert to an entity but I dont know how to do this.

My question is how do I get data with a get command with where conditions if I need to use get for edit functions to make this work?

https://github.com/cakephp/cakephp/issues/5262

Argument 1 passed to Cake\ORM\Table::patchEntity() must be an instance of Cake\Datasource\EntityInterface, instance of Cake\ORM\Query given

public function edit2($id = null)
{
    $a3 = '2016-05-28';
    $a4 = '2016-06-01';

    $lessons = $this->find()
        ->contain(['Tutors', 'Subjects', 'TutoringTypes', 'Terms', 'Students'])
        ->select([
            'Lessons.id', 'Lessons.lesson_date', 'Lessons.lesson_plan',
            'Tutors.id', 'Tutors.first_name', 'Tutors.last_name',
            'Subjects.name', 'TutoringTypes.value'
        ])
        ->where([
            'Lessons.lesson_date >' => $a3,
            'Lessons.lesson_date <' => $a4,
            'Lessons.tutor_id' => 12
        ])
        ->order(['Lessons.lesson_date' => 'ASC'])
        ->hydrate(true);

    $lessons->matching('Students', function ($q) use ($a8) {
        return $q
            ->select(['Students.id', 'Students.first_name', 'Students.last_name'])
            ->where(['Students.first_name like' => '%' . $a8 . '%']);
    });

    if ($this->request->is(['patch', 'post', 'put'])) {
        debug($this->request->data);

        $lessons = $this->Lessons->patchEntity($lessons, $this->request->data);

        if ($this->Lessons->save($lesson)) {
            $this->Flash->success(__('The lesson has been saved.'));
            return $this->redirect(['action' => 'edit2']);
        } else {
            $this->Flash->error(__('The lesson could not be saved. Please, try again.'));
        }

    }

    $this->set(compact('lessons'));
    $this->set('_serialize', ['lessons']);
}

view

<?php foreach ($lessons as $key => $item):
//foreach ($query3 as $item):
    ?>
    <tr>
        <?php echo $this->Form->hidden($key . '.id', ['label' => '', 'value' => $item->id]) ?>

        <td><?= $item->id ?></td>
        <td><?= $item->lesson_date ?></td>
        <td><?= $item->tutor->first_name . ' ' . $item->tutor->last_name ?></td>

        <td><?= $item->students[0]->first_name . ' ' . $item->students[0]->last_name ?></td>
        <td><?= $item->subject->name ?></td>
        <td><?= $item->tutoring_type->value ?></td>

        <td><?php echo $this->Form->input($key . '.lesson_plan', [
                'label' => '',
                'value' => $item->lesson_plan,
                'style' => 'width: 300px; height:50px;'
            ]) ?></td>
    </tr>
<?php endforeach; ?>

returned data

[
    (int) 0 => [
        'id' => '12397',
        'lesson_plan' => 'hello'
    ],
    (int) 1 => [
        'id' => '12408',
        'lesson_plan' => ''
    ],
    (int) 2 => [
        'id' => '14432',
        'lesson_plan' => ''
    ],
    (int) 3 => [
        'id' => '12419',
        'lesson_plan' => ''
    ],
    (int) 4 => [
        'id' => '12441',
        'lesson_plan' => ''
    ]
]  

Upvotes: 1

Views: 91

Answers (1)

AD7six
AD7six

Reputation: 66217

The reason for the error is $lessons is a query returning 5 objects. It's illogical to call save on it as if it were one lesson.

Save one entity at a time

If the submitted data is the structure shown in the question, code similar to this will do what you expect:

if ($this->request->is(['patch', 'post', 'put'])) {
    $patched = $this->Lessons->patchEntities($lessons, $this->request->data());
    foreach ($patched as $entity) {
        $this->Lessons->save($entity);
    }
}

Note that how to patch multiple entries is covered in the docs

Upvotes: 1

Related Questions