我正在使用MongoDB数据库结构通过CakePHP2创建一个Cookbook。 我已经制作了食谱控制器,创建了视图,创建了其他功能(编辑,添加等)。现在,我可以正确添加食谱,但是当我单击旁边的编辑链接时,也可以对其进行编辑我的食谱中,所有内容均正常显示,但是如果字段为空,我不希望显示字段。
在我的数据库中,我的标准结构如下:
食谱
名称:类型:准备时间:烹饪时间:
蔬菜:肉类:其他:香料:
餐具:电器:其他:
从本质上讲,它只是echo $ this-> Form-> input字段,您可以在其中键入与数据库中该数据段相对应的值。
我要解决的问题:
显然,使用不同的食谱,您将不会在甜点中添加“肉”,或者在其他菜肴中也不会包含蔬菜等。
当我单击“编辑配方”,并且我的所有字段都弹出时,我希望隐藏空字段。
如果单击“巧克力蛋奶酥”食谱的“编辑食谱”,则其中没有肉类或蔬菜,如果数据库中没有该值,则我不希望这些输入字段显示。
这是整个edit.ctp代码:
echo $this->Form->create('Recipe');
echo '
echo $this->Form->input('Name');
echo '
echo $this->Form->input('Type');
echo '
echo $this->Form->input('preptime', array('value' => $this->request->data['Recipe']['Time']['preptime']));
echo $this->Form->input('cooktime', array('value' => $this->request->data['Recipe']['Time']['cooktime']));
echo '
echo 'Vegetables';
echo $this->Form->input('Vegetables.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Vegetables']));
echo $this->Form->input('Vegetables.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Vegetables']));
echo 'Meats';
echo $this->Form->input('Meats.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Meats']));
echo $this->Form->input('Meats.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Meats']));
echo 'Misc';
echo $this->Form->input('Misc.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Misc']));
echo $this->Form->input('Misc.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Misc']));
echo 'Spices';
echo $this->Form->input('Spices.', array('values' => $this->request->data['Recipe']['Instructions']['Ingredients']['Spices']));
echo $this->Form->input('Spices.', array('values' => $this->request->data['Recipe']['Instructions']['Ingredients']['Spices']));
echo '
echo 'Cutlery';
echo $this->Form->input('Cutlery.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Cutlery']));
echo $this->Form->input('Cutlery.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Cutlery']));
echo 'Appliances';
echo $this->Form->input('Appliances.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Appliances']));
echo $this->Form->input('Appliances.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Appliances']));
echo 'Misc';
echo $this->Form->input('Misc.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Misc']));
echo $this->Form->input('Misc.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Misc']));
echo $this->Form->end('Save Recipe');
这是RecipeController代码:
公共功能edit($ id = null){
if (!$id) {
throw new NotFoundException(__('Invalid Recipe'));
}
$recipes = $this->Recipe->findById($id);
if (!$recipes) {
throw new NotFoundException(__('Invalid Recipe'));
}
if ($this->request->is(array('post','put'))) {
$this->Recipe->id = $id;
if ($this->Recipe->save($this->request->data)) {
$this->Flash->success(__('Your Recipe has been updated.'));
return $this->redirect(array(
'action' => 'index'
));
}
$this->Flash->error(__('You did something WRONG!'));
}
if (!$this->request->data) {
$this->request->data = $recipes;
}
}
我需要更改一些小事情,这显然不是完美的,但是我现在的主要事情是使用条件隐藏空字段。
代码是CakePHP2,数据库是MongoDB。
谢谢!