我的表结构如下所示:
============================== Id | Name | Image| ==============================
现在我想要一个带有名字的多个图像。我怎么能这样做。
if($this->request->is('post'))
{
if($this->Breakdown->saveAll($this->request->data['Breakdown']))
{
//Image insertion Successfully
$image=$this->request->data['Breakdown'][0]['image1']['name'];
if(!empty($this->request->data['Breakdown'][0]['image1']['name']))
{
$image_path = $this->Image->upload_image_and_thumbnail($image,$this->data['Breakdown'][0]['image1'],600,450,180,120, "Breakdown");
//echo "This".$image_path;
if(isset($image_path))
{
$this->Breakdown->saveField('image',$image_path);
$uploaded = true;
}
}
$this->Session->setFlash('Quantity Breakdown has been added Successfully.', 'default', array('class' => 'oMsg1 oMsgError1'));
$this->redirect('qty_breakdown');
}
else
{
$this->set('errors', $this->Breakdown->invalidFields());
}
是否有其他方法可以使用循环来插入和上传多个图像?
答案 0 :(得分:1)
我不确定我理解你在问什么,但如果你的意思是,迭代每个'Breakdown'实体,我就是这样做的:
foreach($this->request->data['Breakdown'] as $breakdown){
//Image insertion Successfully
$image = $breakdown['image1']['name'];
if(!empty($breakdown['image1']['name'])){
$image_path = $this->Image->upload_image_and_thumbnail($image,$this->data['Breakdown'][0]['image1'],600,450,180,120, "Breakdown");
//echo "This".$image_path;
if(isset($image_path)){
$this->Breakdown->saveField('image', $image_path);
$uploaded = true;
}
}
$this->Session->setFlash('Quantity Breakdown has been added Successfully.', 'default', array('class' => 'oMsg1 oMsgError1'));
$this->redirect('qty_breakdown');
}
为了能够在视图中修改或添加多个记录,您需要以不同的方式配置视图:
// open the form
echo $this->Form->create('Breakdown');
// set the upper-bound limit of $i to whatever you like
for($i = 0; $i < 5; $i++){
echo $this->Form->input(sprintf("Breakdown.%d.image1", $i), array('type' => 'file'));
// add other fields using similar syntax as above:
// Example: "Breakdown.1.name"
}
// close the form
echo $this->Form->end();