2017-05-31 91 views
0

我正在cakephp v3上工作。我面临一个问题。 我想显示复选框的循环,但我得到的数据是在json中。cakephp 3在复选框中传递json

控制器

public function add() { 
     $building = $this->Buildings->newEntity(); 
     if ($this->request->is('post')) { 
      $building = $this->Buildings->patchEntity($building, $this->request->data); 
      if ($this->Buildings->save($building)) { 
       $this->Flash->success(__('The building has been saved.')); 
       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The building could not be saved. Please, try again.')); 
      } 
     } 
     $countries = $this->Buildings->Countries->find('list'); 
     $this->loadModel("Amenities"); 
     $amenities = $this->Amenities->find('All'); 
     $this->set(compact('building', 'countries','amenities')); 
     $this->set('_serialize', ['building','amenities']); 
    } 

查看

foreach ($amenities as $key => $value) { 
        echo $this->Form->control($value, ['type' => 'checkbox']); 
       } 

,输出是

enter image description here

我想在FOL输出低调的态度。

Amenities 

[checkbox] [checkboxname] [fa-icon] 

[checkbox] [checkboxname] [fa-icon] 

[checkbox] [checkboxname] [fa-icon] 
// so on 

我该如何做到这一点。

我可以通过下面的代码

echo $this->Form->input('country_id', ['options' => $countries, 'empty' => 'Select country']); 
+0

使用'$ data = json_decode($ your_json_string,TRUE);'它会返回数组,然后您可以根据需要迭代数组。 – Sam

+0

是的,我可以使用'json_decode',但我正在寻找任何默认'cakephp'方式,因为我用'selectbox'。 –

+0

请相应地更新您的问题。很明显,指定你已经尝试了什么,以及你在找什么。 – Sam

回答

0

control第一个参数是字段名称来设置选项。你正在传递一个实体,它将以最好的方式将它转换为一个字符串。你大概是想用$value->name