如何在 cakephp 的添加表单中显示关联数据 3.x

How to show associated data in add form in cakephp 3.x

我有两个表

CREATE TABLE `user_roles` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `role_name` varchar(20) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=latin1

CREATE TABLE `users` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `username` varchar(20) DEFAULT NULL,
   `email` varchar(255) DEFAULT NULL,
   `password` varchar(255) DEFAULT NULL,
   `user_role_id` int(11) DEFAULT NULL,
   `status` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

我为用户角色创建了插入和删除。不,我必须添加具有用户角色的用户。在用户添加表单中,用户角色显示为空。这是我的代码: UserTable.php: 命名空间 App\Model\Table;

            use Cake\ORM\Table;
        use Cake\Validation\Validator;

        class UsersTable extends Table
        {
            public function initialize(array $config)
           {
              $this->hasMany('UserRoles', [
                    'foreignKey' => 'id',
                    'sort' => ['UserRoles.role_name', 'ASC']
                ]);
                $this->displayField('role_name');
        }   }

        UserController.php

        public function add()
        {
            $this->set('title', 'Add Users');
            $users = $this->Users->newEntity();
            if($this->request->is('post')):
                $users = $this->Users->patchEntity($user, $this->request- 
                 >data);
                if($this->Users->save($user)):
                    $this->Flash->success(__('User was created 
                     successfully.'));
                    return $this->redirect(['action' => 'index']);
                endif;
            endif;
            $this->set('users', $this->users);
            $this->set('user_roles', $this->Users->UserRoles->find('list', 
             ['id', 'role_name']));
        }
            **add.ctp**
        <?php 
        //print_r($user_roles);
        echo $this->Form->create($users, ['class' => 'form']);
            echo $this->Form->input('username');
        echo $this->Form->input('email');
        echo $this->Form->input('user_role_id');
        echo $this->Form->input('password');
        echo $this->Form->button(__("<i class='fa fa-save'></i> Save"));
        echo $this->Form->end();
        ?>
        **UserRolesTable.php**
        namespace App\Model\Table;

        use Cake\ORM\Table;
        use Cake\Validation\Validator;

        class UserRolesTable extends Table
        {
        public function initialize(array $config)
        {
        $this->belongsToMany('Users', [
                'foreignKey' => 'user_role_id'
            ]);
        }

        public function validationDefault(Validator $validator)
        {
        $validator->notEmpty('role_name', 'Role name cannot be empty.');
        return $validator;
        }}

显示 user_role_id 的空 select 框。请帮助我。

您必须在您的视图中为您的输入添加选项:

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

而不是

echo $this->Form->input('user_role_id');

您需要对 View 变量进行驼峰式拼写才能使魔术发生的选项,因此它应该是 $userRoles 而不是 $user_roles:-

$this->set('userRoles', $this->Users->UserRoles->find('list', ['id', 'role_name']));

然后 Cake 就会知道将变量与表单字段相关联。否则,您将必须按照 Jun 的建议指定用于选项的变量。