<p>My working solution:</p>

<pre><code>public function actionResaveEntries()
{
    $site = Craft::$app-&gt;sites-&gt;getSiteById(self::SITE_ID);
    $section = Craft::$app-&gt;sections-&gt;getSectionById(self::SECTION_ID);
    $entryType = Craft::$app-&gt;sections-&gt;getEntryTypeById(self::ENTRY_TYPE_ID);

    $entries = Entry::find()
        -&gt;siteId(self::SITE_ID)
        -&gt;sectionId(self::SECTION_ID)
        -&gt;typeId(self::COMP_ENTRY_TYPE_ID)
        -&gt;all();

    foreach ($entries as $entry) {
        // Set deadline
        $deadline = $entry-&gt;competitionJudgingEnd;
        // Check if after deadline
        $now = new DateTime();
        if ($now &gt; $deadline) {
            Craft::$app-&gt;getQueue()-&gt;push(new ResaveElements([
                'description' =&gt; Craft::t('app', 'Resaving {section}, {type} entries', [
                    'section' =&gt; $section, 'type' =&gt; $entryType-&gt;name,
                ]),
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; [
                    'siteId' =&gt; $site-&gt;id,
                    'sectionId' =&gt; $section-&gt;id,
                    'typeId' =&gt; $entryType-&gt;id,
                    'status' =&gt; null,
                    'enabledForSite' =&gt; true
                ]
            ]));
            echo "Resaving entries\n"
        } else 
            echo "Not resaving entries\n";
        }
    }
}
</code></pre>

CC BY-SA 4.0
2018-12-07T17:57:44.637Z
2018-12-07T17:57:44.637Z
Andrea DeMers
<p>Scenario: I want to programatically bulk resave entries in a certain Site in a certain Section of a certain Type.</p>

<p>I have a Craft 3 Module with a Console Controller containing following code:</p>

<pre><code>public function actionResaveEntries()
{
    $site = Craft::$app-&gt;sites-&gt;getSiteById(self::SITE_ID);
    $section = Craft::$app-&gt;sections-&gt;getSectionById(self::SECTION_ID);
    $entryType = Craft::$app-&gt;sections-&gt;getEntryTypeById(self::ENTRY_TYPE_ID);

    Craft::$app-&gt;getQueue()-&gt;push(new ResaveElements([
        'description' =&gt; Craft::t('app', 'Resaving {section}, {type} entries', [
            'section' =&gt; $section, 'type' =&gt; $entryType-&gt;name,
        ]),
        'elementType' =&gt; Entry::class,
        'criteria' =&gt; [
            'siteId' =&gt; $site-&gt;id,
            'sectionId' =&gt; $section-&gt;id,
            'typeId' =&gt; $entryType-&gt;id,
            'status' =&gt; null,
            'enabledForSite' =&gt; true
        ]
    ]));

    $result = 'something';
    echo "Resaving entries\n";
    return $result;
}
</code></pre>

<p>This works as expected when running <code>./craft resave-entries-module/resave-entries/resave-entries</code> from the command line &amp; also via a cron job.</p>

<p>However, I now want to modify my code to run <code>Craft::$app-&gt;getQueue()-&gt;push(new ResaveElements()</code> only under certain conditions. Specifically, when a deadline (custom date/time field in entry type) has passed.</p>

<p>I tried following, but it doesn't run at all regardless if before or after deadline date. The conditional seems to be ignored.</p>

<pre><code>public function actionResaveEntries()
{
    $site = Craft::$app-&gt;sites-&gt;getSiteById(self::SITE_ID);
    $section = Craft::$app-&gt;sections-&gt;getSectionById(self::SECTION_ID);
    $entryType = Craft::$app-&gt;sections-&gt;getEntryTypeById(self::ENTRY_TYPE_ID);

    $entries = Entry::find()
        -&gt;siteId(self::SITE_ID)
        -&gt;sectionId(self::SECTION_ID)
        -&gt;typeId(self::COMP_ENTRY_TYPE_ID)
        -&gt;all();

    foreach ($entries as $entry) {
        // Set deadline
        $deadline = $entry-&gt;competitionJudgingEnd;
        // Check if after deadline
        $now = new DateTime();
        if ($now &gt; $deadline) {
            Craft::$app-&gt;getQueue()-&gt;push(new ResaveElements([
                'description' =&gt; Craft::t('app', 'Resaving {section}, {type} entries', [
                    'section' =&gt; $section, 'type' =&gt; $entryType-&gt;name,
                ]),
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; [
                    'siteId' =&gt; $site-&gt;id,
                    'sectionId' =&gt; $section-&gt;id,
                    'typeId' =&gt; $entryType-&gt;id,
                    'status' =&gt; null,
                    'enabledForSite' =&gt; true
                ]
            ]));

            //$result = 'something';
            echo "Resaving entries\n";
            //return $result;
        } else {
            //$result = 'nothing';
            echo "Not resaving entries\n";
            //return $result;
        }
    }
</code></pre>

<p>In an effort to troubleshoot, I changed the conditional to <code>if ($entry-&gt;title == "A known title")</code>, but it also seems to be ignored. I.e. resave not occurring.</p>

<p>What am I doing wrong, and what can I do to get this working?</p>

<p><strong>UPDATE 01:</strong> The conditionals are actually working. There are 3 entries that meet criteria in <code>$entries = Entry::find()</code>. However, the loop seems to be stop at the first entry. This entry changes if I change the criteria.</p>

<p><strong>UPDATE 02:</strong> It looks like the <code>return $result;</code> was breaking the loop. Updated code above by commenting out return statements.</p>

<entries><craft3><plugins><modules>
How can I conditionally resave entries based on the value of an entry's field in a Craft 3 Module/Plugin?
# How can I conditionally resave entries based on the value of an entry's field in a Craft 3 Module/Plugin?

(Asked by: username_0 on 2018-11-16)

Scenario: I want to programatically bulk resave entries in a certain Site in a certain Section of a certain Type.
I have a Craft 3 Module with a Console Controller containing following code:
public function actionResaveEntries()
{
    $site = Craft::$app->sites->getSiteById(self::SITE_ID);
    $section = Craft::$app->sections->getSectionById(self::SECTION_ID);
    $entryType = Craft::$app->sections->getEntryTypeById(self::ENTRY_TYPE_ID);

    Craft::$app->getQueue()->push(new ResaveElements([
        'description' => Craft::t('app', 'Resaving {section}, {type} entries', [
            'section' => $section, 'type' => $entryType->name,
        ]),
        'elementType' => Entry::class,
        'criteria' => [
            'siteId' => $site->id,
            'sectionId' => $section->id,
            'typeId' => $entryType->id,
            'status' => null,
            'enabledForSite' => true
        ]
    ]));

    $result = 'something';
    echo "Resaving entries\n";
    return $result;
}

This works as expected when running ./craft resave-entries-module/resave-entries/resave-entries from the command line & also via a cron job.
However, I now want to modify my code to run Craft::$app->getQueue()->push(new ResaveElements() only under certain conditions. Specifically, when a deadline (custom date/time field in entry type) has passed.
I tried following, but it doesn't run at all regardless if before or after deadline date. The conditional seems to be ignored.
public function actionResaveEntries()
{
    $site = Craft::$app->sites->getSiteById(self::SITE_ID);
    $section = Craft::$app->sections->getSectionById(self::SECTION_ID);
    $entryType = Craft::$app->sections->getEntryTypeById(self::ENTRY_TYPE_ID);

    $entries = Entry::find()
        ->siteId(self::SITE_ID)
        ->sectionId(self::SECTION_ID)
        ->typeId(self::COMP_ENTRY_TYPE_ID)
        ->all();

    foreach ($entries as $entry) {
        // Set deadline
        $deadline = $entry->competitionJudgingEnd;
        // Check if after deadline
        $now = new DateTime();
        if ($now > $deadline) {
            Craft::$app->getQueue()->push(new ResaveElements([
                'description' => Craft::t('app', 'Resaving {section}, {type} entries', [
                    'section' => $section, 'type' => $entryType->name,
                ]),
                'elementType' => Entry::class,
                'criteria' => [
                    'siteId' => $site->id,
                    'sectionId' => $section->id,
                    'typeId' => $entryType->id,
                    'status' => null,
                    'enabledForSite' => true
                ]
            ]));

            //$result = 'something';
            echo "Resaving entries\n";
            //return $result;
        } else {
            //$result = 'nothing';
            echo "Not resaving entries\n";
            //return $result;
        }
    }

In an effort to troubleshoot, I changed the conditional to if ($entry->title == "A known title"), but it also seems to be ignored. I.e. resave not occurring.
What am I doing wrong, and what can I do to get this working?
UPDATE 01: The conditionals are actually working. There are 3 entries that meet criteria in $entries = Entry::find(). However, the loop seems to be stop at the first entry. This entry changes if I change the criteria.
UPDATE 02: It looks like the return $result; was breaking the loop. Updated code above by commenting out return statements.

**Tags:** entries, craft3, plugins, modules


---

## 1 answers

### Answer by username_0 on 2018-12-07. Score: 0 | Accepted answer

My working solution:
public function actionResaveEntries()
{
    $site = Craft::$app->sites->getSiteById(self::SITE_ID);
    $section = Craft::$app->sections->getSectionById(self::SECTION_ID);
    $entryType = Craft::$app->sections->getEntryTypeById(self::ENTRY_TYPE_ID);

    $entries = Entry::find()
        ->siteId(self::SITE_ID)
        ->sectionId(self::SECTION_ID)
        ->typeId(self::COMP_ENTRY_TYPE_ID)
        ->all();

    foreach ($entries as $entry) {
        // Set deadline
        $deadline = $entry->competitionJudgingEnd;
        // Check if after deadline
        $now = new DateTime();
        if ($now > $deadline) {
            Craft::$app->getQueue()->push(new ResaveElements([
                'description' => Craft::t('app', 'Resaving {section}, {type} entries', [
                    'section' => $section, 'type' => $entryType->name,
                ]),
                'elementType' => Entry::class,
                'criteria' => [
                    'siteId' => $site->id,
                    'sectionId' => $section->id,
                    'typeId' => $entryType->id,
                    'status' => null,
                    'enabledForSite' => true
                ]
            ]));
            echo "Resaving entries\n"
        } else 
            echo "Not resaving entries\n";
        }
    }
}


---

