cakephp formhelper:在编辑时从输入文件获取当前文件
cakephp formhelper : getting current file from input file on edit
我有一个可以提交图片的表单,这不是我最初的代码,我正在继承它,我正在努力解决它并解决问题。
编辑此表单中的数据时,输入文件与其他输入文件不同,为空。所以保存实际上将当前文件替换为数据库中的"array"。
我希望能够保留当前的,如果它没有改变?
表单编辑:
<h1>Edit</h1>
<?php
echo $this->Form->create($template,['enctype' => 'multipart/form-data']);
echo $this->Form->input('title');
echo $this->Form->input('works_id');
echo $this->Form->input('photo',['type' => 'file']);
echo $this->Form->input('text', ['rows' => '4']);
echo $this->Form->input('url');
echo $this->Form->button(__('Save'));
echo $this->Form->end();
?>
从控制器编辑功能:
public function edit($id = null){
$template = $this->Templates->get($id);
if ($this->request->is(['post', 'put'])) {
$template = $this->Templates->patchEntity($template, $this->request->data,['associated' => ['Works']]);
if ($this->Templates->save($template)) {
$this->Flash->success(__('Your template has been updated.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to update your template.'));
}
$this->set('template', $template);
$works = $this->Templates->Works->find('list');
$this->set(compact('works'));
}
在我的数据库中,它保存在两个列中:
file_dir 和文件名,所以我更困惑如何在编辑表单中获取文件。
编辑:添加模板表:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class TemplatesTable extends Table
{
protected function _initializeSchema(\Cake\Database\Schema\Table $table){
$table->columnType('photo', 'proffer.file');
return $table;
}
public function initialize(array $config){
$this->addBehavior('Timestamp');
$this->addBehavior('Proffer.Proffer', [
'photo' => [
'root' => WWW_ROOT.'files',
'dir' => 'photo_dir',
'thumbnailSizes' => [
'square' => ['w' => 200, 'h' => 200],
'portrait' => ['w' => 100, 'h' => 300, 'crop' => true],
],
'thumbnailMethod' => 'gd' // Options are Imagick, Gd or Gmagick
]
]);
$this->belongsTo(
'Works',
[
'foreignKey' => 'work_id',
]
);
}
public function validationDefault(Validator $validator){
$validator
->notEmpty('work_id')
->notEmpty('title');
return $validator;
}
}
根据文档,Proffer
行为可以开箱即用地处理空上传,您只需相应地配置该字段的验证规则以允许它在更新时为空:
If you want your users to submit a file when creating a record, but
not when updating it, you can configure this using the basic Cake
rules.
$validator
->requirePresence('image', 'create')
->allowEmpty('image', 'update');
So now your users do not need to upload a file every time they update
a record.
- https://github.com/davidyell/CakePHP3-Proffer/blob/0.8.2/docs/validation.md
- https://github.com/davidyell/CakePHP3-Proffer/blob/0.8.2/src/Model/Behavior/ProfferBehavior.php#L56
所以在你的情况下是:
->requirePresence('photo', 'create')
->allowEmpty('photo', 'update')
我有一个可以提交图片的表单,这不是我最初的代码,我正在继承它,我正在努力解决它并解决问题。
编辑此表单中的数据时,输入文件与其他输入文件不同,为空。所以保存实际上将当前文件替换为数据库中的"array"。
我希望能够保留当前的,如果它没有改变?
表单编辑:
<h1>Edit</h1>
<?php
echo $this->Form->create($template,['enctype' => 'multipart/form-data']);
echo $this->Form->input('title');
echo $this->Form->input('works_id');
echo $this->Form->input('photo',['type' => 'file']);
echo $this->Form->input('text', ['rows' => '4']);
echo $this->Form->input('url');
echo $this->Form->button(__('Save'));
echo $this->Form->end();
?>
从控制器编辑功能:
public function edit($id = null){
$template = $this->Templates->get($id);
if ($this->request->is(['post', 'put'])) {
$template = $this->Templates->patchEntity($template, $this->request->data,['associated' => ['Works']]);
if ($this->Templates->save($template)) {
$this->Flash->success(__('Your template has been updated.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to update your template.'));
}
$this->set('template', $template);
$works = $this->Templates->Works->find('list');
$this->set(compact('works'));
}
在我的数据库中,它保存在两个列中: file_dir 和文件名,所以我更困惑如何在编辑表单中获取文件。
编辑:添加模板表:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class TemplatesTable extends Table
{
protected function _initializeSchema(\Cake\Database\Schema\Table $table){
$table->columnType('photo', 'proffer.file');
return $table;
}
public function initialize(array $config){
$this->addBehavior('Timestamp');
$this->addBehavior('Proffer.Proffer', [
'photo' => [
'root' => WWW_ROOT.'files',
'dir' => 'photo_dir',
'thumbnailSizes' => [
'square' => ['w' => 200, 'h' => 200],
'portrait' => ['w' => 100, 'h' => 300, 'crop' => true],
],
'thumbnailMethod' => 'gd' // Options are Imagick, Gd or Gmagick
]
]);
$this->belongsTo(
'Works',
[
'foreignKey' => 'work_id',
]
);
}
public function validationDefault(Validator $validator){
$validator
->notEmpty('work_id')
->notEmpty('title');
return $validator;
}
}
根据文档,Proffer
行为可以开箱即用地处理空上传,您只需相应地配置该字段的验证规则以允许它在更新时为空:
If you want your users to submit a file when creating a record, but not when updating it, you can configure this using the basic Cake rules.
$validator ->requirePresence('image', 'create') ->allowEmpty('image', 'update');
So now your users do not need to upload a file every time they update a record.
- https://github.com/davidyell/CakePHP3-Proffer/blob/0.8.2/docs/validation.md
- https://github.com/davidyell/CakePHP3-Proffer/blob/0.8.2/src/Model/Behavior/ProfferBehavior.php#L56
所以在你的情况下是:
->requirePresence('photo', 'create')
->allowEmpty('photo', 'update')