<p>I'm not sure if it's any less dirty, but another way you could do it would be:</p>

<pre><code>public function init()
{
    craft()-&gt;on('entries.saveEntry', function(Event $event)
    {
        $entry = $event-&gt;params['entry'];

        if ($entry-&gt;section-&gt;name == "Classes")
        {
            craft()-&gt;db-&gt;createCommand()-&gt;update(
                'entries',
                array('expiryDate' =&gt; DateTimeHelper::formatTimeForDb($entry-&gt;classDate)),
                array('id' =&gt; $entry-&gt;id)
            );
        }
    });
}
</code></pre>

CC BY-SA 3.0
2015-07-04T04:51:49.360Z
Ah yes, db->createCommand-update... never thought about hitting the db "directly" but I like the idea! Simple. It works but the expiryDate is not matching up with the classDate.  Looks like the db stores in UTC? I got it to work by doing  ``array('expiryDate' => DateTimeHelper::formatTimeForDb($entry->classDate))`` not sure if there's a better way?
RitterKnight
CC BY-SA 3.0
2015-07-04T13:53:56.753Z
Nope, that's the way to do it... I'l update the answer.
Brad Bell
CC BY-SA 3.0
2015-07-04T00:46:57.010Z
2015-07-04T13:54:20.133Z
2015-07-04T13:54:20.133Z
Brad Bell
<p>I'm trying to update the expiry field—based on an existing field (in this case <code>classDate</code>)—as soon as an entry is saved.</p>

<p>Based on <a href="https://craftcms.stackexchange.com/questions/6662/auto-expire-on-entries">this answer</a>, the clever recursion code in carlcs' <a href="https://github.com/carlcs/craft-autoexpire/blob/master/autoexpire/AutoExpirePlugin.php#L61" rel="nofollow noreferrer">Auto Expire</a> plugin, I was able to get it working with this code:</p>

<pre><code>public function init()
{

craft()-&gt;on('entries.saveEntry', function(Event $event) {
    $entry = $event-&gt;params['entry'];

    if ($entry-&gt;section-&gt;name == "Classes")
    {
        $entry-&gt;expiryDate = $entry-&gt;classDate;

        static $recursionLevel = 0;

        if ($recursionLevel == 0)
         {
            $recursionLevel++;
            $saved =   craft()-&gt;entries-&gt;saveEntry($entry);
         }

    }


});
}
</code></pre>

<p>But that seems a little dirty because <code>saveEntry</code> is going to get called again. </p>

<p>Is there a better way to handle the recursion? <a href="https://craftcms.stackexchange.com/questions/10271/updating-an-entry-after-an-event-action-within-onsaveentry">Bob's answer here</a> leads me to believe you can call <code>craft()-&gt;elements-&gt;saveElement</code> which won't raise the <code>saveEntry</code> event again but I couldn't get that to work.</p>

<p>What am I missing? Seems too easy...</p>

<plugin-development><entry><events>
Handling Endless Recursion in saveEntry?
# Handling Endless Recursion in saveEntry?

(Asked by: username_0 on 2015-07-03)

I'm trying to update the expiry field—based on an existing field (in this case classDate)—as soon as an entry is saved.
Based on this answer, the clever recursion code in carlcs' Auto Expire plugin, I was able to get it working with this code:
public function init()
{

craft()->on('entries.saveEntry', function(Event $event) {
    $entry = $event->params['entry'];

    if ($entry->section->name == "Classes")
    {
        $entry->expiryDate = $entry->classDate;

        static $recursionLevel = 0;

        if ($recursionLevel == 0)
         {
            $recursionLevel++;
            $saved =   craft()->entries->saveEntry($entry);
         }

    }


});
}

But that seems a little dirty because saveEntry is going to get called again. 
Is there a better way to handle the recursion? Bob's answer here leads me to believe you can call craft()->elements->saveElement which won't raise the saveEntry event again but I couldn't get that to work.
What am I missing? Seems too easy...

**Tags:** plugin-development, entry, events


---

## 1 answers

### Answer by username_1 on 2015-07-04. Score: 2 | Accepted answer

I'm not sure if it's any less dirty, but another way you could do it would be:
public function init()
{
    craft()->on('entries.saveEntry', function(Event $event)
    {
        $entry = $event->params['entry'];

        if ($entry->section->name == "Classes")
        {
            craft()->db->createCommand()->update(
                'entries',
                array('expiryDate' => DateTimeHelper::formatTimeForDb($entry->classDate)),
                array('id' => $entry->id)
            );
        }
    });
}

> **Comments:**
> **username_0**: Ah yes, db->createCommand-update... never thought about hitting the db "directly" but I like the idea! Simple. It works but the expiryDate is not matching up with the classDate.  Looks like the db stores in UTC? I got it to work by doing  ``array('expiryDate' => DateTimeHelper::formatTimeForDb($entry->classDate))`` not sure if there's a better way?  
> **username_1**: Nope, that's the way to do it... I'l update the answer.  

---

