<p>The issue turned out to be the result of a variable name mistake on my part. I had typed in an incorrect variable name in the email template. I actually still don't really understand what that has to do with a database transaction, though.</p>

<p>EDIT:
If anyone else is running into a similar issue, I've found a way to get a more useful error message, as opposed to the "CDbTransaction is inactive" message, which doesn't help much. If you go to craft/app/services/UsersService.php, and comment out line 311 ("$transaction->rollback();"), then Yii won't give you the CDbTransaction error before Craft has a chance to tell you its more helpful error. This should give you a better idea of what you're doing wrong. Obviously, don't forget to un-comment that line after you've fixed your issue.</p>

CC BY-SA 3.0
2016-04-19T23:59:29.397Z
Very helpful EDIT tip. The line is #320 now. (Craft 2.6.2780)
Marion Newlevant
CC BY-SA 3.0
2014-06-24T21:36:43.670Z
It was probably triggering some lazy-loading method for users which was, in-turn, making a call to the database to attempt to retrieve data.
Brad Bell
CC BY-SA 3.0
2014-06-24T21:18:35.427Z
2014-07-16T23:15:57.167Z
2014-07-16T23:15:57.167Z
shanecavaliere
<p>It throws the error, since it fires the event in the transaction try-catch, and Twig was throwing the exception. In the catch block then it tried to execute the rollback.</p>

CC BY-SA 3.0
2015-10-25T22:45:32.367Z
2015-10-25T22:45:32.367Z
balazscsaba2006
<p>I'm writing a simple project-specific plugin that sends a notification to the site admin when a new user is registered. I have an HTML template that I'd like to use for this.</p>

<p>In my plugin's main class, I have the following function:</p>

<pre><code>public function init()
    {
        parent::init();
        // Events
        craft()-&gt;on('users.saveUser', function(Event $event) {
            $user = $event-&gt;params['user'];
            $isNewUser = $event-&gt;params['isNewUser'];

            if ($isNewUser)
            {
                // Send notification email
                craft()-&gt;aeiMember-&gt;registrationNotification($user);
            }
        });
    }
</code></pre>

<p>which calls a one of my plugin's services, 'registrationNotification'. Here's the code I have for that service:</p>

<pre><code>public function registrationNotification(UserModel $savedUser)
    {
        $user = new \stdClass();

        $user-&gt;firstName            = $savedUser-&gt;firstName;
        $user-&gt;lastName             = $savedUser-&gt;lastName;
        $user-&gt;email                = $savedUser-&gt;email;
        $user-&gt;aeiGroups            = $savedUser-&gt;aeiGroups;
        $user-&gt;phone                = $savedUser-&gt;phone;
        $user-&gt;mailingAddress       = $savedUser-&gt;mailingAddress;
        $user-&gt;city                 = $savedUser-&gt;city;
        $user-&gt;state                = $savedUser-&gt;state;
        $user-&gt;zip                  = $savedUser-&gt;zip;
        $user-&gt;country              = $savedUser-&gt;country;
        $user-&gt;preferredDealer      = $savedUser-&gt;preferredDealer;
        $user-&gt;dealerAccountNumber  = $savedUser-&gt;dealerAccountNumber;
        $user-&gt;school               = $savedUser-&gt;school;
        $user-&gt;expectedGraduationDate = $savedUser-&gt;expectedGraduationDate;

        $email = new EmailModel();
        $email-&gt;toEmail = '...myemailaddress...';
        $email-&gt;subject = 'New User Registration: ' . $user-&gt;firstName . ' ' . $user-&gt;lastName;
        $email-&gt;htmlBody = craft()-&gt;templates-&gt;render('aeiMember/registration_notification', array(
            'user' =&gt; $user,
        ));

        // Send
        if (craft()-&gt;email-&gt;sendEmail($email))
        {
            return true;
        }
    }
</code></pre>

<p>When I submit the registration form, I get the following error: "CDbException: CDbTransaction is inactive and cannot perform commit or roll back operations.". I narrowed down the issue, and if I use a non-HTML email body, everything works fine, so apparently there's an issue with rendering a template at this event.</p>

<p>Here's a link to a Google Doc with the stack trace: <a href="https://docs.google.com/document/d/1atFNkktuZQ91hU9QipXYMhOCHNGAavO5qUI_TF5jSeE/edit?usp=sharing" rel="nofollow">https://docs.google.com/document/d/1atFNkktuZQ91hU9QipXYMhOCHNGAavO5qUI_TF5jSeE/edit?usp=sharing</a></p>

<p>Am I doing something wrong, or is there a workaround to accomplish what I'm after? It seems like it should be pretty simple.</p>

CC BY-SA 3.0
2014-06-24T04:05:08.630Z
Is your non-HTML email body still accessing all the values of the stdClass object? CDbTransaction is a database class for DB transactions and I can't even begin to understand why this code here is concerned with hitting the database. Do you have devMode on? If so, could you post the stack trace?
Matt Stauffer
CC BY-SA 3.0
2014-06-24T14:59:49.130Z
Good point - no, the non-HTML email body wasn't accessing any values. I reviewed the template, and the only variables it is accessing are 'user' and the global 'now'.

I do have devMode on, I've edited my question to include a link to the stack trace.
shanecavaliere
CC BY-SA 3.0
2014-06-24T15:09:54.430Z
If you substitute the craft()->templates->render call with some static html, do you get the same error? If you do the template render call and store the contents in a variable (and don't add it as a htmlBody), do you get the same error?
André Elvan
CC BY-SA 3.0
2014-06-24T15:13:42.490Z
What's confusing is that the stack trace shows the error on saving the user, which should happen *before* this is triggered, so I don't know why the email template would affect it. As far as I can tell it hasn't even hit the "'onEndRequest'" yet. If @aelvan's questions don't get you anywhere, you might need to just submit a support request.
Matt Stauffer
CC BY-SA 3.0
2014-06-24T15:37:02.280Z
What does the registration_notification template look like?  Something is triggering a call to UsersController->saveUser when calling render() and I suspect that's something in your template code?
Brad Bell
CC BY-SA 3.0
2014-06-24T21:12:07.000Z
Well, I figured out what the issue was. In my template, I had a '{{ user.accountNumber }}' when it should have been '{{ user.dealerAccountNumber }}', so it was just a dumb variable name mistake on my part. I'm actually still not sure why it was showing that particular error, though.
shanecavaliere
<templating><plugin-development><users><emails>
Error when rendering a template (for HTML email) on users.saveUser
# Error when rendering a template (for HTML email) on users.saveUser

(Asked by: username_0 on 2014-06-23)

I'm writing a simple project-specific plugin that sends a notification to the site admin when a new user is registered. I have an HTML template that I'd like to use for this.
In my plugin's main class, I have the following function:
public function init()
    {
        parent::init();
        // Events
        craft()->on('users.saveUser', function(Event $event) {
            $user = $event->params['user'];
            $isNewUser = $event->params['isNewUser'];

            if ($isNewUser)
            {
                // Send notification email
                craft()->aeiMember->registrationNotification($user);
            }
        });
    }

which calls a one of my plugin's services, 'registrationNotification'. Here's the code I have for that service:
public function registrationNotification(UserModel $savedUser)
    {
        $user = new \stdClass();

        $user->firstName            = $savedUser->firstName;
        $user->lastName             = $savedUser->lastName;
        $user->email                = $savedUser->email;
        $user->aeiGroups            = $savedUser->aeiGroups;
        $user->phone                = $savedUser->phone;
        $user->mailingAddress       = $savedUser->mailingAddress;
        $user->city                 = $savedUser->city;
        $user->state                = $savedUser->state;
        $user->zip                  = $savedUser->zip;
        $user->country              = $savedUser->country;
        $user->preferredDealer      = $savedUser->preferredDealer;
        $user->dealerAccountNumber  = $savedUser->dealerAccountNumber;
        $user->school               = $savedUser->school;
        $user->expectedGraduationDate = $savedUser->expectedGraduationDate;

        $email = new EmailModel();
        $email->toEmail = '...myemailaddress...';
        $email->subject = 'New User Registration: ' . $user->firstName . ' ' . $user->lastName;
        $email->htmlBody = craft()->templates->render('aeiMember/registration_notification', array(
            'user' => $user,
        ));

        // Send
        if (craft()->email->sendEmail($email))
        {
            return true;
        }
    }

When I submit the registration form, I get the following error: "CDbException: CDbTransaction is inactive and cannot perform commit or roll back operations.". I narrowed down the issue, and if I use a non-HTML email body, everything works fine, so apparently there's an issue with rendering a template at this event.
Here's a link to a Google Doc with the stack trace: https://docs.google.com/document/d/1atFNkktuZQ91hU9QipXYMhOCHNGAavO5qUI_TF5jSeE/edit?usp=sharing
Am I doing something wrong, or is there a workaround to accomplish what I'm after? It seems like it should be pretty simple.

**Tags:** templating, plugin-development, users, emails

> **Comments:**
> **username_1**: Is your non-HTML email body still accessing all the values of the stdClass object? CDbTransaction is a database class for DB transactions and I can't even begin to understand why this code here is concerned with hitting the database. Do you have devMode on? If so, could you post the stack trace?  
> **username_0**: Good point - no, the non-HTML email body wasn't accessing any values. I reviewed the template, and the only variables it is accessing are 'user' and the global 'now'.  I do have devMode on, I've edited my question to include a link to the stack trace.  
> **username_2**: If you substitute the craft()->templates->render call with some static html, do you get the same error? If you do the template render call and store the contents in a variable (and don't add it as a htmlBody), do you get the same error?  
> **username_1**: What's confusing is that the stack trace shows the error on saving the user, which should happen *before* this is triggered, so I don't know why the email template would affect it. As far as I can tell it hasn't even hit the "'onEndRequest'" yet. If @aelvan's questions don't get you anywhere, you might need to just submit a support request.  
> **username_3**: What does the registration_notification template look like?  Something is triggering a call to UsersController->saveUser when calling render() and I suspect that's something in your template code?  
> **username_0**: Well, I figured out what the issue was. In my template, I had a '{{ user.accountNumber }}' when it should have been '{{ user.dealerAccountNumber }}', so it was just a dumb variable name mistake on my part. I'm actually still not sure why it was showing that particular error, though.  

---

## 2 answers

### Answer by username_0 on 2014-06-24. Score: 5

The issue turned out to be the result of a variable name mistake on my part. I had typed in an incorrect variable name in the email template. I actually still don't really understand what that has to do with a database transaction, though.

EDIT:
If anyone else is running into a similar issue, I've found a way to get a more useful error message, as opposed to the "CDbTransaction is inactive" message, which doesn't help much. If you go to craft/app/services/UsersService.php, and comment out line 311 ("$transaction->rollback();"), then Yii won't give you the CDbTransaction error before Craft has a chance to tell you its more helpful error. This should give you a better idea of what you're doing wrong. Obviously, don't forget to un-comment that line after you've fixed your issue.

> **Comments:**
> **username_3**: It was probably triggering some lazy-loading method for users which was, in-turn, making a call to the database to attempt to retrieve data.  
> **username_4**: Very helpful EDIT tip. The line is #320 now. (Craft 2.6.2780)  

---

### Answer by username_5 on 2015-10-25. Score: 1

It throws the error, since it fires the event in the transaction try-catch, and Twig was throwing the exception. In the catch block then it tried to execute the rollback.


---

