如何在add.ctp视图中将表用户中的所有用户显示到我的主题表中 [英] how to display all the users in table users to my subjects table in add.ctp view

查看:68
本文介绍了如何在add.ctp视图中将表用户中的所有用户显示到我的主题表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的主题add.ctp视图

Here is my subjects add.ctp view

<?= $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 ****

****UsersController.php ****

public function index()
{


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

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


}

如何实现这一目标,请帮助我...

How to achieve this one please help me...

推荐答案

如果您希望用户在主题add.ctp中选择box(Dropdown),则应该发送所有列表用户来自SubjectsController的add()。

If you want user select box(Dropdown) in the subject add.ctp then you should send all the list of user from add() of SubjectsController.

在主题中添加方法

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
}

在您的主题中添加。ctp

In your subject 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(用户模型)中,如下添加/编辑您的initialize()

In your UsersTable(Users Model), add/edit your initialize() as follow

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

这篇关于如何在add.ctp视图中将表用户中的所有用户显示到我的主题表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆