在CakePHP中显示用户名而不是用户id

在CakePHP中显示用户名而不是用户id,cakephp,cakephp-3.0,cakephp-3.x,Cakephp,Cakephp 3.0,Cakephp 3.x,嘿,我是cakePHP新手,我正在学习书签教程,但我使用的不是书签表,而是产品表, 这里我想要的是,在添加新产品时,我想在输入字段user\u id中显示username,而不是user\u id <?= $this->Form->create($product) ?> <fieldset> <legend><?= __('Add Product') ?></legend> <?php

嘿,我是cakePHP新手,我正在学习书签教程,但我使用的不是书签表,而是产品表, 这里我想要的是,在添加新产品时,我想在输入字段user\u id中显示username,而不是user\u 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() ?>

用户稳定

id,
用户名,
电子邮件,
密码

我遵循了
$username=$this Users->find('list')
但我真的不明白如何编写这个查询。
我需要运行什么查询?,

选择输入采用key=>value格式的数组。因此,要以该格式获取
$users
数组,您可以在控制器中执行以下操作:

$query = $this->Users->find('list', [
    'keyField' => 'id',
    'valueField' => 'username'
]);
$users = $query->toArray();
例如,这将为您提供以下数组:

[
    1 => 'Javed',
    2 => 'Test user'
]

选择输入采用key=>value格式的数组。因此,要以该格式获取
$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]);  

现在将调出用户表中的所有用户

您也可以这样做

UsersTable.php

 public function initialize(array $config)
    {       
         $this->displayField('username'); 
    }
然后是代码

echo $this->Form->input('user_id', ['options' => $users]);  

现在将调出用户表中的所有用户

首先,这取决于您的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() ?>
//在html视图端,jsut必须输入字段名,而不需要写入选项参数cakephp自动检测并填充数据
如果您使用的是其他版本,请添加注释。您的版本也将帮助您解决您的版本问题

希望这对你有帮助


谢谢

首先,这取决于您的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() ?>
//在html视图端,jsut必须输入字段名,而不需要写入选项参数cakephp自动检测并填充数据
如果您使用的是其他版本,请添加注释。您的版本也将帮助您解决您的版本问题

希望这对你有帮助


谢谢

请确保使用
列表
方法获得
$users
。另外,请确保Users表的
显示字段
用户名
谢谢@yBrodsky,当然,表的diplayField是用户名,我遵循了命名约定,现在帮我弄清楚,我需要做什么?显示您的查询以获取
$Users
先生,我还没有编写任何查询,我刚烤好了桌子。检查一下你的控制器。那$users是从某处提交的。查看@FrankSunnyman的回答,确保使用
列表
方法获得
$users
。另外,请确保Users表的
显示字段
用户名
谢谢@yBrodsky,当然,表的diplayField是用户名,我遵循了命名约定,现在帮我弄清楚,我需要做什么?显示您的查询以获取
$Users
先生,我还没有编写任何查询,我刚烤好了桌子。检查一下你的控制器。那$users是从某处提交的。看看@FrankSunnyman的回答好的it帮助和那个想法,是我的朋友@greg smith他去年帮了我好的it帮助和那个想法,是我的朋友@greg smith他去年帮了我