1

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

  • 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? – Robin Schambach Jan 30 '18 at 15:31
  • 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. – Noah Learner Jan 30 '18 at 15:52
  • 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. – Robin Schambach Jan 30 '18 at 15:53
  • I'm learning markdown so bear with me here... I pasted the template into a jsbin: jsbin.com/vurokurayi/edit?html,output – Noah Learner Jan 30 '18 at 15:58
  • 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. – Robin Schambach Jan 30 '18 at 16:05
4

The 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 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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