CakePHP 3 - 有条件地保存关联表中的数据

时间:2017-03-08 11:49:08

标签: mysql forms cakephp associations cakephp-3.0

我有以下表格:

  • 预订(hasOne Sessions)
  • 会话(hasOne Guest Staff)
  • 嘉宾员工

在我的表单中,我的输入字段指定如下:

  • 对于预订相关字段,其表单输入ID的格式为name
  • 对于与会话相关的字段,其表单输入ID的格式为session.name
  • 对于与访客职员相关的字段,其表单输入ID的格式为session.gueststaff.name

但是,并非所有Sessions都有Guest Staff,因此我只想创建一个newEntity并在提交时仅在填写字段时将数据保存到Guest Staff。目前,当我尝试提交Guest Staff输入时在,保存工作正常。但是当我尝试在没有任何Guest Staff输入的情况下提交表单时,保存失败。

在Controller中调试$ save只是给了我:false,这不是很有帮助。

查看POST数据,Guest Staff数组的形式为(基本上为空):

Guest Staff {
    firstname: 
    lastname:    
}

然后我尝试删除MySQL中的Not Null要求,然后重新烘焙Guest Staff模型。但是,当我尝试在此之后重新提交表单时,在表单能够保存时,仍然在Guest Staff表中创建了一个新的数据条目,只有一个ID和将其连接到Sessions的外键,而另一个字段是空白的,这不是我想要的,因为这意味着该表中会有很多无用的数据条目。

更新:根据Manohar Khadka的建议,我添加了if语句并且未设置。

目前在控制器中:

public function add()
    {
        if ($this->request->is('post')) {
            $data = $this->request->data;
            if(empty($data['session']['gueststaff']['firstname'])){
                unset($data['session']['gueststaff']);
                $booking = $this->Bookings->newEntity([
                    'associated' => [
                        'Sessions'
                    ]
                ]);
            } else {
                $booking = $this->Bookings->newEntity([
                    'associated' => [
                        'Sessions', 'Sessions.Gueststaff'
                    ]
                ]);
            }
            $booking = $this->Bookings->patchEntity($booking, $data, [
                'associated' => [
                    'Sessions', 'Sessions.Gueststaff'
                ]
            ]);
            $save = $this->Bookings->save($booking);
            if ($save) {
                $this->Flash->success(__('The booking was successfully completed.'));
                return $this->redirect(['controller' => 'bookings', 'action' => 'index']);
            } else {
                $this->Flash->error(__('Unfortunately, the booking could not be completed. Please try again.'));
            }
        }
    }

我的表格:

<?= $this->Form->create($booking) ?>
<legend><?= __('Session Details') ?></legend>
<?= $this->Form->input('session.location', ['class' => 'form-control']); ?>
<?= $this->Form->input('session.date', ['class' => 'form-control']); ?>                     

<h6>Warning: Fill in the following Guest Staff details if a Guest Staff member will be present.</h6>
<?= $this->Form->input('session.gueststaff.firstname', ['class' => 'form-control']); ?>
<?= $this->Form->input('session.gueststaff.lastname', ['class' => 'form-control']); ?>

<?= $this->Form->button('<strong>' . __('Complete Session Details') . '</strong>', ['id' => 'submit', 'class' => 'btn btn-primary']); ?>

<?= $this->Form->end() ?>

1 个答案:

答案 0 :(得分:1)

可能你可以更好地处理像{before 3之前的Callbacks方法那样的事情。

或者您可以在保存方法中检查字段是否为空,例如:

public function add()
{
    $booking = $this->Bookings->newEntity();
    if ($this->request->is('post')) {
        $data = $this->request->data;
        if(empty($data['session']['gueststaff']['firstname'])){
            unset($data['session']['gueststaff']);
            $booking = $this->Bookings->patchEntity($booking, $data, [
                'associated' => [
                    'Sessions'
                ]
            ]);
        } else {
            $booking = $this->Bookings->patchEntity($booking, $data, [
                'associated' => [
                    'Sessions', 'Sessions.Gueststaff'
                ]
            ]);
        }
        $save = $this->Bookings->save($booking);
        if ($save) {
            $this->Flash->success(__('The booking was successfully completed.'));
            return $this->redirect(['controller' => 'bookings', 'action' => 'index']);
        } else {
            $this->Flash->error(__('Unfortunately, the booking could not be completed. Please try again.'));
        }
       // $this->set(compact('booking'));
    }
}

在制作上述逻辑时,我假设输入字段名称如下:

<?= $this->Form->input('gueststaff.fname');?> //<input type="text" name="gueststaff[fname]">
<?= $this->Form->input('gueststaff.lname');?>

如果您愿意,您可以从客户端做这些事情。