$this->save()和$this->commit()在循环中使用时只保存最后一条记录


Yii $this->save() and $this->commit() saves only the last record when used in a loop

我试图通过Yii中的模型插入多个记录到我的数据库中。我正在使用foreach从控制器传递数据到模型。首先,它检查字段是否存在于数据库中,如果结果为false,则插入到数据库中。然而,当插入时,它不插入第一个记录,而是插入最后从控制器传递的记录。但是在第一个数据传递到控制器时生成一个Id。

我的控制器:

foreach($user_likes['data'] as $ul){
    $category_id = $categoriesModel->ifCategoryExists($ul['category']);
    if(!$category_id){
      $category_id = $categoriesModel->add($ul['category']);                        
      $categories[$category_id] = $ul['category'];                          
    }
}

我的模型:

public function add($params = '') {
    $transaction = $this->dbConnection->beginTransaction();
    try {
        $this->Category = $params;
        $this->save();
        $transaction->commit();
        echo "{ inserted_id ", $this->Id," },{ inserted_category ",$params," }<br/>";
        return $this->Id;
    } catch (Exception $e) {
        $transaction->rollback();
        throw $e;
    }
}

当我看到echo

的输出
{ inserted_id 17 },{ inserted_category Community }
{ inserted_id 17 },{ inserted_category Local business }
{ inserted_id 17 },{ inserted_category Actor/director }
{ inserted_id 17 },{ inserted_category Event planning/event services }
{ inserted_id 17 },{ inserted_category Album }
{ inserted_id 17 },{ inserted_category Magazine }
{ inserted_id 17 },{ inserted_category Internet/software }
{ inserted_id 17 },{ inserted_category Consulting/business services }
{ inserted_id 17 },{ inserted_category Education website }
{ inserted_id 17 },{ inserted_category Author }
{ inserted_id 17 },{ inserted_category Public figure }
{ inserted_id 17 },{ inserted_category Business person }
{ inserted_id 17 },{ inserted_category Public figure }
{ inserted_id 17 },{ inserted_category Community }
{ inserted_id 17 },{ inserted_category Comedian }
{ inserted_id 17 },{ inserted_category Business person }
{ inserted_id 17 },{ inserted_category Public figure }
{ inserted_id 17 },{ inserted_category News personality }
{ inserted_id 17 },{ inserted_category Political organization }
{ inserted_id 17 },{ inserted_category Computers/technology }
{ inserted_id 17 },{ inserted_category Travel/leisure }
{ inserted_id 17 },{ inserted_category Community }

很奇怪,它插入了

Community

CActiveRecord有一个状态,它开始为"new"(并使用"insert"场景)。只要对它调用save(),它就不再是新的,并进入"更新"场景。CActiveRecord不是用来插入不同的记录的,1 instance = 1 row。

但是,如果你真的想使用单个实例,只需重置的东西:

$this->Id = NULL;
$this->isNewRecord = TRUE; 

如果你在你的$this->save()之前添加这个,它应该工作。我强烈建议使用框架,因为它意味着是和创建新的实例。

您正在保存相同的模型,一遍又一遍地使用不同的属性(参见CActiveRecord::save()获取更多信息)。我建议你每次保存时使用一个新的CategoriesModel实例,或者在保存前将isNewRecord设置为false并取消id的设置。

public function add($params = '') {
    $transaction = $this->dbConnection->beginTransaction();
    try {
        $this->isNewRecord = false;
        unset($this->Id);
        $this->Category = $params;
        $this->save();
        $transaction->commit();
        echo "{ inserted_id ", $this->Id," },{ inserted_category ",$params," }<br/>";
        return $this->Id;
    } catch (Exception $e) {
        $transaction->rollback();
        throw $e;
    }
}