【问题标题】:Don't display the username when I user CakeDC/users plugin当我使用 CakeDC/users 插件时不显示用户名
【发布时间】:2017-11-08 14:36:14
【问题描述】:

enter image description here

您可以看到插件显示的是用户 ID 而不是用户名。我该如何解决这个问题。

控制器中的用户:

$users = $this->Histories->Users->find('list', ['limit' => 200]);
    $this->set(compact('history', 'companiesGrants', 'statuses', 'users', 'tags'));

以及在模板中的使用,在.ctp文件中

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

【问题讨论】:

    标签: php mysql cakephp model-view-controller orm


    【解决方案1】:

    可以在表格对象上使用setDisplayField()方法来配置表格的显示字段:

    public function initialize(array $config)
    {
        $this->setDisplayField('username');
        // Prior to 3.4.0
        $this->displayField('usernme');
    }
    

    或者您可以分别使用keyFieldvalueField 选项配置用于键和值的字段:

    $users = $this->Histories->Users->find('list', [
        'keyField' => 'id',
        'valueField' => 'username'
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多