首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在YII1.x中运行事务查询

在YII1.x中运行事务查询
EN

Stack Overflow用户
提问于 2015-02-25 13:58:10
回答 1查看 289关注 0票数 0

我正在尝试使用YII1.x运行一个事务性查询--如果有问题,这基本上应该回滚所有的查询,人们能确认这是使用Yii 1运行事务的正确方式吗?

代码语言:javascript
复制
// data comes from a csv
 $transaction = Yii::app()->db->beginTransaction();
    try
    {

        if (($handle = fopen($path, "r")) !== false) {
            while (($data = fgetcsv($handle)) !== FALSE) {
                if ($currentRow == 1) {
                    $header = $this->import_fields(array_map('strtolower', $data));
                    $currentRow++;
                    continue;
                } else {
                    $data = array_combine($header, $data);
                    $csv_import_model = null;

                    if (!empty($data['username'])) {
                        $csv_import_model = StudentImportForm::model()->findByAttributes(array(
                            'username' => $data['username'],
                            'organisation_id' => user()->data->organisation->getViewOrgId()
                        ));
                    }

                    if (is_null($csv_import_model)) {
                        $csv_import_model = new StudentImportForm();
                        $isNew = true;
                    } else {
                        $isNew = false;
                    }

                    $csv_import_model->scenario = 'import';
                    $csv_import_model->setAttributes($data);
                    $csv_import_model->unsetAttributes(array('password'));

                    if ($csv_import_model->validate()) {

                        if (in_array($csv_import_model->username, $processedUsername)) {
                            $csv_import_model->addError('username', sprintf('Duplicate username (%1$s) found in csv file. which may already exists on row number %2$s.', $csv_import_model->username, (array_search($csv_import_model->username, $processedUsername) + 1)));
                        } else {

                            if ($csv_import_model->save()) {

                                if ($isNew) {
                                    $this->csv_results['inserted'] = $this->csv_results['inserted']+1;
                                } else {
                                    $this->csv_results['updated'] = $this->csv_results['updated']+1;
                                }

                            } else {
                                $this->csv_results['error'][$currentRow] = $csv_import_model->getErrors();
                            }
                        }
                    } else {
                        $csv_import_model->csv_index = $currentRow;
                        $this->csv_results['error'][$currentRow] = $csv_import_model->getErrors();
                    }

                    $processedUsername[] = $csv_import_model->username;

                    $currentRow++;
                    Yii::getLogger()->flush(false);
                }
            }
            fclose($handle);
        }

        $transaction->commit();
    }
    catch(Exception $e)
    {
        $transaction->rollback();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-25 14:09:47

$model->save()不会在异常失败时抛出异常。它返回真假。为了回滚整个块,如果save()返回false,则必须手动抛出异常。试着做这样的事情:

代码语言:javascript
复制
$errors = null;
try {
    if ($csv_import_model->save()) {
        // continue with whatever logic you have
        $transaction->commit();
    }else{
        $errors = 'Error when saving';
        throw new Exception('Could not save model');
    }
}catch(Exception $e){
   //Do some logging here
   $transaction->rollback();
   if($errors != null){
       Yii::app()->user->setFlash('error', $errors);
   }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28720883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档