Update Yii Framework上的唯一键的重复输入错误

J.K*_*.A. 2 php mysql yii

我在尝试更新表中的记录时遇到以下错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'ABCD1111P' for key 'pan_UNIQUE'
Run Code Online (Sandbox Code Playgroud)

表具有列,即"pan",其具有分配的唯一键.

以下是我在控制器中的代码:

$model->pan = $_POST['CustomerBasics']['pan'];
$model->cell = $_POST['CustomerBasics']['cell'];
$model->gender = $_POST['PersonalDetails']['gender'];

$valid = $model->validate();
            print_r($model->getErrors());
            if ($valid) {
                $model->update();
                $personal_details_id = $model->personal_details_id;
            }
Run Code Online (Sandbox Code Playgroud)

在模型中:

array('pan', 'unique', 'on' => 'insert', 'message' => '{attribute}:{value} already exists!'),
Run Code Online (Sandbox Code Playgroud)

有什么问题?我哪里错了?

谢谢.

sak*_*zai 8

既然你已经说@Daneil Vaquero的解决方案不起作用了.但是它的工作如下.

UserController更新操作

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

        if(isset($_POST['User']))
        {
            $model->attributes=$_POST['User'];
            $model->username='admin';

            if( $model->validate() && $model->save())
                $this->redirect(array('view','id'=>$model->id));
            else
                print_r($model->getErrors());
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }
Run Code Online (Sandbox Code Playgroud)

用户模型rules()方法

    public function rules()
        {
         array('username', 'unique', 'on' => 'insert,update', 'message' => '{attribute}:{value} already exists!'),
            );
        }
Run Code Online (Sandbox Code Playgroud)

请参阅我手动将用户名设置为admin,除admin用户外,验证将失败,如其他人所预期和指出的那样.

您的应用程序应该可以正常工作,除非您必须在模型规则设置中更改'on' => 'insert''on' => 'insert,update'