Skip to content

Commit

Permalink
Rewrote Element Query docs
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Aug 21, 2018
1 parent b046233 commit cfbe017
Show file tree
Hide file tree
Showing 8 changed files with 3,982 additions and 2,038 deletions.
75 changes: 0 additions & 75 deletions docs/dev/element-queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,81 +49,6 @@ Each type of element has its own function for creating element queries, and they
Most custom fields support element query parameters as well, named after the field handles.
:::

## Attribute Parameters

Many parameters correspond to a particular element attribute. For example, entry queries have a `postDate` parameter that allows you to limit the results to entries with a matching _Post Date_.

Attribute parameters generally support a variety of syntaxes:

| Syntax | Find elements where the param’s attribute…
| - | -
| `param('X')` | is _X_.
| `param('= X')` | is _X_.
| `param('not X')` | is not _X_.
| `param('!= X')` | is not _X_.
| `param('<= X')` | is less than or equal to _X_.
| `param('>= X')` | is greater than or equal to _X_.
| `param('< X')` | is less than _X_.
| `param('> X')` | is greater than _X_.
| `param('*X*')` | contains _X_.
| `param('not *X*')` | doesn’t contain _X_.
| `param(':empty:)` | has no value.
| `param(':notempty:')` | has any value.

You can also specify multiple parameter values by setting the parameter to an array:

| Syntax | Find elements where the param’s attribute…
| - | -
| `param(['X', 'Y']` | is either _X_ or _Y_.
| `param(['or', 'X', 'Y'])` | is either _X_ or _Y_.
| `param(['and', 'X', 'Y'])` | is both _X_ and _Y_.

In these cases, _X_ and _Y_ can follow any of the supported parameter syntaxes.

For example, let’s say you wanted to load entries that were posted sometime in the last month.

<code-toggle :languages="['twig', 'php']">
<template slot="twig">

```twig{7}
{# Set the first and last dates that we want to fetch entries from #}
{% set start = date('first day of last month')|atom %}
{% set end = date('last day of last month')|atom %}
{# Get all the entries that were posted between those dates #}
{% set entries = craft.entries()
.postDate(['and', ">= #{start}", "<= #{end}"])
.all() %}
```
::: tip TWIG TIP
We’re using [interpolation](https://twig.symfony.com/doc/2.x/templates.html#string-interpolation) to inject the `start` and `end` variables into the value strings, which felt a little cleaner than the `~` operator:

```twig{2}
{% set entries = craft.entries()
.postDate(['and', '>= '~start, '<= '~end])
.all() %}
```
:::

</template>
<template slot="php">

```php{9}
use craft\elements\Entry;
// Set the first and last dates that we want to fetch entries from
$start = new \DateTime('first day of next month')->format(\DateTime::ATOM);
$end = new \DateTime('last day of next month')->format(\DateTime::ATOM);
Get all the entries that were posted between those dates
$entries = Entry::find()
->postDate(['and', ">= {$start}", "<= {$end}"])
->all();
```

</template>
</code-toggle>

## Executing Element Queries

Once you’ve defined your parameters on the query, there are multiple functions available to execute it, depending on what you need back.
Expand Down
Loading

0 comments on commit cfbe017

Please sign in to comment.