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


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

if (!Craft::$app->elements->saveElement($entry)) {

$existingEntry = Entry::find()


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.


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 Answer 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)) {

// No need to query, you can already access the Entry you just saved

// 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);

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

  • 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
    Commented May 23, 2021 at 9:51
  • @flipjms did the answer solve your question? Commented May 24, 2021 at 15:16
  • No, not really!
    – flipjms
    Commented May 25, 2021 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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