yiisoft / yiisoft/active-record

Db trans rollback but leave model primary key exists

Open
#56 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
119
Forks
38
Avg merge
1h 11m
Merged PRs (30d)
1

Description

What steps will reproduce the problem?
namespace app\commands;

use app\models\Model1;
use Yii;
use yii\base\Exception;
use yii\console\Controller;

class TestController extends Controller
{
    public function actionIndex()
    {
        $model = new Model1();
        $model->... = '...'; // fill with correct values
        try {
            Yii::$app->db->transaction(function () use (&$model) {
                $model->save();
                throw new Exception('Another error occurred.');
            });
        } catch (\Throwable $e) {
            $this->stdout($e->getMessage() . PHP_EOL);
            $this->stdout($model->id . PHP_EOL);
        }
    }
}
What is the expected result?

Empty $model->id

What do you get instead?

There is a value in $model->id

Additional info
Q A
Yii version 2.0.14
PHP version PHP 7.0.27-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan 5 2018 14:12:46) ( NTS )
Operating system Ubuntu 16.04.3 LTS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided Yii transaction example with the stated PHP and Yii versions, then trace the transaction and model save behavior involved in assigning the primary key. Done means the rolled-back model has an empty id, with the original reproduction still behaving correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.