在 CakePHP 中显示用户名而不是 user_id
Display username instead of user_id in CakePHP
嘿,我是 cakePHP 的新手,我正在学习书签教程,但我使用的不是书签 table,而是产品 table,
我想要的是在添加新产品时,我想在输入字段 user_id 中显示 username 而不是 user_id .
<?= $this->Form->create($product) ?>
<fieldset>
<legend><?= __('Add Product') ?></legend>
<?php
echo $this->Form->input('user_id', ['options' => $users]);
echo $this->Form->input('title');
echo $this->Form->input('description');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
UsersTable 有
id,
username,
email,
password
我关注了$username = $this-Users->find('list')
但我真的不明白如何写这个查询。
我需要什么查询 运行?,
select 输入采用键 => 值格式的数组。因此,要获得该格式的 $users
数组,您可以在控制器中执行以下操作:
$query = $this->Users->find('list', [
'keyField' => 'id',
'valueField' => 'username'
]);
$users = $query->toArray();
例如,这会给你这个数组:
[
1 => 'Javed',
2 => 'Test user'
]
你也可以这样做
UsersTable.php
public function initialize(array $config)
{
$this->displayField('username');
}
然后代码
echo $this->Form->input('user_id', ['options' => $users]);
现在会拉起用户table
中的所有用户
首先要看你的cakephp版本,
这是 cakephp 2.X 版本
的解决方案
要在您的案例中显示用户名而不是 id,您必须分配用户数据列表
查询如下:
//In your controller action code
$users = $this->Users->find('list', array(
'fields' => array('id', 'username')
'recursive' => 0
));
$this->set(compact('users'));
这将在 html
中获得输出
//In your html view side jsut have to field name not required to write options parameter cakephp auto detect that and filled up the data
<?= $this->Form->create($product) ?>
<fieldset>
<legend><?= __('Add Product') ?></legend>
<?php
echo $this->Form->input('user_id', ['empty' => __('Select username')]);
echo $this->Form->input('title');
echo $this->Form->input('description');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
如果您使用的是其他版本,不用担心,请添加评论您的版本也将帮助您解决您的版本
希望对您有所帮助
谢谢
嘿,我是 cakePHP 的新手,我正在学习书签教程,但我使用的不是书签 table,而是产品 table, 我想要的是在添加新产品时,我想在输入字段 user_id 中显示 username 而不是 user_id .
<?= $this->Form->create($product) ?>
<fieldset>
<legend><?= __('Add Product') ?></legend>
<?php
echo $this->Form->input('user_id', ['options' => $users]);
echo $this->Form->input('title');
echo $this->Form->input('description');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
UsersTable 有
id,
username,
email,
password
我关注了$username = $this-Users->find('list')
但我真的不明白如何写这个查询。
我需要什么查询 运行?,
select 输入采用键 => 值格式的数组。因此,要获得该格式的 $users
数组,您可以在控制器中执行以下操作:
$query = $this->Users->find('list', [
'keyField' => 'id',
'valueField' => 'username'
]);
$users = $query->toArray();
例如,这会给你这个数组:
[
1 => 'Javed',
2 => 'Test user'
]
你也可以这样做
UsersTable.php
public function initialize(array $config)
{
$this->displayField('username');
}
然后代码
echo $this->Form->input('user_id', ['options' => $users]);
现在会拉起用户table
中的所有用户首先要看你的cakephp版本, 这是 cakephp 2.X 版本
的解决方案要在您的案例中显示用户名而不是 id,您必须分配用户数据列表
查询如下:
//In your controller action code
$users = $this->Users->find('list', array(
'fields' => array('id', 'username')
'recursive' => 0
));
$this->set(compact('users'));
这将在 html
中获得输出//In your html view side jsut have to field name not required to write options parameter cakephp auto detect that and filled up the data
<?= $this->Form->create($product) ?>
<fieldset>
<legend><?= __('Add Product') ?></legend>
<?php
echo $this->Form->input('user_id', ['empty' => __('Select username')]);
echo $this->Form->input('title');
echo $this->Form->input('description');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
如果您使用的是其他版本,不用担心,请添加评论您的版本也将帮助您解决您的版本
希望对您有所帮助
谢谢