2012-05-08 4 views
-1

で作成&更新MVCの作成:あなたは役割テーブルはuserinrolesテーブルを経由してユーザーに関連して見ることができるようにのYiiは、だから私はにschemca非常に似てい無関係のテーブル

users 
-------------- 
userid, name, password, email 

userinroles 
-------------- 
pk, userid, roleid 

roles 
----------- 
roleid, level, description 

、これがそうユーザーです様々なグループ内で編集権を持ち、異なるものに対して異なるアクセスレベルを持つことができます。たとえば、モジュールに対してスーパー管理者権限を持っている間は、ページエディタである必要があります。

問題は、私がレコードを更新または作成しているときに、ロールをリストする方法がわからないために、どのロールにボックスをチェックして、それをuserinrolesテーブルに挿入できるかということです。

これを行う方法に関するアイデアはありますか?

モデル:

Yii::import('application.models._base.BaseUser'); 
class User extends BaseUser 
{ 
    public static function model($className=__CLASS__) { 
     return parent::model($className); 
    } 
     public function rules() { 
     return array(
       array('username, password, email', 'required'), 
       array('isActive, isDeleted, isLocked', 'numerical', 'integerOnly'=>true), 
       array('username', 'length', 'max'=>50), 
       // Throws error if user name is not unique 
       array('username', 'unique', 'attributeName'=> 'username', 'caseSensitive' => 'false'), 
       array('password', 'length', 'max'=>255), 
       array('email, organization, position', 'length', 'max'=>100), 
       array('salt', 'length', 'max'=>32), 
       array('organization, position, salt, isActive, isDeleted, isLocked', 'default', 'setOnEmpty' => true, 'value' => null), 
       array('userid, username, password, email, organization, position, salt, isActive, isDeleted, isLocked', 'safe', 'on'=>'search'), 
     ); 
    } 
    public function relations() { 
     return array(
      'toolaccesses' => array(self::HAS_MANY, 'Toolaccess', 'userID'), 
      'usergalleries' => array(self::HAS_MANY, 'Usergallery', 'userid'), 
      'userinroles' => array(self::HAS_MANY, 'Userinroles', 'userid'), 
         'tools' =>array(self::MANY_MANY, 'Tool', 'toolid'), 
     ); 
    }   
} 

コントローラー:

class UserController extends GxController { 


    public function actionView($id) { 
     $this->render('view', array(
      'model' => $this->loadModel($id, 'User'), 
     )); 
    } 

    public function actionCreate() { 
     $model = new User; 


     if (isset($_POST['User'])) { 
      $model->setAttributes($_POST['User']); 

         // salting the user's password before we insert 
         $model->password = md5(Yii::app()->params["salt"] . $model->password); 

      if ($model->save()) { 
       if (Yii::app()->getRequest()->getIsAjaxRequest()) 
        Yii::app()->end(); 
       else 
        $this->redirect(array('view', 'id' => $model->userid)); 
      } 
     } 

     $this->render('create', array('model' => $model)); 
    } 

    public function actionUpdate($id) { 
     $model = $this->loadModel($id, 'User'); 

     if (isset($_POST['User'])) { 
        // testing if we need to salt the password. 
        if(strcmp($_POST['User']['password'], $model->password)!=0) 
        { // passwords passed in are not the same. We need to now modify the post password 
         $_POST['User']['password'] = md5(Yii::app()->params["salt"] . $_POST['User']['password']); 
        } 
        $model->setAttributes($_POST['User']); 
        if ($model->save()) { 
          $this->redirect(array('view', 'id' => $model->userid)); 
        } 
     } 

     $this->render('update', array(
       'model' => $model, 
       )); 
    } 

    public function actionDelete($id) { 
      // prevent the deletion of the super user, who has the ID 1. 
      // This is sort of like a Unix "root" user or a Window's Administrator 
      if($id == 1) 
      { 
       throw new CHttpException(400, Yii::t('app', 'You cannot delete the super admin.')); 
      } 
      else 
      { 
     if (Yii::app()->getRequest()->getIsPostRequest()) { 
      $this->loadModel($id, 'User')->delete(); 

      if (!Yii::app()->getRequest()->getIsAjaxRequest()) 
       $this->redirect(array('admin')); 
     } else 
      throw new CHttpException(400, Yii::t('app', 'Your request is invalid.')); 
      } 
    } 

    public function actionIndex() { 
     $dataProvider = new CActiveDataProvider('User'); 
     $this->render('index', array(
      'dataProvider' => $dataProvider, 
     )); 
    } 

    public function actionAdmin() { 
     $model = new User('search'); 
     $model->unsetAttributes(); 

     if (isset($_GET['User'])) 
      $model->setAttributes($_GET['User']); 

     $this->render('admin', array(
      'model' => $model, 
     )); 
    } 

} 

答えて

1

まず、私はあなたがユーザとロールテーブル間の多対多数の関係使うべきだと思う - その後

public function relations() { 
    return array(
     'toolaccesses' => array(self::HAS_MANY, 'Toolaccess', 'userID'), 
     'usergalleries' => array(self::HAS_MANY, 'Usergallery', 'userid'), 
     'roles' => array(self::MANY_MANY, 'Roles', 'userinroles(userid, roleid)'), 
     'tools' => array(self::MANY_MANY, 'Tool', 'toolid'), 
    ); 

を、あなたは$user->rolesを持つユーザーの役割を取得できます。具体的なユーザーに関連する役割を持ついくつかのアクションについて:私はthis extensionを使用して、多対多の関係を保存します。私はあなたを間違って理解して申し訳ありません。

関連する問題