添加具有特定角色id[CakePHP]的用户

添加具有特定角色id[CakePHP]的用户,php,mysql,cakephp,Php,Mysql,Cakephp,我正在尝试构建一个部分,在这里我们可以添加角色为2的用户 角色\u ID位于另一个名为User\u Details的表中,并作为外键与当前用户表ID链接 下面是我的UserController.php的代码 查询中出现错误: 有关于如何构建此表的建议吗?您可以使用bindModel功能来关联或链接两个类似的表 $this->User->bindModelarray'belongsTo'=>array'UserDetail'=>array'className'=>UserDetail', 'for

我正在尝试构建一个部分,在这里我们可以添加角色为2的用户

角色\u ID位于另一个名为User\u Details的表中,并作为外键与当前用户表ID链接

下面是我的UserController.php的代码

查询中出现错误:


有关于如何构建此表的建议吗?

您可以使用bindModel功能来关联或链接两个类似的表

$this->User->bindModelarray'belongsTo'=>array'UserDetail'=>array'className'=>UserDetail', 'foreignKey'=>'Role_ID', ; 现在,当您找到用户表的数据时,它也会显示userdetail数据。 如果需要userdetail模型的特定字段,还可以使用Containeable行为

首先检查userdetail的表名,如果表名类似于userdetails,则可以在bindModel中使用类似于userdetails的类名 如果您使用像这样的表名user\u details,而不是像这样的类名UserDetails。您能给出一个函数在其中查找用户数据并在特定函数中使用bind model函数吗?应该加载模型


请检查表名

请更新用户型号代码:-

用户模型User.php

然后更新控制器代码

下面是UserController.php的代码


请共享用户模型并查看代码更多有关输入的详细信息@apuravgaurGot错误通知8:未定义变量:data[APP/Controller/userscocontroller.php,第1137行]您使用的是哪个cakephp版本?
function addUser(){

        $this->set('setTab','addUser');
        $this->set('parent_tab','Manage Users');
        $this->set('tab','Add User');


        // Super admin doesn't have an access of Manage user section
        if($this->Session->read('User.access_rights') == 4){
            $this->redirect('dashboard');
        }

        $this->layout = 'user';
        $this->set('setTab','manageUsers');
        $this->set('statusArray', $this->statusArray);

        /*CREATE USER DROP DOWN ARRAY START*/
        /*$userDrop['0']='Select Parent';
        $getUsers = $this->User->generatetreelist('User.access_rights <> 4',null,'{n}.User.username','-');
        if($getUsers) {
            foreach ($getUsers as $key=>$value){
                $userDrop[$key] = $value;
            }
            $this->set(compact('userDrop'));
        }*/
        /*CREATE USER DROP DOWN ARRAY END*/

        if(!$this->Session->check('User')){
            $this->redirect('login');
        }

        if($this->data){
                    $data = $this->data;
                    $this->User->set($data);

                    if ($this->User) {
                        // pr($this->data); die;

                                //  because we like to send pretty mail
                                    //Set view variables as normal

                                     $this->set('userDetails', $data['User']);

                                    //$this->User->recursive = 1;

                                    if($this->User->saveAll($data)){

                                        $this->Session->setFlash('User added successfully', 'default', array('class' => 'errMsgLogin'));
                                        $this->redirect('manageUsers');
                                    }
                                    else {
                                        echo "User not added";
                                    }
                    }

                    else {

                        // do nothing
                    }
        }
    }//Ends here
<?php
class User extends AppModel{
          var $name = "User";





         var $validate = array(
        'Username' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'required' => false,
                'message' => 'Username can not be empty!',
            ), 
             'maxLength'=> array(
                'rule' => array('maxLength', 20),
                'message' => 'Username can not be longer that 20 characters.'
             )
        ),
        'First_Name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'First name can not be empty!',
                )
        ),
        /*'phone' => array(
        'numeric' => array(
            'rule' => 'numeric',
            'message' => 'Numbers only'
        ),
            'rule' => array('minLength', 10),
            'message' => 'Phone number should be of 10 digits'
        ),*/
        'Last_Name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Last name can not be empty!',
                )
        ),

        'Email_Id' => array(
             'notempty' => array(
             'rule' => array('email'),
             'allowEmpty' => false,
             'message' => 'Please Enter a valid Email Address'
                )
        )
        /*'status'=> array(
             'notempty' => array(
             'rule' => array('notEmpty'),
             'allowEmpty' => false,
             'message' => 'Please Enter a Status'
                )
        ),*/
);
<h4 class="widgettitle">Add New User</h4>
<div class="widgetcontent">
    <?php echo $this->Form->create('User',array('url'=>'addUser/', "enctype" => "multipart/form-data",'class'=>'stdform','id'=>'form1')); ?>
        <div class="par control-group">
            <label class="control-label" for="firstname">First Name*</label>
            <div class="controls">
                <?php echo $this->Form->input('First_Name',array('label'=>false, 'id'=>'firstname','class'=>'input-large')); ?>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label" for="lastname">Last Name*</label>
            <div class="controls">
                <?php echo $this->Form->input('Last_Name',array('label'=>false,'id'=>'lastname','class'=>'input-large')); ?>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label" for="username">Username*</label>
            <div class="controls">
                <?php echo $this->Form->input('Username',array('label'=>false,'id'=>'username','class'=>'input-large')); ?>
            </div>
        </div>


        <div class="par control-group">
            <label class="control-label" for="email">Email*</label>
            <div class="controls">
                <?php echo $this->Form->input('Email_Id',array('label'=>false,'id'=>'email','class'=>'input-xlarge')); ?>
            </div>
        </div>

        <div class="par control-group">
            <label class="control-label" for="location">Office Phone*</label>
            <div class="controls">
                <?php echo $this->Form->input('User_Phone1',array('label'=>false, 'maxlength'=>false,'id'=>'phone','class'=>'input-large')); ?>
            </div> 
        </div>
            <div class="par control-group">
            <label class="control-label" for="location">Cell Phone*</label>
            <div class="controls">
                <?php echo $this->Form->input('User_Phone2',array('label'=>false, 'maxlength'=>false,'id'=>'phone','class'=>'input-large')); ?>
            </div> 
        </div>
        <div class="par control-group">
            <label class="control-label" for="status">Status</label>
            <div class="controls">
                <?php echo $this->Form->input('Is_Active',array('type'=>'select', 'width' => 100, 'options'=>$statusArray, 'label' => false, 'class'=>'input-large')); ?>
            </div> 
        </div>

        <p class="stdformbutton">
            <button class="btn btn-primary">Save</button>
        </p>
    <?php echo $this->Form->end(); ?>
</div><!--widgetcontent-->
$this->User->bindModel(array('belongsTo'=>array('UserDetail'=>array('className' => 'UserDetail', 'foreignKey' => 'Role_ID', ))));
SQL Query: SELECT `User`.`User_ID`, `User`.`First_Name`, `User`.`Last_Name`, `User`.`Email_Id`, `User`.`Username`, `User`.`User_Password`, `User`.`User_Phone1`, `User`.`User_Phone2`, `User`.`Created_By`, `User`.`Created_Date`, `User`.`Is_Active`, `User`.`Is_Deleted`, `UserDetail`.`User_Detail_ID`, `UserDetail`.`User_ID`, `UserDetail`.`Role_ID`, `UserDetail`.`Unit_ID`, `UserDetail`.`Rank_ID`, `UserDetail`.`City_ID`, `UserDetail`.`State_ID`, `UserDetail`.`RSID`, `UserDetail`.`User_Address1`, `UserDetail`.`User_Address2`, `UserDetail`.`Zip_Code`, `UserDetail`.`User_Url`, `UserDetail`.`Created_By`, `UserDetail`.`Created_Date`, `UserDetail`.`Is_Active`, `UserDetail`.`Is_Deleted` FROM `national`.`users` AS `User` LEFT JOIN `national`.`user_details` AS `UserDetail` ON (`User`.`Role_ID` = `UserDetail`.`id`) WHERE 1 = 1 ORDER BY `User`.`User_ID` DESC 
  <?php
    class User extends AppModel{
         var $name = "User";

         /**
         * Model associations: hasOne
         *
         * @var array
         * @access public
         */
        public $hasOne = array(
            'UserDetail'
        );

         var $validate = array(
            'Username' => array(
                'notempty' => array(
                    'rule' => array('notempty'),
                    'required' => false,
                    'message' => 'Username can not be empty!',
                ), 
                 'maxLength'=> array(
                    'rule' => array('maxLength', 20),
                    'message' => 'Username can not be longer that 20 characters.'
                 )
            ),
            'First_Name' => array(
                'notempty' => array(
                    'rule' => array('notempty'),
                    'message' => 'First name can not be empty!',
                    )
            ),
            /*'phone' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'message' => 'Numbers only'
            ),
                'rule' => array('minLength', 10),
                'message' => 'Phone number should be of 10 digits'
            ),*/
            'Last_Name' => array(
                'notempty' => array(
                    'rule' => array('notempty'),
                    'message' => 'Last name can not be empty!',
                    )
            ),

            'Email_Id' => array(
                 'notempty' => array(
                 'rule' => array('email'),
                 'allowEmpty' => false,
                 'message' => 'Please Enter a valid Email Address'
                    )
            )
            /*'status'=> array(
                 'notempty' => array(
                 'rule' => array('notEmpty'),
                 'allowEmpty' => false,
                 'message' => 'Please Enter a Status'
                    )
            ),*/
            );
    }
function addUser(){

        $this->set('setTab','addUser');
        $this->set('parent_tab','Manage Users');
        $this->set('tab','Add User');


        // Super admin doesn't have an access of Manage user section
        if($this->Session->read('User.access_rights') == 4){
            $this->redirect('dashboard');
        }

        $this->layout = 'user';
        $this->set('setTab','manageUsers');
        $this->set('statusArray', $this->statusArray);

        /*CREATE USER DROP DOWN ARRAY START*/
        /*$userDrop['0']='Select Parent';
        $getUsers = $this->User->generatetreelist('User.access_rights <> 4',null,'{n}.User.username','-');
        if($getUsers) {
            foreach ($getUsers as $key=>$value){
                $userDrop[$key] = $value;
            }
            $this->set(compact('userDrop'));
        }*/
        /*CREATE USER DROP DOWN ARRAY END*/

        if(!$this->Session->check('User')){
            $this->redirect('login');
        }

        if($this->data){
                    $data = $this->data;
                    $this->User->set($data);

                    if ($this->User) {
                        // pr($this->data); die;

                                //  because we like to send pretty mail
                                    //Set view variables as normal

                                     $this->set('userDetails', $data['User']);

                                    //$this->User->recursive = 1;
                                    $data['UserDetail']['Role_ID'] = 2;
                                    if($this->User->saveAll($data)){

                                        $this->Session->setFlash('User added successfully', 'default', array('class' => 'errMsgLogin'));
                                        $this->redirect('manageUsers');
                                    }
                                    else {
                                        echo "User not added";
                                    }
                    }

                    else {

                        // do nothing
                    }
        }
    }//Ends here