【问题标题】:Replace data from same id user in Yii framework在 Yii 框架中替换来自同一 id 用户的数据
【发布时间】:2014-08-11 22:06:32
【问题描述】:

我该如何解决这个错误?我有问题替换来自相同 ID 的数据,这是我的 _from:

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Update'); ?>
</div>

这是我的控制器操作创建:

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

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['TblUasUts']))
    {
        $model->attributes=$_POST['TblUasUts'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->nim_mhs));
    }

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

这是我得到的错误:

CDbCommand gagal menjalankan 语句SQL:SQLSTATE[23000]:完整性约束违规:1062 键“PRIMARY”的重复条目“2010140360”

【问题讨论】:

  • 请澄清您的问题。目前完全无法理解您遇到了什么问题。
  • 我正在解决我的问题
  • 您好,再次学习,请您显示您的数据库架构,以便我们可以帮助达到极限。
  • 你为什么要发布同一个问题的重复 -> stackoverflow.com/questions/24276763/…

标签: php button yii submit


【解决方案1】:

这是因为您尝试使用现有主键 ID 保存元素。如果你想更新 DB 中的现有条目,你应该从 DB(TblUasUts::model()->findByPk) 加载 ActiveRecord。您还可以将字段 id 从 $_POST 数组中取消设置,并每次在 DB 中创建新行。

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

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['TblUasUts']))
{
    if (isset($_POST['TblUasUts']['id']) && $_POST['TblUasUts']['id']) {
        $model = TblUasUts::model()->findByPk((int)$_POST['TblUasUts']['id']);
    }

    $model->attributes=$_POST['TblUasUts'];
    if($model->update())
        $this->redirect(array('view','id'=>$model->nim_mhs));
}

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

}

【讨论】:

  • 因为是新记录所以可以更新,请问如何修复?
  • 插入未设置($_POST['TblUasUts']['id']); $model->attributes=$_POST['TblUasUts']; 之前
  • if(isset($_POST['TblUasUts'])) { if (isset($_POST['TblUasUts']['id']) && $_POST['TblUasUts']['id ']) { $model = TblUasUts::model()->findByPk((int)$_POST['TblUasUts']['id']) ?: new TblUasUts;未设置($_POST['TblUasUts']['id'])} $model->attributes=$_POST['TblUasUts']; if($model->update()) $this->redirect(array('view','id'=>$model->nim_mhs)); }
【解决方案2】:

删除保存更新:

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Update'); ?>
</div>

and this is my Controller action Create :

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

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['TblUasUts']))
    {
        $model->attributes=$_POST['TblUasUts'];
        if($model->update())
            $this->redirect(array('view','id'=>$model->nim_mhs));
    }

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

还要检查您是否将 ID 字段设置为自动递增和主要字段。

【讨论】:

  • 出现错误的原因如下: 1) PK定义错误或模型无法加载新实例。
  • 我将primary或​​id设为不自动递增,但我将primary设置为用户id,如果没有数据,我不会保存,但如果数据库中有数据,则会更新,但使用一个提交按钮
  • 您需要使 id 递增,否则模型如何在此处创建具有相同 id 的记录。如果需要同名记录,在yii中使用查询方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多