<p>Below is the full block of code I am working with. This is part of a template for showing all businesses that belong to a sub-category. I need to load all businesses related to the current category, but in addition to that I need to group the businesses by state and list the town each business belongs to. I've asked other questions related to this before. This time I am actively trying to use Eager Loading to load each related town for each business. From there I'm hoping to pull out the state each business belongs to (this needs to be daisy chained, however, from business->town->county->region->state).</p>

<p>The code below results in the following error:</p>

<p><strong>CException</strong><br>
<em>Craft\EntryModel and its behaviors do not have a method or closure named "N".</em></p>

<p>And finally, here's the code:</p>

<pre><code>    {% block entry_content %}
        {%
            set businesses = craft.entries.section('businesses').relatedTo(category).find({
                with: ['bizTown']
            })
        %}

        {% set state = entry.bizTown.first.townCounty.first.countyRegion.first.regionState.first ?? 'N/A' %}

        {% set businessesByState = businesses|group(state) %}

        &lt;div class="container"&gt;
            &lt;div class="card"&gt;

                &lt;p&gt;Businesses with video stories will have the video icon &lt;i class="fa fa-video-camera" aria-hidden="true"&gt;&lt;/i&gt; in front of their names.&lt;/p&gt;

                {% if businesses|length %}
                    {% for state, businessesInState in businessesByState %}
                        &lt;ul class="three-col-list"&gt;
                            &lt;h3&gt;{{state}}&lt;/h3&gt;
                            &lt;ul&gt;
                                {% for business in businessesInState %}
                                    &lt;li class="business-cat__item"&gt;
                                        {% if business.ytVideoId|length %}
                                            &lt;i class="fa fa-video-camera" aria-hidden="true"&gt;&lt;/i&gt;
                                        {% endif %}
                                        &lt;strong&gt;{{ business.link }}&lt;/strong&gt;&lt;br&gt;
                                        {% set twn = business.bizTown.first %}
                                        &lt;a class="business-cat__item--town" href="{{twn.url}}"&gt;{{twn.title}}&lt;/a&gt;
                                    &lt;/li&gt;
                                {% endfor %}
                            &lt;/ul&gt;
                        &lt;/ul&gt;
                    {% endfor %}
                {% else %}

                    &lt;p&gt;We apologize, we appear to be experiencing some technical difficulties…&lt;/p&gt;

                {% endif %}

            &lt;/div&gt;
        &lt;/div&gt;


    {% endblock entry_content %}
</code></pre>

<p>There's got to be something simple I'm overlooking, or I could be approaching my problem all wrong. I feel out of my element and could use some advice/wisdom.</p>

<p>Thanks!</p>

CC BY-SA 3.0
2017-08-01T17:40:43.513Z
If you enable devMode and reproduce, you'll be able to see the compiled template in the `craft/storage/runtime/compiled_templates` folder in the stack trace the error is originating from. From there you'll see a comment with the line number from _your_ original template file. Which line in your template is generating that error?
Brad Bell
CC BY-SA 3.0
2017-08-01T18:40:15.890Z
Hmm, looks like it is saying line 1. =\ Here is a gist that includes both the full compiled template and the full actual template code: https://gist.github.com/swthate/532c9b5f279433dddf80885d9b99cea6

Also, I noticed I made an embarrassing typo when trying to call "with", I had typed "width". I fixed that and the behavior persists.
Steven Thate
CC BY-SA 3.0
2017-08-01T19:25:58.360Z
Probably not your issue, but on line 52 if `state` does end up being set to `N/A`, then you're grouping by N/A right after that.  `{% set businessesByState = businesses|group('N/A') %}` Unless you've got a state defined as "N/A" you're selecting that'll be a logic error.
Brad Bell
CC BY-SA 3.0
2017-08-01T20:37:13.957Z
Removing `?? 'N/A'` changes the error, which now highlights that line 52 and says `Variable "entry" does not exist.`. Having `entry` there felt really weird, anyway! Not sure why it was there to begin with, I think it was just copied over from another template. Should line 52 be placed within a `for` loop, e.g. `for business in businesses`. That seems like it'd be the opposite of what I want though, to make the querie(s) on the page more efficient.
Steven Thate
CC BY-SA 3.0
2017-08-02T03:20:53.490Z
I'd probably start with that and see how it works... my guess is the queries would be similar especially if you used something like https://github.com/marionnewlevant/craft-mnbreakandcontinue
Brad Bell
<entrymodel><eager-loading>
Getting "do not have method or closure named" error when trying to eager load
# Getting "do not have method or closure named" error when trying to eager load

(Asked by: username_0 on 2017-08-01)

Below is the full block of code I am working with. This is part of a template for showing all businesses that belong to a sub-category. I need to load all businesses related to the current category, but in addition to that I need to group the businesses by state and list the town each business belongs to. I've asked other questions related to this before. This time I am actively trying to use Eager Loading to load each related town for each business. From there I'm hoping to pull out the state each business belongs to (this needs to be daisy chained, however, from business->town->county->region->state).
The code below results in the following error:
CException
Craft\EntryModel and its behaviors do not have a method or closure named "N".
And finally, here's the code:
    {% block entry_content %}
        {%
            set businesses = craft.entries.section('businesses').relatedTo(category).find({
                with: ['bizTown']
            })
        %}

        {% set state = entry.bizTown.first.townCounty.first.countyRegion.first.regionState.first ?? 'N/A' %}

        {% set businessesByState = businesses|group(state) %}

        
            

                Businesses with video stories will have the video icon  in front of their names.

                {% if businesses|length %}
                    {% for state, businessesInState in businessesByState %}
                        
                            {{state}}
                            
                                {% for business in businessesInState %}
                                    
                                        {% if business.ytVideoId|length %}
                                            
                                        {% endif %}
                                        {{ business.link }}
                                        {% set twn = business.bizTown.first %}
                                        {{twn.title}}
                                    
                                {% endfor %}
                            
                        
                    {% endfor %}
                {% else %}

                    We apologize, we appear to be experiencing some technical difficulties…

                {% endif %}

            
        


    {% endblock entry_content %}

There's got to be something simple I'm overlooking, or I could be approaching my problem all wrong. I feel out of my element and could use some advice/wisdom.
Thanks!

**Tags:** entrymodel, eager-loading
> **Comments:**
> **username_1**: If you enable devMode and reproduce, you'll be able to see the compiled template in the `craft/storage/runtime/compiled_templates` folder in the stack trace the error is originating from. From there you'll see a comment with the line number from _your_ original template file. Which line in your template is generating that error?  
> **username_0**: Hmm, looks like it is saying line 1. =\ Here is a gist that includes both the full compiled template and the full actual template code: https://gist.github.com/swthate/532c9b5f279433dddf80885d9b99cea6  Also, I noticed I made an embarrassing typo when trying to call "with", I had typed "width". I fixed that and the behavior persists.  
> **username_1**: Probably not your issue, but on line 52 if `state` does end up being set to `N/A`, then you're grouping by N/A right after that.  `{% set businessesByState = businesses|group('N/A') %}` Unless you've got a state defined as "N/A" you're selecting that'll be a logic error.  
> **username_0**: Removing `?? 'N/A'` changes the error, which now highlights that line 52 and says `Variable "entry" does not exist.`. Having `entry` there felt really weird, anyway! Not sure why it was there to begin with, I think it was just copied over from another template. Should line 52 be placed within a `for` loop, e.g. `for business in businesses`. That seems like it'd be the opposite of what I want though, to make the querie(s) on the page more efficient.  
> **username_1**: I'd probably start with that and see how it works... my guess is the queries would be similar especially if you used something like https://github.com/marionnewlevant/craft-mnbreakandcontinue  

---

## 0 answers

