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

时间:2015-09-09 06:04:47

标签: php mysql cakephp

我正在尝试构建一个部分,我们可以使用Role_ID=2

添加用户

Role_ID位于名为User_Details的其他表中,并与当前userid链接为外键。

以下是我的UserController.php的代码

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

以下是型号 user.php

的代码
<?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'
                )
        ),*/
);

以下是观看代码 add_user.ctp

<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 

有关如何构建此内容的任何建议?

1 个答案:

答案 0 :(得分:0)

您可以将bindModel功能用于关联或链接两个表,如此

$ this-&gt; User-&gt; bindModel(array('belongsTo'=&gt; array('UserDetail'=&gt; array('className'=&gt;'UserDetail',             'foreignKey'=&gt; 'ROLE_ID',             )))); 现在,当您发现User表的数据时,它也会显示userdetail数据。 如果需要特定的userdetail模型字段

,也可以使用可包含的行为

首先检查userdetail的表名,如果你的表名如userdetails,那么你可以在bindModel中使用类名这样的Userdetails 如果你使用像这个user_details这样的表名而不是像这个UserDetails那样放类名。你可以给一个函数找到用户数据并在特定函数中使用绑定模型函数。应该加载模型

请检查表名