Sammaye/MongoYii

How to do transaction?

Closed this issue · 1 comments

Kuzdo commented

Hello again!

I have one problem. I want to create a command (yiic) which should retrive documents from mongo with status 0, and then update that document with status 1. Then command will be doing something with this document and after that will be go to next document.

But when I ran two instances of that command, all documents was processed by both instances. I was trying do some transactions, but MongoYii has not appropriate method.

What I should do?
This is my code:

    public function actionRun()
    {
        $count = CronTasks::model()->count();
        for ($i = 0; $i < $count; $i++) {
            $task        = CronTasks::single();
//            $transaction = $task->getDbConnection();
            if ($task instanceof CronTasks && $task->status == 0) {
                $task->status = 1;
                $task->save();
//                $transaction->commit();
                echo 'ID: ' . $task->id. '; Method: ' . $task->method . '; Detail: ' . $task->detail . PHP_EOL;
//              There will be more operations
            } else {
                break;
            }
        }
    }
Kuzdo commented

Nevermind, findAndModify solves the problem.