cakephp保存不同表中的数据
我在保存package和image表中输入的数据时遇到问题,因为我想同时保存这两个表的数据。我有一张放包装和图像的桌子。图像表由id、package_id和Image组成。包有很多图像,图像属于包 这是函数add_包:cakephp保存不同表中的数据,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我在保存package和image表中输入的数据时遇到问题,因为我想同时保存这两个表的数据。我有一张放包装和图像的桌子。图像表由id、package_id和Image组成。包有很多图像,图像属于包 这是函数add_包: if ($this->request->is('post')) { $this->Package->create(); if ($this->Package->save($this->request->
if ($this->request->is('post')) {
$this->Package->create();
if ($this->Package->save($this->request->data)) {
$this->Image->create();
if(empty($this->data['Image']['image']['name'])) {
unset($this->request->data['Image']['image']);
}
if(!empty($this->data['Image']['image']['name'])) {
$filename = $this->request->data['Image']['image']['name'];
$new_filename = STring::uuid().'-'.$filename;
$file_tmp_name = $this->request->data['Image']['image']['tmp_name'];
$dir = WWW_ROOT.'img'.DS.'uploads';
move_uploaded_file($file_tmp_name,$dir.DS.$new_filename);
$this->request->data['Image']['package_id'] = $this->Package->id;
$this->request->data['Image']['image'] = $new_filename;
if($this->Package->Image->save($this->request->data)) {
return $this->redirect(array('action' => 'admins'));
}
}
//return $this->redirect(array('action' => 'admins'));
}else {
$this->Session->setFlash(__('The package could not be saved. Please, try again.'));
}
}
这是add_package函数的视图:
<?php echo $this->Form->create('Package');?>
<fieldset>
<legend><?php echo __('Create a Package'); ?></legend>
<table cellpadding=12>
<?php
echo $this->Html->tableCells(array(
array(
'Name of the Package: ',
$this->Form->input('name', array('label' => false, 'class' => 'textbox'))
),
array(
'Description: ',
$this->Form->input('description', array('label' => false, 'rows' => '15'))
),
array(
'Places: ',
$this->Form->input('Place', array('label'=>false, 'type'=>'select', 'multiple' => 'checkbox', 'options' => $places))
),
array(
'Inclusions: ',
$this->Form->input('Inclusion', array('label'=>false, 'type'=>'select', 'multiple' => 'checkbox', 'options' => $inclusions))
),
array(
'Departure Time: ',
$this->Form->input('departure_time', array('label' => false, 'class' => 'drop'))
),
array(
'Price: ',
$this->Form->input('price', array('label' => false, 'class' => 'textbox'))
),
));
?>
</table>
<?php
echo $this->Form->create('Image',array('enctype'=>'multipart/form-data'));
echo $this->Form->input('image', array('label'=> false, 'type' => 'file'));
?>
<?php
echo $this->Form->Submit(__('Create Package'), array('class' => 'button'));
?>
</fieldset>
我试图同时保存包和图像。最初,我所做的是保存包的数据,然后将图像添加到包中,但现在我想同时保存包和图像,但我无法正确获取它。我已经做了半天了。什么都可以,谢谢。请随意提问 阅读cakephp文档
在控制器中,请使用:
if($this->Package->saveAll($this->request->data)) {
return $this->redirect(array('action' => 'admins'));
}
你的表格是:
<?php echo $this->Form->create('Package',array('type' => 'file');?>
<fieldset>
<legend><?php echo __('Create a Package'); ?></legend>
<table cellpadding=12>
<?php
echo $this->Html->tableCells(array(
array(
'Name of the Package: ',
$this->Form->input('name', array('label' => false, 'class' => 'textbox'))
),
array(
'Description: ',
$this->Form->input('description', array('label' => false, 'rows' => '15'))
),
array(
'Places: ',
$this->Form->input('Place', array('label'=>false, 'type'=>'select', 'multiple' => 'checkbox', 'options' => $places))
),
array(
'Inclusions: ',
$this->Form->input('Inclusion', array('label'=>false, 'type'=>'select', 'multiple' => 'checkbox', 'options' => $inclusions))
),
array(
'Departure Time: ',
$this->Form->input('departure_time', array('label' => false, 'class' => 'drop'))
),
array(
'Price: ',
$this->Form->input('price', array('label' => false, 'class' => 'textbox'))
),
));
?>
</table>
<?php
echo $this->Form->input('Image.image', array('label'=> false, 'type' => 'file'));
?>
<?php
echo $this->Form->Submit(__('Create Package'), array('class' => 'button'));
?>
</fieldset>
<?php
echo $this->Form->end();
?>
嗨,我试过了,很不错。图像表中的包id已保存,但我选择的文件不会出现。也许是时候使用上载插件了?