【问题标题】:how to display all the users in table users to my subjects table in add.ctp view如何在 add.ctp 视图中将表用户中的所有用户显示到我的主题表中
【发布时间】:2017-08-23 03:13:47
【问题描述】:

这是我的主题 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']);


}

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

【问题讨论】:

    标签: php cakephp cakephp-3.0


    【解决方案1】:

    如果您希望用户在 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
    }
    

    在您的主题中添加.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() ?>
    

    在您的用户表(用户模型)中,添加/编辑您的初始化()如下

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

    【讨论】:

    • 这应该可以工作..更好的是$users = $this-&gt;Subjects-&gt;Users-&gt;find('list'),但只有在主题和用户模型是associated时才有效
    • 但是我怎么知道我添加的记录是否属于那个人?例如,我在此下拉列表中选择用户 ID ibb.co/cSDhe5,然后这是我添加时主题表的输出。另请参阅图片上的用户表ibb.co/nCv9sQ
    • 我不确定你想要什么。尽管如此,您仍然执行以下操作,这将为您提供用户名下拉列表。我在上面的代码中添加了。 @JeromeManatad
    • 请看上面的2张图片。如果我选择属于 jovy@gmail.com 的 84 id .. 但在主题表中我无法分类属于 JOVY 的记录,
    • 我已经写好了答案,阅读上面代码的最后3行。实现它并刷新主题/添加@JeromeManatad
    【解决方案2】:
    1. 在您的控制器

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

    2. 在您的视图

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

    【讨论】:

    • 未定义变量:allUser [APP/Template\Subjects\add.ctp, line 19]...我将您的代码放在 usersController.php 的方法索引中,然后在 Subjects 的 add.ctp 视图中
    • $this-&gt;Users-&gt;find(); 确保此代码有效,我不确定您的型号。在使用之前检查$allUser 是否有数据,您可以使用蛋糕的调试套件进行调试debug($allUser); exit; 此代码将打印 $allUser 变量并显示在屏幕上...
    • 所以你需要debug($allUser-&gt;toArray())--->它会为你显示数据---如果你需要更多支持请联系我
    【解决方案3】:

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

    如果你不想做 foreach 循环。

    尝试:

    <?= $this->Form->select('users', $users); ?>
    

    【讨论】:

    • 它也说未定义
    猜你喜欢
    • 2013-05-19
    • 1970-01-01
    • 2018-03-27
    • 2019-06-23
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多