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