1
我已經搜索了高和低的解決方案,但似乎無法得到這個想通了。我想要做的是在添加產品時,我希望名稱字段從表單中的輸入中填充。因此該名稱將包含用戶爲type_id,category_id和subcategory_id選擇的值。有誰知道一種方法來完成這個?Cakephp:結合輸入值創建隱藏名稱輸入
添加產品查看頁面
<fieldset>
<legend><?php echo __('Add Product'); ?></legend>
<?php
echo $this->Form->input('type_id');
echo $this->Form->input('category_id', array('label' => 'Vendor'));
echo $this->Form->input('subcategory_id', array('label' => 'Model'));
echo $this->Form->input('location', array('label' => 'Location'));
echo $this->Form->input('sku', array('label' => 'Asset Tag'));
echo $this->Form->input('mac');
echo $this->Form->input('description', array('label' => 'Notes'));
echo $this->Form->input('name', array('value' => ['type_id']['category_id'] , 'type' => 'hidden'));
//echo $this->Form->input('cost');
// echo $this->Form->input('Tag');
?>
</fieldset>
產品控制器添加功能
public function add() {
if ($this->request->is('post')) {
$this->Product->create();
if ($this->Product->save($this->request->data)) {
$this->Session->setFlash(__('The product has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}
}
$subcategories = $this->Product->Subcategory->find('list',array('order'=>'Subcategory.name asc'));
$categories = $this->Product->Category->find('list',array('order'=>'Category.name asc'));
$types = $this->Product->Type->find('list',array('order'=>'Type.name asc'));
$this->set(compact('subcategories', 'categories', 'types'));
}
真棒謝謝!這工作幾乎完全符合我的要求。你知道是否有可能用關聯的type_id和category_id替換它們各自的名稱而不是id? – rubyme8 2013-02-27 20:09:03
@ rubyme8是的,這是可能的,看到我更新的答案。 – Oldskool 2013-02-27 20:13:58