无法在cakephp3中更新关联的hasMany数据

时间:2016-07-11 06:06:40

标签: cakephp cakephp-3.0

我无法保存(更新)与主模型具有hasMany关系的模型AvailabilityForTutors的关联表单数据。我使用复选框更新了' AvailabilityForTutors的一些选项。 NOT_AVAILABLE'领域。我添加了id,但没有区别。更新的' AvailabilityForTutors。 NOT_AVAILABLE'字段未在数据库中更新。我没有收到错误,导师的主要模型数据确实存在。

我检查过这个简单任务的文档和过去的帖子,我无法得到它。我做错了。

//controller
  $tutor = $this->Tutors->get($id, [
        'contain' => ['AvailabilityForTutors']
    ]);
  if ($this->request->is(['patch', 'post', 'put'])) {


         debug($this->request->data);
            $tutor = $this->Tutors->patchEntity($tutor, $this->request->data,['associated' => ['AvailabilityForTutors'],'validate' => false ] );

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


        }

//view

  <?php

            echo $this->Form->hidden('id', ['value'=>$id]);
            echo $this->Form->input('first_name',['label' => 'Tutor FirstName']);
            echo $this->Form->input('last_name',['label' => 'Tutor LastName']);




            foreach ($tutor->availability_for_tutors as $key => $item) {

               echo $this->Form->hidden('AvailabilityForTutors.'.$key.'.id', ['value'=>$item->id]);

               echo $this->Form->input('AvailabilityForTutors.'.$key.'.not_available', ['label'=>$item->weekday,
                'checked'=> $item->not_available,'type'=>'checkbox']);



           }


model tutor//

 $this->hasMany('AvailabilityForTutors', [
            'foreignKey' => 'tutor_id'
        ]);


posted data which is correct


'id' => '12',
    'first_name' => 'fred',
    'last_name' => 'Tow',
    'AvailabilityForTutors' => [
        (int) 0 => [
            'id' => '36',
            'not_available' => '1'
        ],
        (int) 1 => [
            'id' => '37',
            'not_available' => '0'
        ],
        (int) 2 => [
            'id' => '38',
            'not_available' => '0'
        ],
http://stackoverflow.com/questions/27398100/cakephp-3-0-cant-save-hasmany-associated-data
http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-with-associations

http://stackoverflow.com/questions/35018335/update-a-hasmany-records

UPDATE-调试修补数据 对象(App \ Model \ Entity \ Tutor){

'id' => (int) 12,
'tutor_inactive' => false,
'first_name' => 'fred2',
'last_name' => 'Tow2',

'availability_for_tutors' => [
    (int) 0 => object(App\Model\Entity\AvailabilityForTutor) {

        'id' => (int) 36,
        'not_available' => false,
        ....
],
'AvailabilityForTutors' => [
    (int) 0 => [
        'id' => '36',
        'not_available' => '1'

//请参阅not_available与修补数据输出

不同

3 个答案:

答案 0 :(得分:0)

也许其中一种方法可以帮助你我猜你需要更换 http://api.cakephp.org/3.2/class-Cake.ORM.Association.HasMany.html

我尝试的方式和你一样,但失败了,但是链接()对我有用(对于HABTM)

答案 1 :(得分:0)

尝试在$tutor->dirty('availability_for_tutors', true);功能之后在Controller中插入patchEntity。 它会将您的关联标记为“脏”(已修改),以便在调用$this->Tutors->save方法时将其保存。

答案 2 :(得分:0)

         I have 2 solutions
1st

  public function edittest2($id = null)
    {
        $tutor = $this->Tutors->get($id, [
            'contain' => ['AvailabilityForTutors']
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {


       //  debug($this->request->data);
            $tutor = $this->Tutors->patchEntity($tutor, $this->request->data(),['associated' => ['AvailabilityForTutors']] );
         //    $tutor->dirty('availability_for_tutors', true);

        //  debug($tutor);
             $this->Tutors->save($tutor,[
                        'atomic'=>false,
                        'validate' => false,
                        'associated' => ['AvailabilityForTutors']
                    ]); 


             return $this->redirect(['action' => 'edittest2',$id]);
        }



 view

 foreach ($tutor->availability_for_tutors as $key => $item) {

               echo $this->Form->hidden('availability_for_tutors.'.$key.'.id', ['value'=>$item->id]);

                echo $this->Form->input('availability_for_tutors.'.$key.'.not_available', ['label'=>$item->weekday,
                    'checked'=> $item->not_available,'type'=>'checkbox']);


           }


      2nd 
         public function edittest4($id = null)
            $tutor = $this->Tutors->get($id, [
                'contain' => ['AvailabilityForTutors']
            ]);


            $availability=$tutor['availability_for_tutors'];


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


                $tutor = $this->Tutors->patchEntity($tutor, $this->request->data(),['validate' => false ] );

                 $this->Tutors->save($tutor); 

                 $newavailability = $this->Tutors->AvailabilityForTutors->patchEntities(  $availability,$tutor->AvailabilityForTutors, ['validate' =>  false]); //multiple entities
                foreach ($newavailability as $key => $item) {
                   $result=$this->Tutors->AvailabilityForTutors->save($item, ['checkExisting' => false]);

                    if ( $result ){
                       $this->Flash->success(__('A lesson has been saved on .'));

                    }
                    else{
                      $this->Flash->error(__('There has been an error saving a lesson. Please, try again.'));
                    }//if

                 }//for each



                 return $this->redirect(['action' => 'edittest4',$id]);
            }


    view
    ///
     foreach ($availability as $key => $item) {
                 //  debug($item->toArray());
                echo $this->Form->hidden('AvailabilityForTutors.'.$key.'.id', ['value'=>$item->id]);

                      echo $this->Form->input('AvailabilityForTutors.'.$key.'.not_available', ['label'=>$item->weekday,
                        'checked'=> $item->not_available,'type'=>'checkbox']);


               }