【发布时间】:2015-02-04 13:11:32
【问题描述】:
我在保存包和图像表中输入的数据时遇到问题,因为我想同时保存两者的数据。我有一张用于包装和图像的表格。 Image 表由(id、package_id 和 image)组成。 Package有很多Image,Image属于Package。
这是函数add_package:
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 cakephp-2.0