Answers.Body: <p>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.</p>
  <p>The <code>$entry</code> object that you pass to the <code>craft\services\elements::saveElement()</code> method will get updated in the same scope that you are running the <code>saveElement</code> code. It appears that you are already accessing that same <code>$entry</code> object when you are using it to throw errors.</p>
  <p>See the <a href="https://www.php.net/manual/en/language.oop5.references.php" rel="nofollow noreferrer">Objects and References</a> page in the PHP docs for a discussion of the behavior.</p>
  <p>Here's a rough update of your code per that expectation:</p>
  <pre><code>$entry = new Entry([stuff here]);
  $entry-&gt;title = 'foo';
  
  if (!Craft::$app-&gt;elements-&gt;saveElement($entry)) {
    dd($entry-&gt;getErrors());
  }
  
  // No need to query, you can already access the Entry you just saved
  $entry-&gt;title
  
  // If you need to query, you could do so via the ID 
  // from the Entry Element object you just saved:
  $entry = Craft::$app-&gt;elements-&gt;getElementById($entry-&gt;id);
  $entry-&gt;title
  </code></pre>
  <p>Is there a reason you need to query the Element again right after you save it?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-23T09:51:05.903Z
Answers.Text: 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).
Answers.UserDisplayName: flipjms
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-24T15:16:14.207Z
Answers.Text: @flipjms did the answer solve your question?
Answers.UserDisplayName: RitterKnight
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-25T09:35:46.437Z
Answers.Text: No, not really!
Answers.UserDisplayName: flipjms
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-22T03:30:47.450Z
Answers.LastActivityDate: 2021-05-22T03:46:12.503Z
Answers.LastEditDate: 2021-05-22T03:46:12.503Z
Answers.UserDisplayName: Ben Parizek
Body: <p>I'm having this issue where when I save an Entry and try to fetch right away I get a <code>null</code>.</p>
  <p>Example:</p>
  <pre><code>$entry = new Entry([stuff here]);
  $entry-&gt;title = 'foo';
  
  if (!Craft::$app-&gt;elements-&gt;saveElement($entry)) {
      dd($entry-&gt;getErrors());
  }
  
  $existingEntry = Entry::find()
              -&gt;noCache()
              -&gt;section('handle')
              -&gt;title('foo')
              -&gt;one();
  
  //null
  </code></pre>
  <p>This is just to simplify the issue I'm having.</p>
  <p>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 <code>null</code>.
  I thought it could be cache, hence the <code>noCache()</code> but nope.</p>
  <p>And I can also confirm that the entry is saved/created. If I go to the dashboard I can see it there.</p>
  <p><strong>Clarification:</strong></p>
  <p>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.</p>
  <p>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.</p>
  
Tags: <plugin-development><entry><saveentry>
Title: Retrieving entry just after saving it returns null
ThreadText: # Retrieving entry just after saving it returns null
  
  (Asked by: username_0 on 2021-05-21)
  
  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.
  
  **Tags:** plugin-development, entry, saveentry
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-05-22. Score: 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](https://www.php.net/manual/en/language.oop5.references.php) 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?
  
  > **Comments:**
  > **username_0**: 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).  
  > **username_2**: @username_0 did the answer solve your question?  
  > **username_0**: No, not really!  
  
  ---
  
  