2015-10-15 56 views
2

插入沒有數據I具有在控制器的以下函數Yii2 ActiveRecord->保存()返回真,在數據庫

public function actionSave_role() { 

     $role = new Auth_Item; 

     if ($role->load(Yii::$app->request->post()) && $role->save()) { 
      //echo '<pre>'; print_r($role->getErrors()); 
      //yii\helpers\VarDumper::dump($role->getErrors(), 10, true); 
      //pring_r($role->getErrors()); 
      //die(); 
      return $this->actionGet_role($role->id); 
     } 
} 

的POST返回true,但沒有數據被輸入到數據庫中。我試圖查看錯誤。數據庫不能滿足某種驗證,但我一直無法弄清楚。所以我試圖查看實際的錯誤,這已被證明是困難的,我的嘗試都被註釋掉了。仍在研究我的PHP調試技巧。我相信它們都是「失敗」的,出於同樣的原因,這與數據實際打印出來的地方有關。

在$ role-> save()和view $ role-> getErrors()之後有沒有什麼辦法殺死應用程序?任何其他意見調試這個問題也將不勝感激!

EDIT1:

試圖

$role->save(false); //No Luck 

這裏是我的記錄模式

`auth_item`(`id`, `name`, `owner_user_id`, `type`, `description`, `rule_name`, `data`, `created_at`, `updated_at`, `admin`, `create_package`, `read_package`, `update_package`, `delete_package`) 

這是我saveRole JavaScript函數來踢東西了:

function saveRole(role){ 
    var newRole = { 
     _csrf: csrf, 
     'Role[id]': role.id, 
     'Role[name]': role.name, 
     'Role[description]': role.description, 
     'Role[admin]': role.admin ? role.admin = 1 : role.admin = 0, 
     'Role[create_package]': role.create_package ? role.create_package = 1 : role.create_package = 0, 
     'Role[read_package]': role.read_package ? role.read_package = 1 : role.read_package = 0, 
     'Role[update_package]': role.update_package ? role.update_package = 1 : role.update_package = 0, 
     'Role[delete_package]': role.delete_package ? role.delete_package = 1 : role.delete_package = 0 
    }; 

    var request = $http({ 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
     url: 'roles/save_role', 
     method: "POST", 
     data: $.param(newRole) 
    }); 

    return request.then(handleSuccess, handleError); 
}; 

這是我Auth_Item AcitveRecord模型

class Auth_Item extends \yii\db\ActiveRecord 
{ 
public static function tableName() 
{ 
    return 'auth_item'; 
} 

public function rules() 
{ 
    return [ 
     [['name'], 'required'], 
     [['created_at', 'updated_at'], 'safe'], 
     [['name'], 'string', 'max' => 255] 
    ]; 
} 

public function beforeSave($insert) { 
    if ($insert) { 
     $this->owner_user_id = Yii::$app->user->identity->id; 
     $this->rule_name = null; 
     $this->type = 1; 
     $this->data = null; 
     $this->created_at = new \yii\db\Expression('NOW()'); 

    } 
    $this->updated_at = new \yii\db\Expression('NOW()'); 
    return true; 
} 
} 

這裏是我的表結構:

CREATE TABLE IF NOT EXISTS `auth_item` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `name` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
    `owner_user_id` int(11) DEFAULT NULL, 
    `type` int(11) NOT NULL, 
    `description` text COLLATE utf8_unicode_ci, 
    `rule_name` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `data` text COLLATE utf8_unicode_ci, 
    `created_at` timestamp NULL DEFAULT NULL, 
    `updated_at` timestamp NULL DEFAULT NULL, 
    `admin` tinyint(1) DEFAULT NULL, 
    `create_package` tinyint(1) DEFAULT NULL, 
    `read_package` tinyint(1) DEFAULT NULL, 
    `update_package` tinyint(1) DEFAULT NULL, 
    `delete_package` tinyint(1) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `name` (`name`), 
    KEY `rule_name` (`rule_name`), 
    KEY `idx-auth_item-type` (`type`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ; 

ALTER TABLE `auth_item` 
ADD CONSTRAINT `auth_item_ibfk_1` FOREIGN KEY (`rule_name`) REFERENCES `auth_rule` (`name`) ON DELETE SET NULL ON UPDATE CASCADE; 

EDIT2:

這是我在哪裏。我認爲它越來越近了。 Post仍然返回200 OK,數據仍然沒有真正進入auth_item表。

public function actionSave_role() { 
    $auth = Yii::$app->authManager; 
    $id = $_POST["Role"]["id"]; 
     $roleModel = null; 
     if ($id != null) { // contact exists 
      $roleModel = Auth_Item::findOne($id); 
     } else { 
      $roleModel = new Auth_Item(); 
     } 

     if ($roleModel->load(Yii::$app->request->post())) { 
      $role = $auth->createRole($roleModel->name); 
      $role->data = $roleModel->data; 
      $role->description = $roleModel->description; 
      $role->admin = $roleModel->admin; 
      $role->create_package = $roleModel->create_package; 
      $role->read_package = $roleModel->read_package; 
      $role->update_package = $roleModel->update_package; 
      $role->delete_package = $roleModel->delete_package; 
      //End Posted data 
      $role->rule_name = null; 
      $role->type = 1; 
      $role->created_at = new \yii\db\Expression('NOW()'); 
      //$role->updated_at = new \yii\db\Expression('NOW()'); 
      $role->owner_user_id = Yii::$app->user->identity->id; 

      $auth->add($role); 

      return $this->actionGet_role($role->id); 
     } 

} 

回答

0

對於authmanager不保存使用,但您shuold創建角色並添加角色

我用這個actionCreate

/** 
* Creates a new AuthItem model. 
* If creation is successful, the browser will be redirected to the 'view' page. 
* @return mixed 
*/ 
public function actionCreate() 
{ 
    $model = new AuthItem(); 
    $auth = Yii::$app->authManager; 


    if ($model->load(Yii::$app->request->post())) { 
     switch ($model->type) { 
      case AuthItem::TYPE_ROLE : // 1 = TYPE_ROLE 
       $role = $auth->createRole($model->name); 
       $role->data   = $model->data; 
       //$role->ruleName  = $model->rule_name; 
       $role->description = $model->description; 
       //$role->type   = $model->type; 
       $auth->add($role); 
       break;      
      case AuthItem::TYPE_PERMISSION : // 2 = TYPE_PERMISSION 
       $permission = $auth->createPermission($model->name); 
       $permission->data   = $model->data; 
       //$permission->ruleName  = $model->rule_name; 
       $permission->description = $model->description; 
       //$permission->type   = $model->type; 
       $auth->add($permission); 
       break;    
      default: 
       break; 
     } 
     return $this->redirect(['view', 'id' => $model->name]); 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 

這個模型

<?php 

    namespace vendor\dfenx\auth\models; 

    use Yii; 

    /** 
    * This is the model class for table "auth_item". 
    * 
    * @property string $name 
    * @property integer $type 
    * @property string $description 
    * @property string $rule_name 
    * @property string $data 
    * @property integer $created_at 
    * @property integer $updated_at 
    * 
    * @property AuthAssignment[] $authAssignments 
    * @property AuthRule $ruleName 
    * @property AuthItemChild[] $authItemChildren 
    */ 
    class AuthItem extends \yii\db\ActiveRecord 
    { 

     const TYPE_ROLE  = '1'; 
     const TYPE_PERMISSION = '2'; 

     const TEXT_TYPE_ROLE  = 'TYPE_ROLE'; 
     const TEXT_TYPE_PERMISSION = 'TYPE_PERMISSION'; 
     const TEXT_ERROR_TYPE_NOT_ASSIGNED = 'ERROR_TYPE_NOT_ASSIGNED'; 

     /** 
     * @inheritdoc 
     */ 
     public static function tableName() 
     { 
      return 'dfenx_auth_item'; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function rules() 
     { 
      return [ 
       [['name', 'type'], 'required'], 
       [['type', 'created_at', 'updated_at'], 'integer'], 
       [['description', 'data'], 'string'], 
       [['name', 'rule_name'], 'string', 'max' => 64] 
      ]; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function attributeLabels() 
     { 
      return [ 
       'name' => Yii::t('app', 'Name'), 
       'type' => Yii::t('app', 'Type'), 
       'description' => Yii::t('app', 'Description'), 
       'rule_name' => Yii::t('app', 'Rule Name'), 
       'data' => Yii::t('app', 'Data'), 
       'created_at' => Yii::t('app', 'Created At'), 
       'updated_at' => Yii::t('app', 'Updated At'), 
      ]; 
     } 

     /** 
     * @return \yii\db\ActiveQuery 
     */ 
     public function getAuthAssignments() 
     { 
      return $this->hasMany(AuthAssignment::className(), ['item_name' => 'name']); 
     } 

     /** 
     * @return \yii\db\ActiveQuery 
     */ 
     public function getRuleName() 
     { 
      return $this->hasOne(AuthRule::className(), ['name' => 'rule_name']); 
     } 

     /** 
     * @return \yii\db\ActiveQuery 
     */ 
     public function getAuthItemChildren() 
     { 
      return $this->hasMany(AuthItemChild::className(), ['child' => 'name']); 
     } 
    } 
+0

我其實已經試過那個伯爵呃,現在又來了。數據庫中仍然沒有數據。 – Goat

+0

您正在使用哪種類型的授權管理員? – scaisEdge

+0

'authManager'=> [ 'class'=>'yii \ rbac \ DbManager', 'defaultRoles'=> ['guest'] ], – Goat