我遇到了一个问题。我有一个表单和表单字段名称card_number
。 card_number
来自数据库。提交card_number
的入境卡号。在我的ctp文件中,我写了这个。
<div class="form-group">
<label class="col-sm-2 control-label"> Entry Card Number </label>
<div class="col-md-10">
<?php echo $this->Form->input('card_number', array('options' => $readCard,'class'=>'form-control','div'=>false,'label'=>false));?>
</div>
</div>
我已在Controller中编写此代码。我的观点看起来像上面的图像。
public function add_card_to_device($btsId = null){
$readerData = $this->CardManagement->find('list',
array(
'conditions' => array('site_name'=>$site_name),
'fields' => array('card_number'),
'keyField' => 'card_number',
'valueField' => 'card_number'
)
);
debug($readerData);
$this->set('readCard', $readerData);
if ($this->request->is('post')|| $this->request->is('put')) {
$dataa = $this->request->data;
print_r($dataa);
}
}
单击“提交”按钮时出现问题。当我单击提交按钮时,我得到这些值。我得到了id值而不是card_number [card_number] =&gt; 5a13b3d9-67ac-4847-b3f9-1870991894ac
Array ( [CardManagement] => Array ( [id] => 5a12d321-a7e0-4cf6-ab84-1870991894ac [site_name] => 1235 [card_number] => 5a13b3d9-67ac-4847-b3f9-1870991894ac ) )
但是我想要的输出看起来像这样。 [card_number] =&gt; 6473088
Array ( [CardManagement] => Array ( [id] => 5a12d321-a7e0-4cf6-ab84-1870991894ac [site_name] => 1235 [card_number] => 6473088 ) )
请提出任何解决方案或建议。
答案 0 :(得分:2)
Cakephp 3.x
调用list时,您可以分别使用keyField和valueField选项配置用于键和值的字段:
https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs
$readerData = $this->CardManagement->find('list',
array(
'conditions' => array('site_name'=>$site_name),
'fields' => array('card_number'),
'keyField' => 'card_number',
'valueField' => 'card_number'
)
);
Cakephp 2.x
https://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list
$readerData = $this->CardManagement->find('list',
array(
'conditions' => array('site_name' => $site_name),
'fields' => array('card_number','card_number'),
)
);