Answers.Body: <p>The <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a> is an object that prepares your queries. For example when you do <code>craft.entries</code> you'll receive an <code>ElementCriteriaModel</code> where you can insert ids, sections, relations and many other attributes until you finally <a href="https://craftcms.com/docs/templating/elementcriteriamodel#fetching-the-elements" rel="nofollow noreferrer">fetch</a> your entries with <code>find()</code>, <code>first()</code> or <code>ids()</code>. </p>
  
  <p>When you have a relation field attached to the entry the values of these fields are objects of type <code>ElementCriteriaModel</code> too, so you have a ready to use query that can be executed. </p>
  
  <p>This class has an <code>iterator</code> interface, that means the method <code>find()</code> is executed when you loop through it, that being said <code>for image in entry.images</code> does actually <code>for image in entry.images.find()</code>. Thus many people think <code>entry.images</code> contains an array of elements rather than one single element. </p>
  
  <p>So - for future reference - when you ever receive an error like</p>
  
  <blockquote>
    <p>Object of class Craft\ElementCriteriaModel could not be converted to XXX</p>
  </blockquote>
  
  <p>It's likely you treated your <code>ElementCriteriaModel</code> as an array or element or string or whatever the error explains. </p>
  
  <p>In your special case it was just a syntax error: change <code>gridImages.length</code> to <code>gridImages|length</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-30T17:49:40.040Z
Answers.LastActivityDate: 2018-01-30T17:49:40.040Z
Answers.UserDisplayName: Robin Schambach
Body: <p>We just created an entry and hit save.  When we go to the front end of site we get a popup error that reads: </p>
  
  <blockquote>
    <p>Object of class Craft\ElementCriteriaModel could not be converted to
    int</p>
  </blockquote>
  
  <p>What does this mean and how do I troubleshoot it</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T15:31:37.663Z
Comments.Text: The `ElementCriteriaModel` is an object that prepares your queries. For example if you do `craft.entries` you'll receive an `ElementCriteriaModel` where you can insert ids, sections, relations and so on until you finally fetch all entries with the `find()` function. When you have a relation field attached to the entry the values of these are objects of type `ElementCriteriaModel` too, so you have a ready to use query that can be executed. It's likely you'll receive this error because you are trying to treat it as an integer in your templates. Do you have a relation field in your entry?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T15:52:09.980Z
Comments.Text: Thanks for super fast response! I don't think so, but can't tell.  Can I tell by looking at the entry fields when creating a page?  If so, then I see plain text fields, image fields, text sections that have wysiwyg fields.<br/><br/> If it is looking into the templates then I'm not sure what to look for there.  I do see some for loops etc, but I don't see something that 'relates' one entry to another entry or entry type.
Comments.UserDisplayName: Noah Learner
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T15:53:56.453Z
Comments.Text: Image fields are relation fields. Your image is nothing but an element and the field contains a query (lazy loading) when you do `entry.image` you have an `ElementCriteriaModel` so please update your answer with your template where you use your image. Many people think `entry.image` will return the images that are used in the entry but thats totally wrong.
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T15:58:42.247Z
Comments.Text: I'm learning markdown so bear with me here... I pasted the template into a jsbin: https://jsbin.com/vurokurayi/edit?html,output
Comments.UserDisplayName: Noah Learner
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T16:05:31.493Z
Comments.Text: Next time please insert the code here at stack exchange in your question and format it a little bit so people can read it. I guess your error is in this line `gridImages.length != 1` it should be `gridImages|length` by the way - as I said gridImages is an object of type `ElementCriteriaModel` and no array. With the `|length` filter you execute the query and count the results. Doing `set count = gridImages|length` is a little bit better performance wise because you save the number once.
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T16:46:59.560Z
Comments.Text: I made the gridImages.length fix and the page loaded fine.  I now see your next comment and don't understand how to fix the block.next() issue.  Are you willing to share a fix or edit the jsbin I linked to?  I will also format templates / code inside here at SE in future.. I appreciate your patience with me as a noob.
Comments.UserDisplayName: Noah Learner
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T16:48:40.417Z
Comments.Text: No no please forget about that. It was my mistake. I didn't realize you made a check with `loop.last` that's why I deleted my comment. Sorry for the confusion. I'm glad I could help you. Just one last hint: some of your if condition will always become true, no matter what. For example `if gridImages` you don't need all those checks because they'll all contain objects, not arrays. With `if gridImages` you check if gridImages is not null (it's always an object) with `for image in gridImages` you'll receive null (no image) or an array of images
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T17:39:16.963Z
Comments.Text: How do I mark your comment as the best answer?
Comments.UserDisplayName: Noah Learner
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-30T17:50:43.090Z
Comments.Text: You can't mark comments, that's why I collected the information from my comments and created an answer, you can mark that as accepted so others will know the problem is solved. Thank you
Comments.UserDisplayName: Robin Schambach
Tags: <entries>
Title: Why am I getting error: Object of class Craft\ElementCriteriaModel could not be converted to int
ThreadText: # Why am I getting error: Object of class Craft\ElementCriteriaModel could not be converted to int
  
  (Asked by: username_0 on 2018-01-30)
  
  We just created an entry and hit save. When we go to the front end of site we get a popup error that reads:
  
  > Object of class Craft\ElementCriteriaModel could not be converted to
  > int
  
  What does this mean and how do I troubleshoot it
  
  **Tags:** entries
  
  > **Comments:**
  > **username_1**: The `ElementCriteriaModel` is an object that prepares your queries. For example if you do `craft.entries` you'll receive an `ElementCriteriaModel` where you can insert ids, sections, relations and so on until you finally fetch all entries with the `find()` function. When you have a relation field attached to the entry the values of these are objects of type `ElementCriteriaModel` too, so you have a ready to use query that can be executed. It's likely you'll receive this error because you are trying to treat it as an integer in your templates. Do you have a relation field in your entry?  
  > **username_0**: Thanks for super fast response! I don't think so, but can't tell.  Can I tell by looking at the entry fields when creating a page?  If so, then I see plain text fields, image fields, text sections that have wysiwyg fields.<br/><br/> If it is looking into the templates then I'm not sure what to look for there.  I do see some for loops etc, but I don't see something that 'relates' one entry to another entry or entry type.  
  > **username_1**: Image fields are relation fields. Your image is nothing but an element and the field contains a query (lazy loading) when you do `entry.image` you have an `ElementCriteriaModel` so please update your answer with your template where you use your image. Many people think `entry.image` will return the images that are used in the entry but thats totally wrong.  
  > **username_0**: I'm learning markdown so bear with me here... I pasted the template into a jsbin: https://jsbin.com/vurokurayi/edit?html,output  
  > **username_1**: Next time please insert the code here at stack exchange in your question and format it a little bit so people can read it. I guess your error is in this line `gridImages.length != 1` it should be `gridImages|length` by the way - as I said gridImages is an object of type `ElementCriteriaModel` and no array. With the `|length` filter you execute the query and count the results. Doing `set count = gridImages|length` is a little bit better performance wise because you save the number once.  
  > **username_0**: I made the gridImages.length fix and the page loaded fine.  I now see your next comment and don't understand how to fix the block.next() issue.  Are you willing to share a fix or edit the jsbin I linked to?  I will also format templates / code inside here at SE in future.. I appreciate your patience with me as a noob.  
  > **username_1**: No no please forget about that. It was my mistake. I didn't realize you made a check with `loop.last` that's why I deleted my comment. Sorry for the confusion. I'm glad I could help you. Just one last hint: some of your if condition will always become true, no matter what. For example `if gridImages` you don't need all those checks because they'll all contain objects, not arrays. With `if gridImages` you check if gridImages is not null (it's always an object) with `for image in gridImages` you'll receive null (no image) or an array of images  
  > **username_0**: How do I mark your comment as the best answer?  
  > **username_1**: You can't mark comments, that's why I collected the information from my comments and created an answer, you can mark that as accepted so others will know the problem is solved. Thank you  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-01-30. Score: 4
  
  The [ElementCriteriaModel](https://craftcms.com/docs/templating/elementcriteriamodel) is an object that prepares your queries. For example when you do `craft.entries` you'll receive an `ElementCriteriaModel` where you can insert ids, sections, relations and many other attributes until you finally [fetch](https://craftcms.com/docs/templating/elementcriteriamodel#fetching-the-elements) your entries with `find()`, `first()` or `ids()`.
  
  When you have a relation field attached to the entry the values of these fields are objects of type `ElementCriteriaModel` too, so you have a ready to use query that can be executed.
  
  This class has an `iterator` interface, that means the method `find()` is executed when you loop through it, that being said `for image in entry.images` does actually `for image in entry.images.find()`. Thus many people think `entry.images` contains an array of elements rather than one single element.
  
  So - for future reference - when you ever receive an error like
  
  > Object of class Craft\ElementCriteriaModel could not be converted to XXX
  
  It's likely you treated your `ElementCriteriaModel` as an array or element or string or whatever the error explains.
  
  In your special case it was just a syntax error: change `gridImages.length` to `gridImages|length`
  
  
  ---
  
  