We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Transaction Manager getting 500 server error when rollback is triggered

Phalcom Transaction Manager getting 500 server error when rollback is triggered

https://drive.google.com/file/d/0B7AtJVATjxPFTDktdFFuRXlFM3c/view?usp=sharing

    $request = new Request();
    $guid = new Guid();
    $transactionManager = new TransactionManager();
    $transaction = $transactionManager->get();

    if($request->isPost()){

        $directoryid    = $guid->GUID();
        $memberid       = $request->getPost('memberid');
        $directorytitle = $request->getPost('directorytitle');
        $path           = $request->getPost('path');

        $findfolder = Membersdirectory::findFirst("directorytitle = '".$directorytitle."' AND path = '". $path ."'");
        if($findfolder){
            $data = array('error' => 'Folder is already exist!', 'statuscode' => 400 );
        }
        else{
            try {
                    $savefolder = new Membersdirectory();
                    $savefolder->setTransaction($transaction);

                    $savefolder->directoryid    = $directoryid;
                    $savefolder->memberid       = $memberid;
                    // $savefolder->directorytitle = $directorytitle;
                    $savefolder->path           = $path;
                    $savefolder->datecreated    = date('Y-m-d H:i:s');

                    if($savefolder->save()){
                        $data = array('success' => 'New folder successfully created!', 'statuscode' => 200 );
                        // $transaction->rollback();
                    }
                    else{
                        $data = array('error' => 'Something went wrong!', 'statuscode' => 400 );
                        $transaction->rollback();
                    }

                     $transaction->commit();

                } 

                catch (Phalcon\Mvc\Model\Transaction\Failed $e) {
                    echo "Failed, reason: ", $e->getMessage();
                }

        }

        echo json_encode($data);

    }


13.4k
Accepted
answer

Put a \ in front of the FQN so your catch block is:

catch (\Phalcon\Mvc\Model\Transaction\Failed $e) {...}

Also, your catch block should come directly after the closing bracket from the try block.

try {

} catch ($e) {

}
edited Mar '16

Put a \ in front of the FQN so your catch block is:

catch (\Phalcon\Mvc\Model\Transaction\Failed $e) {...}

Also, your catch block should come directly after the closing bracket from the try block.

try {

} catch ($e) {

}

thank you so much i fixed the problem thank you sir :) and one more question not related but how did you make your code dark background my code in description section is white but your code is dark how is that?

You can use code blocks, read about it in the markdown help page