如何在 add.ctp 视图中向我的主题 table 显示 table 用户中的所有用户

how to display all the users in table users to my subjects table in add.ctp view

这是我的主题add.ctp查看

<?= $this->Form->create($subject) ?>
<fieldset>
    <legend><?= __('Add Subject') ?></legend>
    <?php
        echo $this->Form->input('math');
        echo $this->Form->input('english');
        echo $this->Form->input('history');
        echo $this->Form->input('science');

       ******this field will display all the users in drop down************* from users table
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

****UsersController.php ****

public function index()
{


     $user=  $this->set('users', $this->paginate());

    $this->set('user', $user);
    $this->set('_serialize', ['user']);


}

如何实现这一点请帮助我...

使用$this->Form->select() 或 $this->Form->input('users', ['options' => array()]);

如果你不想做foreach循环。

尝试:

<?= $this->Form->select('users', $users); ?>
  1. 在您的控制器

    $allUser = $this->Users->find(); // this will be get all user from user table $this->set('allUser', $allUser);

  2. 在您的 View

    /**@var array $allUser */// already type of array <?= $this->Form->select('all_user', [ 'multiple' => true, 'value' => $allUser ]); ?>

如果你想要用户 select 框(下拉)在主题 add.ctp 那么你应该从 SubjectsController 的 add() 发送所有用户列表。

在科目中添加方法

public function add()
{
    //Your previous logics      

    $this->loadModel('Users');
    $users=  $this->Users->find('list');

    $this->set('users', $users);
    $this->set('_serialize', ['users']);
    //Your previous logics
}

在你的主题中add.ctp

<?= $this->Form->create($subject) ?>
<fieldset>
    <legend><?= __('Add Subject') ?></legend>
    <?php
        echo $this->Form->input('math');
        echo $this->Form->input('english');
        echo $this->Form->input('history');
        echo $this->Form->input('science');
        echo $this->Form->input('user_id', ['options' => $users]);
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

在您的 UsersTable(用户模型)中,add/edit您的 initialize() 如下

$this->displayField('username');
$this->primaryKey('id');