尝试为嵌套实体数组创建表单选择输入。我已经“手动”以这种方式实现了它,但它并没有感觉非常白皙。
<select name="restaurant_id" class="form-control" id="restauarnt-id">
<option value="">Select Restaurant</option>
<?php foreach($post->city->restaurants as $restauarnt): ?>
<option value="<?= $restauarnt->id?>"><?= $restauarnt->name ?></option>
<?php endforeach; ?>
</select>
这样的事情感觉更合适:
$this->Form->input('restauarnt_id', ['options' => $post->city->restaurants, 'empty' => 'Select Restaurant', 'class' => 'form-control', 'label' => false]);
但是这给了我:
<select name="restauarnt_id" class="form-control" id="restauarnt-id">
<option value="">Select Restaurant</option>
<option value="0">{"id": 1, "city_id": 1, "name": "Some Place"}</option>
</select>
最好的方法是什么?
(Cake Version 3.x)
答案 0 :(得分:0)
添加:
$restaurants = $this->Restaurants->find('list');
$this->set(compact('restaurants'));
在查看
中<?= $this->Form->input('restauarnt_id', ['type' => 'select', 'options' => $restaurants, 'empty' => __('Select Restaurant'), 'label' => false]) ?>
答案 1 :(得分:0)
:
$restaurants = $this->[(whatever $post is)]->Cities->Restaurants->find('list'
'keyField' => 'id',
'valueField' => 'name'
);
$this->set(compact('restaurants'));
在视图中
<?= $this->Form->input('retaurant_id', [
'empty' => '--',
'label' => 'Restaurants'
]); ?>