Questions tagged [elementcriteriamodel]

Questions having to do with Craft's ElementCriteriaModel, which is used to return a set of elements narrowed by a variety of parameters.

Filter by
Sorted by
Tagged with
5 votes
1 answer
172 views

Many entry fields, only one db request?

Craft 101: I have entries with many different fields, so I'm doing something like this: {% set var = craft.entries.section('foo').slug('bar').first() %} {% if var %} {{ var.field1 }} {{ var....
  • 339
2 votes
1 answer
639 views

Or element criteria model filters [duplicate]

Im trying to do an OR filter for craft.entries and not sure of the syntax so like this {% set startedThisYear = craft.entries.find({ section: 'exhibitions', startDate: ">= " ~startDate, startDate:...
2 votes
1 answer
1k views

Working with non-craft database table

What is the best way to work with non-craft database tables as models? I have a given table and I should render its records with multiple filtering and pagination. Importing them to entries is not an ...
user avatar
4 votes
1 answer
915 views

How can I output a list of categories that are not empty?

In other words, categories must have at least one enabled entry assigned to them.
  • 1,503
6 votes
1 answer
517 views

Display thumbnail using defineTableAttributes for a custom Element Type

Hi, I'm new to Craft and VERY new to plugin development. I'd like to display a thumbnail image in my table list view and thought it would be possible to accomplish using a new Custom Element Type. ...
  • 121
1 vote
1 answer
563 views

Using modifyElementsQuery to get plugin's custom table data

I need to join my plugin's custom table to a an ElementCriteriaModel (ECM). I am trying to use modifyElementsQuery. In the plugin's custom table, rows can share an element/entry via a separate id. ...
  • 2,797
4 votes
1 answer
1k views

Build Query relatedTo multiple categories

Trying to build a complex query and I'm sure there is bound to be a better way of doing it that the multiple if statements I currently have. URL for the page is /products/(category1)/(category2)/(...
  • 1,779
2 votes
1 answer
2k views

How can I search for entries that are enabled OR disabled?

Within a plugin I am fetching existing entries using the CriteriaModel class as documented here http://buildwithcraft.com/docs/plugins/working-with-elements#fetching-elements, specifically like this: ...
  • 1,503
1 vote
1 answer
2k views

Get a page URL using only the Slug?

Wondered what the best approach would be to get the url for a given entry page using only a slug for reference? I imagined it being something like this: public function getPageURL($slug) { $page ...
  • 2,315
5 votes
2 answers
1k views

How to retrieve entries from two entry-types ordered by different date-fields?

I have two entry-types, 'Events' and 'Reports'. 'Events' have a matrix-field ('eventMultiDate') that allows authors to assign multiple dates to an event (recurring). 'Reports' have the default '...
5 votes
1 answer
1k views

How can I create an ElementCriteriaModel with 'and' and 'or'

In my template, I want to: Get all the entries in the section 'uploads'; Get all the assets that are related to one of the upload entries, and have a relation to all the tags I provide. So, that ...
  • 6,318
2 votes
1 answer
508 views

Can someone help me identify why my entry is invalidating?

I have a custom plugin that's saving an entry successfully. While saving the entry I'm setting our "merchant" field (an entries field, limit=1) like so: <?php # ... $entry = newEntryModel(); # set ...
1 vote
2 answers
438 views

Can I have multiple comparisons within a single criteria parameter?

The Craft site shows an example using a single comparison Building upon this example from that page: craft.entries.width('>= 100') Can I do something similar but where 'width' is ALSO less than ...
0 votes
1 answer
106 views

What is the difference between elements.enabled and elements_i18n.enabled?

It appears, toggling the status button on an entry affects only the elements.enabled value. What affects the elements_i18n.enabled value? When and how would we choose to update the enabled column in ...
  • 13.3k
4 votes
2 answers
2k views

How do I find the closest parent entry with an Assets field filled in?

I have a structure that goes up to 4 levels deep. Each page has a field for a 'hero' image. If a child page does not have this field filled out I would like to traverse up the structure and find the ...
  • 43
1 vote
1 answer
204 views

Modify an ElementCriteriaModel to compare against numeric field

Can anyone tell me why this is returning all entries instead of entries with a creditsUsed field > 0? creditsUsed is a number field that I created for this entry type. $criteria = craft()->...
7 votes
1 answer
1k views

How can I do more complicated queries with an ElementCriteriaModel?

I need to join my 3rd party table with a standard ElementCriteriaModel (ECM). I'd like to start with an ordinary ECM, so my plugin will be compatible with any element type which may exist, now or in ...
  • 23.8k
4 votes
2 answers
479 views

What's the difference between ElementCriteriaModel and DbCommand?

Forgive me if this is a stupid or naive question... I generally think of an ElementCriteriaModel as being a "search object". Specifically, it's a search object which revolves around a specific type ...
  • 23.8k
4 votes
1 answer
772 views

Using search to display entries based on a value in a number field

I'm trying to return results based on a year stored in a custom field. For example, returning entries with a model year greater than 2006 and less than 2009. Is there a search syntax to support this? ...
  • 367
2 votes
1 answer
294 views

How can I join plugin data with entry data based on uri?

I have a plugin with a record (and model). The record have attributes uri and hits. The uri attribute in my record corresponds to an entry's uri, as stored in the craft_elements_i18n table. What is ...
  • 7,258
6 votes
1 answer
1k views

How can I follow relations in ElementCriteriaModels from plugin code?

In Twig code you can use craft->entries->relatedTo to do all sorts of neat things. From plugin code I am not sure how this works? $c = craft()->elements->getCriteria(ElementType::Entry)...
  • 631
6 votes
1 answer
675 views

How can I return specific matrix rows based on criteria?

So let's say I have the following code. I have the data to retrieve the entry I want, But I only want to return some of the matrix rows based on some criteria. Example Code: {% set entryId = 1 %} {% ...
2 votes
1 answer
109 views

How can I get a related Asset of a specific field?

I'm trying to grab an Asset which is a child of my Entry... $criteria = craft()->elements->getCriteria(ElementType::Asset); $criteria->childOf = $entryId; $photo = craft()->assets->...
  • 23.8k
4 votes
1 answer
1k views

Complex logic on a ElementCriteriaModel parameter?

How would you combine multiple logical expressions on a ElementCriteriaModel parameter? You could query twice and then merge the two returned arrays of entry models, but that probably wouldn't allow ...
  • 36k
3 votes
1 answer
2k views

What's the difference between total function and count function?

I found out that the functions count and total on the elementCriteriaModel show different results: The following examples gave different results. paginate.total was the same as total, but not correct....
  • 131
6 votes
2 answers
707 views

Ability to SUM() or GROUP BY from the template or ElementCriteriaModel

After twitter conversation with Brandon Kelly, is it possible to implement something similar to the following? craft.entries.section("mySection").sum("myCustomField")
5 votes
1 answer
732 views

How can you return elements between two values?

I my be in a bit of a "wood for the trees" situation but how is it possible to return elements based on a custom field being between two supplied values. For example returning users based on a number ...
1 vote
1 answer
96 views

Problem with Pulling Event Entries

The following code is throwing an error for me. {% set eventEntries = craft.entries.find({ section: 'events', event_startDateTime: '<= {{ now }}', limit: '4' }) %} {% for entry in ...
11 votes
3 answers
3k views

How to search for a specific field using getCriteria()?

I'm looking at making a plugin and need to search news entries. I can use: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'news'; $find = $criteria->...
  • 375
8 votes
1 answer
9k views

Order entries by date field

I have a channel section with a handle named 'events'. In my field layout for this section I have a field named eventsDate, which is a Date/Time field. I also have a plain text field called '...
  • 605
4 votes
1 answer
175 views

Is it possible for an Element Type to return multiple content tables with the getContentTableForElementsQuery() method?

I'm building a plugin where each Element created has a unique content table associated with it. Each individual Element behaves as needed, however, when I try to create an index page that returns all ...
  • 13.3k
12 votes
2 answers
1k views

How to find entries with no category

I have a field categories in a channel news, and I want to find all the entries in news that don't have a category set. I know I could loop over the entries and find them that way, but I'm hoping to ...
3 votes
1 answer
671 views

How to fetch specific users OR admins with the ElementCriteriaModel?

I'm getting all Users from specific UserGroups like this: $criteria = craft()->elements->getCriteria(ElementType::User); $criteria->groupId = array(1, 3); $users = $criteria->find(); But ...
  • 8,346
3 votes
1 answer
817 views

Ordering of ElementCriteriaModel with varying statuses

I have a built a little plugin that calls the Twitter API to add new Tweets from a user timeline as entries, so that I can return them in an ElementCriteriaModel request to be integrated with content ...
  • 945
12 votes
2 answers
1k views

What are the differences between an elementCriteriaModel and fetched elements?

I am a little confused about the differences between an elementCriteriaModel and fetched elements. Why do I sometimes have to fetch elements with first(), last(), find(): {% set entries = craft....
  • 8,346
22 votes
3 answers
4k views

How can I get only those entries with non-empty assets field

Since Craft 2.0 it is possible to pass :empty: and :notempty: to ElementCriteriaModel parameters when you’re looking for empty/non-empty values. If you are for example looking for all entries with a ...
  • 36k
15 votes
1 answer
1k views

How do you sort elements queried from multiple channels?

This is how I query elements from multiple channels: {% set allEntries = craft.entries.section(['publication','text', 'exhibition']).limit(null) %} I now want to sort all these entries by a custom ...
  • 36k

1 2 3
4