CakePHP 4 两次保存数据
CakePHP 4 Saving Data Twice
CakePHP 以某种方式将相同的数据保存了两次。出于某种原因,我想实现此添加方法,以便在有人直接转到 domain.com/recordings/add
时立即保存 $dummy
它看起来很简单,我一直在摸不着头脑。我检查了验证错误;我试过禁用验证;我试过使用 patchEntity()
代替。
但是,一件奇怪的事情是,如果您通过点击 domain.com/recordings/index
中的 add recording
按钮(而不是在浏览器),数据只保存一次。
控制器:
public function add()
{
$dummy = [
"user_id" => 1,
"title" => "tgfbthgdthb",
"body" => "rgcvfghfhdxcgb",
"published" => 0,
];
$recording = $this->Recordings->newEntity($dummy);
$this->Recordings->save($recording);
}
Model/table:
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('recordings');
$this->setDisplayField('title');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
]);
$this->hasMany('Words', [
'foreignKey' => 'recording_id',
]);
}
Model/entity:
protected $_accessible = [
'user_id' => true,
'title' => true,
// 'slug' => true,
'body' => true,
'published' => true,
'created' => true,
'modified' => true,
'user' => true,
'words' => true,
];
观点:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Recording $recording
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('List Recordings'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column-responsive column-80">
<div class="recordings form content">
<?= $this->Form->create($recording) ?>
<fieldset>
<legend><?= __('Add Recording') ?></legend>
<?php
echo $this->Form->control('user_id', ['options' => $users]);
echo $this->Form->control('title');
echo $this->Form->control('body');
echo $this->Form->control('published');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
不要试图迎合人们的懒惰,不要让他们通过简单地访问 URL 来保存数据,即通过 GET
请求,那只会引起麻烦,并且最重要的是,它的应用程序设计很糟糕。
至少实施适当的保护措施,只为 POST
请求保存数据。
浏览器可能会在各种情况下发出多个请求,从预检 OPTIONS
请求到奇怪的怪癖,例如如果在响应数据的前 x 个字节中找不到任何编码信息,Firefox 就会中止请求,然后发出一个新请求,该请求采用特定的响应编码。
public function add()
{
if ($this->request->is('post')) {
// save data here
}
}
CakePHP 以某种方式将相同的数据保存了两次。出于某种原因,我想实现此添加方法,以便在有人直接转到 domain.com/recordings/add
它看起来很简单,我一直在摸不着头脑。我检查了验证错误;我试过禁用验证;我试过使用 patchEntity()
代替。
但是,一件奇怪的事情是,如果您通过点击 domain.com/recordings/index
中的 add recording
按钮(而不是在浏览器),数据只保存一次。
控制器:
public function add()
{
$dummy = [
"user_id" => 1,
"title" => "tgfbthgdthb",
"body" => "rgcvfghfhdxcgb",
"published" => 0,
];
$recording = $this->Recordings->newEntity($dummy);
$this->Recordings->save($recording);
}
Model/table:
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('recordings');
$this->setDisplayField('title');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
]);
$this->hasMany('Words', [
'foreignKey' => 'recording_id',
]);
}
Model/entity:
protected $_accessible = [
'user_id' => true,
'title' => true,
// 'slug' => true,
'body' => true,
'published' => true,
'created' => true,
'modified' => true,
'user' => true,
'words' => true,
];
观点:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Recording $recording
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('List Recordings'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column-responsive column-80">
<div class="recordings form content">
<?= $this->Form->create($recording) ?>
<fieldset>
<legend><?= __('Add Recording') ?></legend>
<?php
echo $this->Form->control('user_id', ['options' => $users]);
echo $this->Form->control('title');
echo $this->Form->control('body');
echo $this->Form->control('published');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
不要试图迎合人们的懒惰,不要让他们通过简单地访问 URL 来保存数据,即通过 GET
请求,那只会引起麻烦,并且最重要的是,它的应用程序设计很糟糕。
至少实施适当的保护措施,只为 POST
请求保存数据。
浏览器可能会在各种情况下发出多个请求,从预检 OPTIONS
请求到奇怪的怪癖,例如如果在响应数据的前 x 个字节中找不到任何编码信息,Firefox 就会中止请求,然后发出一个新请求,该请求采用特定的响应编码。
public function add()
{
if ($this->request->is('post')) {
// save data here
}
}