gpt4 book ai didi

php - CakePHP 3 - 有条件地为关联表创建新实体

转载 作者:行者123 更新时间:2023-11-28 23:17:25 24 4
gpt4 key购买 nike

我有这三个表:

  • 预订(有一个 session )
  • 具有链接到 Bookings 的外键 booking_id 的 session (hasOne 文件)
  • 具有链接到 session 的外键 session_id 的文件(属于 session )

在 BookingsController 中,我有两个函数:

  • New(功能同add功能)
  • 确认(类似编辑功能)

当用户首次提交新的预订数据条目时,会为预订和 session 创建并保存新实体,但不会为文件创建和保存新实体。但是,当更新和确认预订/ session 时,即第一次创建文件中的新实体时。由于 Confirm 函数可以多次使用,我使用条件 if 语句来确定关联的 Files 条目是否存在 - 如果存在,则创建、修补并保存 Files 的 newEntity。如果没有,它只是被修补并保存。

在我的确认函数中:

public function confirm($id = null)
{
$booking = $this->Bookings->get($id,[
'contain' => ['Sessions']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$booking = $this->Bookings->patchEntity($booking, $this->request->data,[
'contain' => ['Sessions']
]);
$fileTable = TableRegistry::get('Files');
$findFiles = $fileTable->find()->where([
'session_id' => $session['id']
])->first();
if($findFiles == null){
$findFiles = $fileTable->newEntity();
$findFiles = $fileTable->patchEntity($findFiles, $data);
$findFiles->session_id = $booking['session']['id'];
if($fileTable->save($findFiles)){

} else {

}
} else {
$findFiles = $filesTable->patchEntity($findFiles, $data);
if($filesTable->save($findFiles)){

} else {

}
}
if ($this->Bookings->save($booking)) {
$this->Flash->success(__('The booking has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The booking could not be saved. Please, try again.'));
}
$this->set(compact('booking'));
$this->set('_serialize', ['booking']);
}
}

但是,当我尝试使用 Confirm 函数时,我发现 Sessions 表中的 booking_id 外键违反了完整性约束。我已经指出,通过删除所有关于 Files 表的条件保存代码,该函数可以正常工作,但这意味着在需要时都不会创建 Files 的 newEntity。

我认为更简单的方法是只在新功能中包含文件,但由于某些预订可能会被取消,因此可能会有很多空的文件数据条目。

更新:包括模型和确认函数的 View 和表单输入。

下面是 BookingsController 中确认函数的 View :

<?= $this->Form->create($booking) ?>
<fieldset>
<legend><?= __('Confirm Booking') ?></legend>
<?php
echo $this->Form->input('session.startdate', ['class'=>'form-control']);
echo $this->Form->input('session.enddate', ['class'=>'form-control']);
echo $this->Form->input('session.no_people', ['class'=>'form-control']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

以及各个表的模型。

预订:

public function initialize(array $config)
{
parent::initialize($config);

$this->table('bookings');
$this->displayField('id');
$this->primaryKey('id');

$this->addBehavior('Timestamp');

$this->hasOne('Sessions', [
'foreignKey' => 'booking_id'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');

return $validator;
}

session :

public function initialize(array $config)
{
parent::initialize($config);

$this->table('sessions');
$this->displayField('id');
$this->primaryKey('id');

$this->addBehavior('Timestamp');

$this->belongsTo('Bookings', [
'foreignKey' => 'booking_id',
'joinType' => 'INNER'
]);
$this->hasOne('Templates', [
'foreignKey' => 'session_id'
]);
}

public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');

$validator
->date('startdate')
->requirePresence('startdate', 'create')
->notEmpty('startdate');

$validator
->date('enddate')
->requirePresence('enddate', 'create')
->notEmpty('enddate');
$validator
->integer('no_people')
->requirePresence('no_people', 'create')
->notEmpty('no_people');

return $validator;
}

public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['booking_id'], 'Bookings'));
return $rules;
}

文件:

public function initialize(array $config)
{
parent::initialize($config);

$this->table('files');
$this->displayField('id');
$this->primaryKey('id');

$this->addBehavior('Timestamp');

$this->belongsTo('Sessions', [
'foreignKey' => 'session_id',
'joinType' => 'INNER'
]);
}

public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');

$validator
->allowEmpty('link');

$validator
->allowEmpty('name');

return $validator;
}

public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['session_id'], 'Sessions'));

return $rules;
}

最佳答案

外键 session_id 是必需的属性(这就是文件永远无法保存的原因)。我已将函数更改为以下内容:

public function confirm($id = null)
{
$booking = $this->Bookings->get($id,[
'contain' => ['Sessions', 'Sessions.Files']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
if($booking->session->file == null){ //checks for existing Files array
$template = $this->Bookings->Sessions->Files->newEntity(); //creates new Files entity if one doesn't exist for the associated session
$template->session_id = $booking->session->id; //fills in the foreign key with the currently active session primary key.
if($this->Bookings->Sessions->Templates->save($template)){ //saves the File entity

} else {

}
}
$booking = $this->Bookings->patchEntity($booking, $this->request->data,[
'contain' => ['Sessions', 'Sessions.Files']
]);
$fileTable = TableRegistry::get('Files');
$file = $fileTable->find('all',[
'contain' => ['Sessions'],
'conditions' => ['Sessions.booking_id' => $booking->id, 'Files.session_id' => $booking->session->id]
])->first();
if($file->session_id != $data['session']['id']){ //checks for any changes in session_id
$file->engineer_id = $data['session']['id']; //changes value to match if there isn't a match
$fileTable->save($template);
}
if ($this->Bookings->save($booking)) {
$this->Flash->success(__('The booking has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The booking could not be saved. Please, try again.'));
}
$this->set(compact('booking'));
$this->set('_serialize', ['booking']);
}
}

关于php - CakePHP 3 - 有条件地为关联表创建新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com