我需要帮助。我有2张桌子。 项和 item_picture 。名称为 item_picture 的表格具有属于item的外键 item_id 。我有一个向导,其中包含项目表的字段,如标题,描述,地址和item_picture表的字段,如header_pic_1,header_pic_2,header_pic_3。在一个视图中。
代码如下:
项目型号:
<?php
App::uses('AppModel', 'Model');
class Item extends AppModel {
public $name = 'Item';
public $primaryKey = 'id';
public $displayField = 'title';
public $validate = array(
'id' => array(
'blank' => array(
'rule' => 'blank',
'on' => 'create',
),
),
'title' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Item name can only contain letters, numbers and spaces.',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'The Item name must not be empty.',
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This Item name already exists.',
),
),
'item_description' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Item description can only contain letters, numbers and spaces.',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Description can not be Empty',
),
),
'media_path' => array(
'uploadError' => array(
'rule'=>'uploadError',
'message' => 'The File Did NOT Upload. Please Try Again!',
'allowEmpty'=>false,
'on' => 'create',
),
'processMediaUpload'=>array(
'rule' =>'processMediaUpload',
'message'=>'Uploading File Failed!',
'allowEmpty'=>true,
),
),
'address' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'You Should Put an Address',
),
),
'user_id' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Not Empty',
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*/
public $belongsTo = array(
'ItemUser' => array(
'className' => 'User',
'foreignKey' => 'user_id',
)
);
/**
* hasMany associations
*/
public $hasOne = array(
'ItemPictures' => array(
'className' => 'ItemPicture',
'foreignKey' => 'item_id',
'dependent' => false,
),
);
//at the bottom of my Upload model
}
item_picture 型号:
<?php
App::uses('AppModel', 'Model');
class ItemPicture extends AppModel {
public $name = 'ItemPicture';
public $primaryKey = 'id';
public $displayField = 'header_title';
public $validate = array(
'id' => array(
'blank' => array(
'rule' => 'blank',
'on' => 'create',
),
),
'item_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Choose the item this gallery belongs to?',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
),
),
'header_title' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Item name can only contain letters, numbers and spaces.',
),
'maxLength' => array(
'rule' => array('maxLength', 100),
'message' => 'The Header Title must not be longer than 100 characters.',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'The Header Title must not be empty.',
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This Header Title already exists.',
),
),
'parallax1_text' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Paralaxe name can only contain letters, numbers and spaces.',
'allowEmpty'=>TRUE,
),
'maxLength' => array(
'rule' => array('maxLength', 100),
'message' => 'The Paralaxe name must not be longer than 100 characters.',
'allowEmpty'=>TRUE,
),
),
'parallax2_text' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Paralaxe name can only contain letters, numbers and spaces.',
'allowEmpty'=>TRUE,
),
'maxLength' => array(
'rule' => array('maxLength', 100),
'message' => 'The Paralaxe name must not be longer than 100 characters.',
'allowEmpty'=>TRUE,
),
),
'parallax3_text' => array(
'words' => array(
'rule' => array('custom', '/[0-9A-Za-z\._-]/'),
'message' => 'The Paralaxe name can only contain letters, numbers and spaces.',
'allowEmpty'=>TRUE,
),
'maxLength' => array(
'rule' => array('maxLength', 100),
'message' => 'The Paralaxe name must not be longer than 100 characters.',
'allowEmpty'=>TRUE,
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
public $belongsTo = array(
'ItemItemPicture' => array(
'className' => 'Item',
'foreignKey' => 'item_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/*******************************************************************
////////// HERE IS THE OBJECT SLIDER PROCESS IMAGE ///////////////
*********************************************************************
public function processHeaderPic1Upload($slidercheck=array()){
// if there is no video uploaded return False
if(!is_uploaded_file($slidercheck['header_pic_1']['tmp_name'])){
return FALSE;
}
// then if the file has been uploaded lets try to move it to it's destination
// We will put some logic to see if the move(uploading) fails
if(!move_uploaded_file($slidercheck['header_pic_1']['tmp_name'], WWW_ROOT. 'img/uploads/item/pictures/headslider/' . $slidercheck['header_pic_1']['name'])){
// if move didnt happened @return False
return FALSE;
}
//At this point the move is successfull and the model is going to save the data
//now we will overload the data cuz the file is in type array
$this->data[$this->alias]['header_pic_1']='uploads/item/pictures/headslider/' . $slidercheck['header_pic_1']['name'];
//@return TRUE since the operation was sucesfull
return TRUE;
}
我构建的视图是:项目 add.ctp
<div class="content-header">
<a id="menu-toggle" href="#" class="btn btn-info">Menu<i class="icon-reorder"></i></a><h1> Add Item</h1>
</div>
<div class="container">
<!-- Tabs -->
<?php echo $this->Form->create('Item', array('type'=>'file', 'id'=>'itemWizard','class'=>'gllpLatlonPicker')); ?>
<?php echo $this->Form->input('id'); ?>
<div id="wizard" class="swMain">
<ul>
<li><a href="#step-1">
<span class="stepNumber">1</span>
<span class="stepDesc">
Generalle <br />
<small>Fill your account details</small>
</span>
</a></li>
<li><a href="#step-2">
<span class="stepNumber">2</span>
<span class="stepDesc">
Galleria Header<br />
<small>Insert Gallery images</small>
</span>
</a></li>
<li><a href="#step-3">
<span class="stepNumber">3</span>
<span class="stepDesc">
Parallaxe<br />
<small>Insert Parallaxe Images</small>
</span>
</a></li>
<li><a href="#step-4">
<span class="stepNumber">4</span>
<span class="stepDesc">
Blocco 2<br />
<small>Fill your characteristice details</small>
</span>
</a></li>
<li><a href="#step-5">
<span class="stepNumber">5</span>
<span class="stepDesc">
Blocco 3<br />
<small>Fill localization information</small>
</span>
</a></li>
<li><a href="#step-6">
<span class="stepNumber">6</span>
<span class="stepDesc">
Venditori<br />
<small>Select item sellers</small>
</span>
</a></li>
</ul>
<div id="step-1">
<h2 class="StepTitle">Step 1: Generalle</h2>
<table >
<tr>
<td align="right">Item Title :</td>
<td align="left">
<?php echo $this->Form->input('Item.title',array('class'=>'txtBox','id'=>'item_title','required'=>'required' ,'placeholder'=>'Item Title','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_title"></span> </td>
</tr>
<tr>
<td align="right">Item Description :</td>
<td align="left">
<?php echo $this->Form->input('Item.seo_description',array('class'=>'txtBox','id'=>'item_seo_description','required'=>'required' ,'placeholder'=>'Item Description','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_seo_description"></span> </td>
</tr>
<tr>
<td align="right">Item Location:</td>
<td align="left">
<?php echo $this->Form->input('Item.item_location_id',array('empty'=>true,'class'=>'txtBox','id'=>'item_locationId','placeholder'=>'Item Description','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_locationId"></span> </td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td align="left">
<?php echo $this->Form->input('Item.seo_keywords',array('class'=>'txtBox','placeholder'=>'Item Keywords','label'=>false)); ?>
</td>
<td align="left"></td>
</tr>
</table>
</div>
<div id="step-2">
<h2 class="StepTitle">Step 2: Header Gallery images</h2>
<table>
<tr>
<td align="right">Header Title :</td>
<td align="left">
<?php echo $this->Form->input('ItemPicture.header_title',array('class'=>'txtBox','id'=>'item_header_title' ,'placeholder'=>'Header Title','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_header_title"></span> </td>
</tr>
<tr>
<td align="right">Header Slider Image (1) :</td>
<td align="left">
<div id="attachment<?php echo 1;?>" >
<?php echo $this->Form->input('ItemPicture.header_pic_1',array('type'=>'file','class'=>'txtBox','id'=>'item_headerImage1' ,'label' => false,'div' => false));?>
<div id="attachmentlink<?php echo 1;?>"></div>
</div>
</td>
<td align="left"><span id="msg_item_headerImage1"></span> </td>
</tr>
<tr>
<td align="right">Header Slider Image (2) :</td>
<td align="left">
<div id="attachment<?php echo 2;?>" >
<?php echo $this->Form->input('ItemPicture.header_pic_2',array('type'=>'file','class'=>'txtBox','id'=>'item_headerImage2' ,'label' => false,'div' => false));?>
<div id="attachmentlink<?php echo 2;?>"></div>
</div>
</td>
<td align="left"><span id="msg_item_headerImage2"></span> </td>
</tr>
<tr>
<td align="right">Header Slider Image (3) :</td>
<td align="left">
<div id="attachment<?php echo 3;?>" >
<?php echo $this->Form->input('ItemPicture.header_pic_3',array('type'=>'file','class'=>'txtBox','id'=>'item_headerImage3','label' => false,'div' => false));?>
<div id="attachmentlink<?php echo 3;?>"></div>
</div>
</td>
<td align="left"><span id="msg_item_headerImage3"></span> </td>
</tr>
<?php for($i=4; $i<7; $i++) { ?>
<tr>
<td align="right">Header Slider Image (<?php echo $i; ?>) :</td>
<td align="left">
<div id="attachment<?php echo $i;?>" <?php if($i !=4) echo "style='display:none;'";?> >
<?php echo $this->Form->input('ItemPicture.header_pic_'.$i,array('type'=>'file','class'=>'txtBox','id'=>'item_headerImage' . $i ,'label' => false,'div' => false));?>
<div id="attachmentlink<?php echo $i;?>" <?php if($i==6) echo "style='display:none;'";?>>
<a href="javascript:void(0);" onclick="show('attachment<?php echo $i+1;?>'); hide('attachmentlink<?php echo $i;?>');">Add More</a>
</div>
</div>
</td>
<td align="left"><span id="msg_item_headerImage<?php echo $i; ?>"></span> </td>
</tr>
<?php } ?>
</table>
</div>
<div id="step-3">
<h2 class="StepTitle">Step 3: Parallaxe Images</h2>
<table>
<?php for ($i= 1; $i < 4; $i++) { ?>
<tr>
<td align="right">Paralaxe Text :</td>
<td align="left">
<?php echo $this->Form->input('ItemPicture.parallax'.$i.'_text', array('class'=>'txtBox','id'=>'item_parallax'.$i.'text' ,'label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_parallax<?php echo $i; ?>text"></span> </td>
</tr>
<tr>
<td align="right">Parallaxe Picture :</td>
<td align="left">
<?php echo $this->Form->input('ItemPicture.parallax'.$i.'_pic', array('type'=>'file','class'=>'txtBox','id'=>'item_parallax'.$i.'pic','label' => false)); ?>
</td>
<td align="left"><span id="msg_item_parallax<?php echo $i; ?>pic"></span> </td>
</tr>
<tr>
<td align="right">Parallaxe Pattern :</td>
<td align="left">
<?php echo $this->Form->input('ItemPicture.parallax'.$i.'_pattern' , array('type'=>'checkbox','class'=>'txtBox','id'=>'item_parallax'.$i.'patern','label' => false)); ?>
</td>
<td align="left"><span id="msg_item_parallax<?php echo $i; ?>patern"></span> </td>
</tr>
<hr />
<?php } ?>
</table>
</div>
<div id="step-4">
<h2 class="StepTitle">Step 4: Bloco 2</h2>
<table>
<tr>
<td align="right">Descrizione Intervento * :</td>
<td align="left">
<?php echo $this->Form->input('Item.item_description',array('class'=>'txtBox','id'=>'item_description','required'=>'required' ,'placeholder'=>'Item Description','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_description"></span> </td>
</tr>
<tr>
<td align="right">Item Characteristics :</td>
<td align="left">
<?php echo $this->Form->input('Item.item_characteristic_id',array('empty'=>true,'class'=>'txtBox','id'=>'item_characteristic' ,'placeholder'=>'Item Description','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_characteristic"></span> </td>
</tr>
<tr>
<td align="right">Video Path :</td>
<td align="left">
<?php echo $this->Form->input('Item.video_path',array('type'=>'text','class'=>'txtBox','id'=>'item_video' ,'placeholder'=>'Paste here video path','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_video"></span> </td>
</tr>
<tr>
<td align="right">Media Path :</td>
<td align="left">
<?php echo $this->Form->input('Item.media_path',array('type'=>'file','id'=>'imgInp','required'=>'required' ,'label'=>false)); ?>
<?php echo "<img id=\"showImg\" src=\"#\" alt=\" \" style=\"max-height:100px; class=\"img-rounded img-responsive\" \" />" ; ?>
</td>
<td align="left"><span id="msg_imgImp"></span> </td>
</tr>
<tr>
<td align="right">Broshure Path :</td>
<td align="left">
<?php echo $this->Form->input('Item.brochure_path',array('type'=>'file','id'=>'item_broshure','required'=>'required' ,'label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_broshure"></span> </td>
</tr>
</table>
</div>
<div id="step-5">
<h2 class="StepTitle">Step 5: Blocco 3</h2>
<table>
<tr>
<td align="right" style="width:15%">Descrizione Localizzazione * :</td>
<td align="left" style="width:70%">
<?php echo $this->Form->input('Item.location_description',array('class'=>'txtBox','id'=>'location_description','required'=>'required' ,'placeholder'=>'Item Description','label'=>false)); ?>
</td>
<td align="left" style="width:15%"><span id="msg_location_description"></span> </td>
</tr>
<tr>
<td align="right" style="width:15%">address :</td>
<td align="left" style="width:70%">
<?php echo "<input type=\"button\" class=\"gllpSearchButton\" value=\"search\">"; ?>
<?php echo $this->Form->input('Item.address',array('id' => 'address','class'=>'gllpSearchField','label'=>false)); ?>
<?php echo "<input type=\"button\" class=\"gllpUpdateButton\" value=\"update map\">"; ?>
<?php echo $this->Form->input('Zoom',array('type'=>'text', 'class'=>'gllpZoom', 'value'=>'3')); ?>
</td>
<td align="left" style="width:15%"><span id="msg_address"></span> </td>
</tr>
<tr>
<td align="right" style="width:15%">Google Maps :</td>
<td style="width:85%" colspan="2">
<?php echo "<div class=\"gllpMap\">Google Maps</div>"; ?>
</td>
</tr>
<tr>
<td align="right" style="width:15%">longitude :</td>
<td align="left" style="width:70%">
<?php echo $this->Form->input('Item.longitude',array('class'=>'txtBox','id' => 'longitude','class'=>'gllpLongitude','placeholder'=>'longitude','label'=>false)); ?>
</td>
<td style="width:15%"><span id="msg_item_longitude"></span> </td>
</tr>
<tr>
<td align="right" style="width:15%">latitude:</td>
<td align="left" style="width:70%">
<?php echo $this->Form->input('Item.latitude',array('class'=>'txtBox','id' => 'latitude','placeholder'=>'latitude','class'=>'gllpLatitude','label'=>false)); ?>
</td>
<td style="width:15%"><span id="msg_item_latitude"></span> </td>
</tr>
</table>
</div>
<div id="step-6">
<h2 class="StepTitle">Step 6: Venditori</h2>
<table>
<tr>
<td align="right">Venditoro 1 :</td>
<td align="left">
<?php echo $this->Form->input('Item.first_seller_id',array('empty'=>true,'class'=>'txtBox','id'=>'item_seller1','required'=>'required','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_seller1"></span> </td>
</tr>
<tr>
<td align="right">Venditoro 2 :</td>
<td align="left">
<?php echo $this->Form->input('Item.second_seller_id',array('empty'=>true,'class'=>'txtBox','id'=>'item_seller2','required'=>'required','label'=>false)); ?>
</td>
<td align="left"><span id="msg_item_seller2"></span> </td>
</tr>
</table>
</div>
</div>
<?php echo $this->Form->submit(__('Add Item'),array('class'=>'btn btn-success','after'=>$this->Html->link('Cancel',array('controller'=>'items','action'=>'index'),array('class'=>'btn btn-default')))); ?>
<?php echo $this->Form->end(); ?>
<!-- End SmartWizard Content -->
</div>
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#showImg').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){readURL(this);
});
</script>
<script>
function show(target){document.getElementById(target).style.display = 'block';}
function hide(target){document.getElementById(target).style.display = 'none'; }
</script>
** 解决问题的方法:
function add() {
$this->layout='item_add';
if ($this->request->is('post')) {
if (!empty($this->data)) {
$this->ItemPicture->create();
// FOR HEADER SLIDER
for($i=1;$i<7;$i++)
{ $dir = 'img/uploads/item/pictures/headslider/';
if(empty($this->data['ItemPicture']['header_pic_'.$i]['name'])){
unset($this->request->data['ItemPicture']['header_pic_'.$i]);
}
if(!empty($this->data['ItemPicture']['header_pic_'.$i]['name']))
{
$allowedExts = array('jpeg', 'png', 'jpg', 'gif');
$extension=strtolower(end(explode(".", $this->data['ItemPicture']['header_pic_'.$i]['name'])));
$file=$this->data['ItemPicture']['header_pic_'.$i];
if(in_array($extension, $allowedExts))
{
if ($file["error"] <= 0) {
move_uploaded_file($file['tmp_name'], WWW_ROOT . $dir . mktime().$file['name']);
$this->request->data['ItemPicture']['header_pic_'.$i] = mktime().$file['name'];
}
}
}
}
// FOR PARRALAXE
for($i=1;$i<4;$i++){
$dirparallaxe = 'img/uploads/item/pictures/parallaxe/';
if(empty($this->data['ItemPicture']['parallax'.$i.'_pic']['name'])) {
unset($this->request->data['ItemPicture']['parallax'.$i.'_pic']);
}
if(!empty($this->data['ItemPicture']['parallax'.$i.'_pic']['name'])) {
$allowedExts = array('jpeg', 'png', 'jpg', 'gif');
$extension=strtolower(end(explode(".", $this->data['ItemPicture']['parallax'.$i.'_pic']['name'])));
$parallaxe=$this->data['ItemPicture']['parallax'.$i.'_pic'];
if(in_array($extension, $allowedExts)){
if ($parallaxe["error"] <= 0) {
move_uploaded_file($parallaxe['tmp_name'], WWW_ROOT . $dirparallaxe . mktime().$parallaxe['name']);
$this->request->data['ItemPicture']['parallax'.$i.'_pic'] = mktime().$parallaxe['name'];
}
}
}
}
unset($this->ItemPicture->validate['item_id']);
$this->request->data['Item']['user_id'] = $this->Auth->user('id');
if($this->Item->save($this->request->data)){
$this->request->data['ItemPicture']['item_id'] = $this->Item->getLastInsertId();
if ($this->ItemPicture->save($this->data)) {
$this->Session->setFlash(__('The profile has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The profile could not be saved. Please, try again.', true));
} // END OF ItemPicture->save
} else {
$this->Session->setFlash(__('The profile could not be saved. Please, try again.', true));
} // END OF Item->save
}
}
//$this->loadModel("Item");
$itemLocations = $this->Item->ItemLocation->find('list');
$itemCharacteristics = $this->Item->ItemCharacteristic->find('list');
$firstSellers = $this->Item->FirstSeller->find('list');
$secondSellers = $this->Item->SecondSeller->find('list');
$users = $this->Item->ItemUser->find('list');
$this->set(compact('itemLocations', 'itemCharacteristics', 'firstSellers', 'secondSellers', 'users'));
//if the user is admin or superadmin, show all on dropdown
if ( AuthComponent::user('role')==='admin' ||AuthComponent::user('role')==='superadmin' ){
$items = $this->ItemPicture->ItemItemPicture->find('list');
} else {// else if the user is author, show only item created by him.
$items = $this->ItemPicture->ItemItemPicture->find('list', array('conditions' => array('ItemItemPicture.user_id' => AuthComponent::user('id'))));
}
$this->set(compact('items'));
} // END OF ADD ACTION
因此需要在 ItemPicturesController 中完成处理,因为它将foregin kew保存到item表中。在那里我使用
public $uses = array('ItemPicture','Item');
在itemPictureController中使用模型,现在正在做我想要的。
答案 0 :(得分:1)
Read the chapter in the book about saving your data,您的情况很好explained there。
您的表单必须生成该结构(示例):
$data = array(
'Item' => array('title' => 'My first article'),
'ItemPicture' => array(
array('image' => 'test'),
array('image' => 'test2'),
array('image' => 'test3'),
),
);
您必须为表单输入命名,如:
$this->Form->input('Image.' . $i . ' . 'image');
你的控制器添加方法看起来不太好,它也应该进入模型方法。你想要胖模特,瘦小的控制器。