我在控制器中有以下功能
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调试技巧。我相信他们都是失败的&#39;打印出于同样的原因,这与实际打印数据的位置有关。
有没有办法在$ role-&gt; save()和查看$ role-&gt; 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 :(得分:0)
对于authmanager不能保存使用,但是您需要创建角色并添加角色
我使用此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']);
}
}