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);
}
}
我其實已經試過那個伯爵呃,現在又來了。數據庫中仍然沒有數據。 – Goat
您正在使用哪種類型的授權管理員? – scaisEdge
'authManager'=> [ 'class'=>'yii \ rbac \ DbManager', 'defaultRoles'=> ['guest'] ], – Goat