2

I'm having this issue where when I save an Entry and try to fetch right away I get a null.

Example:

$entry = new Entry([stuff here]);
$entry->title = 'foo';

if (!Craft::$app->elements->saveElement($entry)) {
    dd($entry->getErrors());
}

$existingEntry = Entry::find()
            ->noCache()
            ->section('handle')
            ->title('foo')
            ->one();

//null

This is just to simplify the issue I'm having.

My use case is actually a loop that checks if the entry already exists (according to the title), if so it adds data to it, if not, creates it and adds the data. What is happening is that I get multiple entries created with the same title because when I check if it exists, I get a null. I thought it could be cache, hence the noCache() but nope.

And I can also confirm that the entry is saved/created. If I go to the dashboard I can see it there.

Clarification:

The snippet above is just to explain it in a simple way what the problem is. It's not actual code I'm using. I am aware that I do have the entry object and it doesn't make sense to fetch it right after saving it.

My question is solely about why I cannot get an entry that was previously saved. I find this behaviour weird and was not expecting it.

1

Searching for an Element by Title, as you describe, is potentially problematic as the Title is not required to be unique. But may not even be necessary in your case.

The $entry object that you pass to the craft\services\elements::saveElement() method will get updated in the same scope that you are running the saveElement code. It appears that you are already accessing that same $entry object when you are using it to throw errors.

See the Objects and References page in the PHP docs for a discussion of the behavior.

Here's a rough update of your code per that expectation:

$entry = new Entry([stuff here]);
$entry->title = 'foo';

if (!Craft::$app->elements->saveElement($entry)) {
  dd($entry->getErrors());
}

// No need to query, you can already access the Entry you just saved
$entry->title

// If you need to query, you could do so via the ID 
// from the Entry Element object you just saved:
$entry = Craft::$app->elements->getElementById($entry->id);
$entry->title

Is there a reason you need to query the Element again right after you save it?

3
  • I don't need to query the Element right after I save it. That was just to simplify the issue. I have a loop that needs to check the existence of the Element by title (hence using the title). If it doesn't exist I need to create it, if it does exist I use the one that is already there. I can save in a buffer the ones I'm creating and check that instead but at this point I'm more interested in understanding this behaviour of craft (like I said on the clarification I added).
    – flipjms
    May 23 at 9:51
  • @flipjms did the answer solve your question? May 24 at 15:16
  • No, not really!
    – flipjms
    May 25 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.