<p>The actual answer is mostly unrelated to your question, but I wanted to provide some context, first!</p>

<p>Entry Types don't get their own PHP classes. All Entries are just instances of the <code>craft\elements\Entry</code> class, distinguished by their <code>section</code> (<code>$entry-&gt;getSection()</code>) and <code>type</code> (<code>$entry-&gt;getType()</code>) values.</p>

<p>If you need to check the section <em>and</em> type of your entry, you can compare against the <code>sectionId</code> and <code>typeId</code> properties. Keep in mind that these IDs may be different, across environments, so it's better to use the <code>handle</code> and keep it in sync with your <code>project.yml</code>:</p>

<pre class="lang-php prettyprint-override"><code>if ($entry-&gt;getSection()-&gt;handle == 'mySectionHandle' &amp;&amp; $entry-&gt;getType()-&gt;handle == 'myEntryTypeHandle') {
  // Your logic!
}
</code></pre>

<p>Ok, now on to the error you're seeing!</p>

<pre><code>Calling unknown method: craft\elements\db\AssetQuery::getUrl()
</code></pre>

<p>This is actually telling you that the accessed field/property (<code>photo</code>) returns an object that doesn't have a <code>getUrl()</code> method.</p>

<p>Unless <a href="https://docs.craftcms.com/v3/relations.html" rel="nofollow noreferrer">relational field data</a> is <a href="https://docs.craftcms.com/v3/dev/eager-loading-elements.html" rel="nofollow noreferrer">eager-loaded</a>, accessing its property will return an <code>ElementQuery</code> (in your case, an <code>AssetQuery</code>), prepped to return the related Elements.</p>

<p>So, before accessing methods on the expected value (an <code>Asset</code> Element), you have to execute the query:</p>

<pre class="lang-php prettyprint-override"><code>$photo = $entry-&gt;photo-&gt;one();

if ($photo) {
  // At least one photo was attached! You can safely access the Asset object's properties + methods:
  $url = $photo-&gt;getUrl();
} else {
  // Set a default:
  $url = 'https://...';
}
</code></pre>

CC BY-SA 4.0
2020-04-26T04:17:55.667Z
Understood well & working perfectly. Thanks v.much for providing the detailed context. Marking your answer as the correct one. Thanks again!
nparabz
CC BY-SA 4.0
2020-04-26T04:45:37.033Z
Hi again. I'm facing a new issue with Craft. Since you are knowledgeable about Craft, maybe you could help me out with this one too? Thanks in advance. https://craftcms.stackexchange.com/questions/35208/elementsevent-after-save-element-event-handler-in-module-being-called-mult
nparabz
CC BY-SA 4.0
2020-04-25T20:56:45.917Z
2020-04-25T20:56:45.917Z
August Miller
<p>Newbie to Craft CMS. I am trying to send a push notification to an app when an entry of specific type (NewsItem) is saved, and I'm doing so via a Craft Module. I got this code snippet from another StackExchange question, and I have changed it a bit to implement it according to my needs:</p>

<pre><code>        Craft::$app-&gt;elements-&gt;on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {

            // Make sure it's an entry.
            if ($event-&gt;element instanceof craft\elements\Entry) {
                $entry = $event-&gt;element;

                // Check if it's in the section you care about
                if ($entry-&gt;sectionId == 1) {
                    // Do your custom logic

                        if(isset($entry-&gt;photo))
                            $image_name = $entry-&gt;photo-&gt;getUrl();
                        else
                            $image_name = 'http://img.youtube.com/vi/' . $entry-&gt;youtubeVideoCode . '/0.jpg';
</code></pre>

<p>I'm getting this error:</p>

<pre><code>Calling unknown method: craft\elements\db\AssetQuery::getUrl()
</code></pre>

<p>I may be wrong, but I'm assuming I have to cast $entry to (NewsItem) somewhere in the code to access the getUrl() function? How and where do I do that?</p>

<p>Thanks in advance for your help.</p>

<craft3>
Cast Entry to appropriate type to access fields and functions in module
# Cast Entry to appropriate type to access fields and functions in module

(Asked by: username_0 on 2020-04-25)

Newbie to Craft CMS. I am trying to send a push notification to an app when an entry of specific type (NewsItem) is saved, and I'm doing so via a Craft Module. I got this code snippet from another StackExchange question, and I have changed it a bit to implement it according to my needs:
        Craft::$app->elements->on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {

            // Make sure it's an entry.
            if ($event->element instanceof craft\elements\Entry) {
                $entry = $event->element;

                // Check if it's in the section you care about
                if ($entry->sectionId == 1) {
                    // Do your custom logic

                        if(isset($entry->photo))
                            $image_name = $entry->photo->getUrl();
                        else
                            $image_name = 'http://img.youtube.com/vi/' . $entry->youtubeVideoCode . '/0.jpg';

I'm getting this error:
Calling unknown method: craft\elements\db\AssetQuery::getUrl()

I may be wrong, but I'm assuming I have to cast $entry to (NewsItem) somewhere in the code to access the getUrl() function? How and where do I do that?
Thanks in advance for your help.

**Tags:** craft3


---

## 1 answers

### Answer by username_1 on 2020-04-25. Score: 1 | Accepted answer

The actual answer is mostly unrelated to your question, but I wanted to provide some context, first!
Entry Types don't get their own PHP classes. All Entries are just instances of the craft\elements\Entry class, distinguished by their section ($entry->getSection()) and type ($entry->getType()) values.
If you need to check the section and type of your entry, you can compare against the sectionId and typeId properties. Keep in mind that these IDs may be different, across environments, so it's better to use the handle and keep it in sync with your project.yml:
if ($entry->getSection()->handle == 'mySectionHandle' && $entry->getType()->handle == 'myEntryTypeHandle') {
  // Your logic!
}

Ok, now on to the error you're seeing!
Calling unknown method: craft\elements\db\AssetQuery::getUrl()

This is actually telling you that the accessed field/property (photo) returns an object that doesn't have a getUrl() method.
Unless relational field data is eager-loaded, accessing its property will return an ElementQuery (in your case, an AssetQuery), prepped to return the related Elements.
So, before accessing methods on the expected value (an Asset Element), you have to execute the query:
$photo = $entry->photo->one();

if ($photo) {
  // At least one photo was attached! You can safely access the Asset object's properties + methods:
  $url = $photo->getUrl();
} else {
  // Set a default:
  $url = 'https://...';
}

> **Comments:**
> **username_0**: Understood well & working perfectly. Thanks v.much for providing the detailed context. Marking your answer as the correct one. Thanks again!  
> **username_0**: Hi again. I'm facing a new issue with Craft. Since you are knowledgeable about Craft, maybe you could help me out with this one too? Thanks in advance. https://craftcms.stackexchange.com/questions/35208/elementsevent-after-save-element-event-handler-in-module-being-called-mult  

---

