Skip to content

saveElement not updating entry? #5878

@heymarkreeves

Description

@heymarkreeves

Description

I've been using the following code in a plugin on a few sites to create/update entries from a news feed.

I'm finding that after the entries have first been created, they're not getting updated. I'm seeing multiple records for each entry in the entries table. If I edit the entry in the control panel, deleting the link field value, for example, save that, and then rerun the loop, I don't see link value set in that field again. I see a new entry record in the database, but I don't see a new entry in the control panel.

I had set up the section with revisions enabled, and I could see revisions were being created with the updated value, but the Current version didn't have it. I disabled revisions, deleted all the entries via the control panel, and ran the loop again. Same issue.

Per the docs, saveElement should just save the entry with the updated content, right?

$result = Entry::find()->section('pressReleases')
            ->where([
                'field_prNasdaqId' => $pressReleaseId
            ])->one();
if (!isset($result)) {
    /*
    ** Create new Craft entry
    */
    $postJson = json_decode(file_get_contents($pressRelease->body[0]->link->url));

    $entry = new Entry();
    $entry->sectionId = 23;
    $entry->typeId = 29;
    $entry->authorId = 5985;
    $entry->title = $title;
    $entry->prNasdaqId = $pressReleaseId;
    $entry->prNasdaqLink = (string)$pressRelease->body[0]->link->url;
    $entry->postDate = new \DateTime($pressRelease->releaseDate->dateUTC.'+00:00');
    $entry->prNasdaqLastUpdated = new \DateTime($pressRelease->lastUpdatedUTC.'+00:00');
    if(Craft::$app->elements->saveElement($entry)) {
        // echo 'Successfully created new entry<br />';
    } else {
        throw new \Exception("Couldn't create new entry: " . print_r($entry->getErrors(), true));
    }
}
else {
    /*
    ** Update existing Craft entry
    */
    $postJson = json_decode(file_get_contents($pressRelease->body[0]->link->url));
    $result->title = $title;
    $result->prNasdaqId = $pressReleaseId;
    $result->prNasdaqLink = (string)$pressRelease->body[0]->link->url;
    $result->postDate = new \DateTime($pressRelease->releaseDate->dateUTC.'+00:00');
    $result->prNasdaqLastUpdated = new \DateTime($pressRelease->lastUpdatedUTC.'+00:00');
    if(Craft::$app->elements->saveElement($result)) {
        echo 'Successfully saved entry<br />';
    } else {
        throw new \Exception("Couldn't save entry: " . print_r($result->getErrors(), true));
    }
}

Additional info

  • Craft version: 3.4.13
  • PHP version:
  • Database driver & version: MySQL
  • Plugins & versions:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions