find(list)返回几个空白选项,find(all)返回正确的数据,但格式为{ [英] find(list) returns several blank options, find(all) returns correct data but formatted with {
本文介绍了find(list)返回几个空白选项,find(all)返回正确的数据,但格式为{的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
AvadiariesTable.php
$this->belongsTo('AlumnesGrups', [
'foreignKey' => 'alumnes_grup_id',
'joinType' => 'INNER'
AlumnesGrupsTable.php
$this->belongsTo('Alumnes', [
'foreignKey' => 'alumne_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Grups', [
'foreignKey' => 'grup_id',
'joinType' => 'INNER'
]);
为什么在AvadiariesController.php中是这样:
public function add()
{
$avadiary = $this->Avadiaries->newEntity();
if ($this->request->is('post')) {
$avadiary = $this->Avadiaries->patchEntity($avadiary, $this->request->data);
$avadiary->user_id = $this->Auth->user('id');
if ($this->Avadiaries->save($avadiary)) {
$this->Flash->success(__('The avadiary has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The avadiary could not be saved. Please, try again.'));
}
}
$alumnesGrups = $this->Avadiaries->AlumnesGrups->find('all', [
'fields' => ['Alumnes.name'],
'contain' =>['Alumnes', 'Grups'],
'conditions' => ['Grups.id =' => 1],
'order' => ['Alumnes.name' => 'ASC']
]);
$this->set(compact('avadiary', 'alumnesGrups'));
$this->set('_serialize', ['avadiary']);
}
返回正确的数据,但格式如下:
returning correct data but formatted like this:
{校友:{名称:安格拉·史密斯}}?
{"Alumnes":{"name":"Angela Smith"}}?
我怎样才能得到Angela Smith?如果我将find(all)更改为find(list),则选择框将填充几个空白选项。
How can I just get Angela Smith? If I change find(all) to find(list), the selectbox is populated with several blank options.
谢谢!
推荐答案
使用 find('list')
并使用 valueField
要显示的字段,并使用 keyField
为其值:
Use find('list')
and use valueField
for the field you want to show and keyField
for its value:
$alumnesGrups = $this->Avadiaries->AlumnesGrups->find('list', [
'keyField' => 'alumne.name',
'valueField' => 'alumne.name'])
->contain(['Alumnes', 'Grups'])
->where(['Grups.id =' => 1])
->order(['Alumnes.name' => 'ASC']);
检查:
http://book.cakephp.org/3.0/zh-CN/orm/retrieving-data- and-resultsets.html#finding-key-value-pairs
这篇关于find(list)返回几个空白选项,find(all)返回正确的数据,但格式为{的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文