diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index ab0d0c3de34..0f6a50863c1 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -2,11 +2,15 @@ ## Unreleased +### Changed +- Improved the button labels in the confirmation dialog that can appear after running the Asset Indexes utility. ([#4943](https://github.com/craftcms/cms/issues/4943)) + ### Fixed - Fixed a bug where asset queries’ `withTransforms` param wasn’t working for eager-loaded assets. ([#4931](https://github.com/craftcms/cms/issues/4931)) - Fixed a bug where the "Edit Image" element action sometimes would be missing. ([#3349](https://github.com/craftcms/cms/issues/3349)) - Fixed a bug where querying for elements by their Lightswitch field value could only return elements that had been saved since the Lightswitch field was added. ([#4939](https://github.com/craftcms/cms/issues/4939)) - Fixed a bug where the "Update all" button was missing on the updates page. ([#4938](https://github.com/craftcms/cms/issues/4938)) +- Fixed a bug where the “Updating search indexes” job could fail when updating search indexes for a Matrix block that contained a relational field. - Fixed a bug where it was impossible to query for entries using author id with the GraphQL API. ## 3.3.3 - 2019-09-12 diff --git a/docs/dev/element-queries/asset-queries.md b/docs/dev/element-queries/asset-queries.md index ced33f53019..3495a60efd3 100644 --- a/docs/dev/element-queries/asset-queries.md +++ b/docs/dev/element-queries/asset-queries.md @@ -212,6 +212,128 @@ $assets = \craft\elements\Asset::find() ::: +### `draftCreator` + +Narrows the query results to only drafts created by a given user. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch drafts by the current user #} +{% set assets = craft.assets() + .draftCreator(currentUser) + .all() %} +``` + +```php +// Fetch drafts by the current user +$assets = \craft\elements\Asset::find() + ->draftCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `draftId` + +Narrows the query results based on the assets’ draft’s ID (from the `drafts` table). + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the draft with an ID of 1. + + + +::: code +```twig +{# Fetch a draft #} +{% set assets = craft.assets() + .draftId(10) + .all() %} +``` + +```php +// Fetch a draft +$assets = \craft\elements\Asset::find() + ->draftIf(10) + ->all(); +``` +::: + + +### `draftOf` + +Narrows the query results to only drafts of a given asset. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the asset with an ID of 1. +| a [Asset](api:craft\elements\Asset) object | for the asset represented by the object. + + + +::: code +```twig +{# Fetch drafts of the asset #} +{% set assets = craft.assets() + .draftOf(myAsset) + .all() %} +``` + +```php +// Fetch drafts of the asset +$assets = \craft\elements\Asset::find() + ->draftOf($myAsset) + ->all(); +``` +::: + + +### `drafts` + +Narrows the query results to only drafts assets. + + + + + +::: code +```twig +{# Fetch a draft asset #} +{% set assets = {twig-function} + .drafts() + .id(123) + .one() %} +``` + +```php +// Fetch a draft asset +$assets = \craft\elements\Asset::find() + ->drafts() + ->id(123) + ->one(); +``` +::: + + ### `filename` Narrows the query results based on the assets’ filenames. @@ -383,6 +505,20 @@ This can be combined with [fixedOrder](#fixedorder) if you want the results to b ::: +### `ignorePlaceholders` + +Causes the query to return matching assets as they are stored in the database, ignoring matching placeholder +elements that were set by [craft\services\Elements::setPlaceholderElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-setplaceholderelement). + + + + + + + + + + ### `inReverse` Causes the query results to be returned in reverse order. @@ -559,6 +695,41 @@ $assets = \craft\elements\Asset::find() ::: +### `preferSites` + +If [unique](#unique) is set, this determines which site should be selected when querying multi-site elements. + + + +For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, +and this is set to `['c', 'b', 'a']`, then Foo will be returned for Site C, and Bar will be returned +for Site B. + +If this isn’t set, then preference goes to the current site. + + + +::: code +```twig +{# Fetch unique assets from Site A, or Site B if they don’t exist in Site A #} +{% set assets = craft.assets() + .site('*') + .unique() + .preferSites(['a', 'b']) + .all() %} +``` + +```php +// Fetch unique assets from Site A, or Site B if they don’t exist in Site A +$assets = \craft\elements\Asset::find() + ->site('*') + ->unique() + ->preferSites(['a', 'b']) + ->all(); +``` +::: + + ### `relatedTo` Narrows the query results to only assets that are related to certain other elements. @@ -586,6 +757,128 @@ $assets = \craft\elements\Asset::find() ::: +### `revisionCreator` + +Narrows the query results to only revisions created by a given user. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch revisions by the current user #} +{% set assets = craft.assets() + .revisionCreator(currentUser) + .all() %} +``` + +```php +// Fetch revisions by the current user +$assets = \craft\elements\Asset::find() + ->revisionCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `revisionId` + +Narrows the query results based on the assets’ revision’s ID (from the `revisions` table). + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the revision with an ID of 1. + + + +::: code +```twig +{# Fetch a revision #} +{% set assets = craft.assets() + .revisionId(10) + .all() %} +``` + +```php +// Fetch a revision +$assets = \craft\elements\Asset::find() + ->revisionIf(10) + ->all(); +``` +::: + + +### `revisionOf` + +Narrows the query results to only revisions of a given asset. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the asset with an ID of 1. +| a [Asset](api:craft\elements\Asset) object | for the asset represented by the object. + + + +::: code +```twig +{# Fetch revisions of the asset #} +{% set assets = craft.assets() + .revisionOf(myAsset) + .all() %} +``` + +```php +// Fetch revisions of the asset +$assets = \craft\elements\Asset::find() + ->revisionOf($myAsset) + ->all(); +``` +::: + + +### `revisions` + +Narrows the query results to only revision assets. + + + + + +::: code +```twig +{# Fetch a revision asset #} +{% set assets = {twig-function} + .revisions() + .id(123) + .one() %} +``` + +```php +// Fetch a revision asset +$assets = \craft\elements\Asset::find() + ->revisions() + ->id(123) + ->one(); +``` +::: + + ### `search` Narrows the query results to only assets that match a search query. @@ -639,7 +932,7 @@ Possible values include: ::: tip If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you -only want unique elements to be returned, use `\craft\elements\db\unique()` in conjunction with this. +only want unique elements to be returned, use [unique](#unique) in conjunction with this. ::: @@ -806,6 +1099,36 @@ $asset = \craft\elements\Asset::find() ::: +### `unique` + +Determines whether only elements with unique IDs should be returned by the query. + + + +This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not +desired. + + + +::: code +```twig +{# Fetch unique assets across all sites #} +{% set assets = craft.assets() + .site('*') + .unique() + .all() %} +``` + +```php +// Fetch unique assets across all sites +$assets = \craft\elements\Asset::find() + ->site('*') + ->unique() + ->all(); +``` +::: + + ### `volume` Narrows the query results based on the volume the assets belong to. diff --git a/docs/dev/element-queries/category-queries.md b/docs/dev/element-queries/category-queries.md index 1d9bf0c091a..b46e4811506 100644 --- a/docs/dev/element-queries/category-queries.md +++ b/docs/dev/element-queries/category-queries.md @@ -313,6 +313,128 @@ This can be combined with [descendantDist](#descendantdist) if you want to limit ::: +### `draftCreator` + +Narrows the query results to only drafts created by a given user. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch drafts by the current user #} +{% set categories = craft.categories() + .draftCreator(currentUser) + .all() %} +``` + +```php +// Fetch drafts by the current user +$categories = \craft\elements\Category::find() + ->draftCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `draftId` + +Narrows the query results based on the categories’ draft’s ID (from the `drafts` table). + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the draft with an ID of 1. + + + +::: code +```twig +{# Fetch a draft #} +{% set categories = craft.categories() + .draftId(10) + .all() %} +``` + +```php +// Fetch a draft +$categories = \craft\elements\Category::find() + ->draftIf(10) + ->all(); +``` +::: + + +### `draftOf` + +Narrows the query results to only drafts of a given category. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the category with an ID of 1. +| a [Category](api:craft\elements\Category) object | for the category represented by the object. + + + +::: code +```twig +{# Fetch drafts of the category #} +{% set categories = craft.categories() + .draftOf(myCategory) + .all() %} +``` + +```php +// Fetch drafts of the category +$categories = \craft\elements\Category::find() + ->draftOf($myCategory) + ->all(); +``` +::: + + +### `drafts` + +Narrows the query results to only drafts categories. + + + + + +::: code +```twig +{# Fetch a draft category #} +{% set categories = {twig-function} + .drafts() + .id(123) + .one() %} +``` + +```php +// Fetch a draft category +$categories = \craft\elements\Category::find() + ->drafts() + ->id(123) + ->one(); +``` +::: + + ### `enabledForSite` Narrows the query results based on whether the categories are enabled in the site they’re being queried in, per the [site](#site) parameter. @@ -504,6 +626,20 @@ This can be combined with [fixedOrder](#fixedorder) if you want the results to b ::: +### `ignorePlaceholders` + +Causes the query to return matching categories as they are stored in the database, ignoring matching placeholder +elements that were set by [craft\services\Elements::setPlaceholderElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-setplaceholderelement). + + + + + + + + + + ### `inReverse` Causes the query results to be returned in reverse order. @@ -756,6 +892,41 @@ $categories = \craft\elements\Category::find() ::: +### `preferSites` + +If [unique](#unique) is set, this determines which site should be selected when querying multi-site elements. + + + +For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, +and this is set to `['c', 'b', 'a']`, then Foo will be returned for Site C, and Bar will be returned +for Site B. + +If this isn’t set, then preference goes to the current site. + + + +::: code +```twig +{# Fetch unique categories from Site A, or Site B if they don’t exist in Site A #} +{% set categories = craft.categories() + .site('*') + .unique() + .preferSites(['a', 'b']) + .all() %} +``` + +```php +// Fetch unique categories from Site A, or Site B if they don’t exist in Site A +$categories = \craft\elements\Category::find() + ->site('*') + ->unique() + ->preferSites(['a', 'b']) + ->all(); +``` +::: + + ### `prevSiblingOf` Narrows the query results to only the category that comes immediately before another category. @@ -815,6 +986,128 @@ $categories = \craft\elements\Category::find() ::: +### `revisionCreator` + +Narrows the query results to only revisions created by a given user. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch revisions by the current user #} +{% set categories = craft.categories() + .revisionCreator(currentUser) + .all() %} +``` + +```php +// Fetch revisions by the current user +$categories = \craft\elements\Category::find() + ->revisionCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `revisionId` + +Narrows the query results based on the categories’ revision’s ID (from the `revisions` table). + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the revision with an ID of 1. + + + +::: code +```twig +{# Fetch a revision #} +{% set categories = craft.categories() + .revisionId(10) + .all() %} +``` + +```php +// Fetch a revision +$categories = \craft\elements\Category::find() + ->revisionIf(10) + ->all(); +``` +::: + + +### `revisionOf` + +Narrows the query results to only revisions of a given category. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the category with an ID of 1. +| a [Category](api:craft\elements\Category) object | for the category represented by the object. + + + +::: code +```twig +{# Fetch revisions of the category #} +{% set categories = craft.categories() + .revisionOf(myCategory) + .all() %} +``` + +```php +// Fetch revisions of the category +$categories = \craft\elements\Category::find() + ->revisionOf($myCategory) + ->all(); +``` +::: + + +### `revisions` + +Narrows the query results to only revision categories. + + + + + +::: code +```twig +{# Fetch a revision category #} +{% set categories = {twig-function} + .revisions() + .id(123) + .one() %} +``` + +```php +// Fetch a revision category +$categories = \craft\elements\Category::find() + ->revisions() + ->id(123) + ->one(); +``` +::: + + ### `search` Narrows the query results to only categories that match a search query. @@ -900,7 +1193,7 @@ Possible values include: ::: tip If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you -only want unique elements to be returned, use `\craft\elements\db\unique()` in conjunction with this. +only want unique elements to be returned, use [unique](#unique) in conjunction with this. ::: @@ -1111,6 +1404,36 @@ $category = \craft\elements\Category::find() ::: +### `unique` + +Determines whether only elements with unique IDs should be returned by the query. + + + +This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not +desired. + + + +::: code +```twig +{# Fetch unique categories across all sites #} +{% set categories = craft.categories() + .site('*') + .unique() + .all() %} +``` + +```php +// Fetch unique categories across all sites +$categories = \craft\elements\Category::find() + ->site('*') + ->unique() + ->all(); +``` +::: + + ### `uri` Narrows the query results based on the categories’ URIs. diff --git a/docs/dev/element-queries/entry-queries.md b/docs/dev/element-queries/entry-queries.md index 0357e790c6e..8dd350005f2 100644 --- a/docs/dev/element-queries/entry-queries.md +++ b/docs/dev/element-queries/entry-queries.md @@ -474,6 +474,128 @@ This can be combined with [descendantDist](#descendantdist) if you want to limit ::: +### `draftCreator` + +Narrows the query results to only drafts created by a given user. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch drafts by the current user #} +{% set entries = craft.entries() + .draftCreator(currentUser) + .all() %} +``` + +```php +// Fetch drafts by the current user +$entries = \craft\elements\Entry::find() + ->draftCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `draftId` + +Narrows the query results based on the entries’ draft’s ID (from the `drafts` table). + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the draft with an ID of 1. + + + +::: code +```twig +{# Fetch a draft #} +{% set entries = craft.entries() + .draftId(10) + .all() %} +``` + +```php +// Fetch a draft +$entries = \craft\elements\Entry::find() + ->draftIf(10) + ->all(); +``` +::: + + +### `draftOf` + +Narrows the query results to only drafts of a given entry. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the entry with an ID of 1. +| a [Entry](api:craft\elements\Entry) object | for the entry represented by the object. + + + +::: code +```twig +{# Fetch drafts of the entry #} +{% set entries = craft.entries() + .draftOf(myEntry) + .all() %} +``` + +```php +// Fetch drafts of the entry +$entries = \craft\elements\Entry::find() + ->draftOf($myEntry) + ->all(); +``` +::: + + +### `drafts` + +Narrows the query results to only drafts entries. + + + + + +::: code +```twig +{# Fetch a draft entry #} +{% set entries = {twig-function} + .drafts() + .id(123) + .one() %} +``` + +```php +// Fetch a draft entry +$entries = \craft\elements\Entry::find() + ->drafts() + ->id(123) + ->one(); +``` +::: + + ### `enabledForSite` Narrows the query results based on whether the entries are enabled in the site they’re being queried in, per the [site](#site) parameter. @@ -637,6 +759,20 @@ This can be combined with [fixedOrder](#fixedorder) if you want the results to b ::: +### `ignorePlaceholders` + +Causes the query to return matching entries as they are stored in the database, ignoring matching placeholder +elements that were set by [craft\services\Elements::setPlaceholderElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-setplaceholderelement). + + + + + + + + + + ### `inReverse` Causes the query results to be returned in reverse order. @@ -926,6 +1062,41 @@ $entries = \craft\elements\Entry::find() ::: +### `preferSites` + +If [unique](#unique) is set, this determines which site should be selected when querying multi-site elements. + + + +For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, +and this is set to `['c', 'b', 'a']`, then Foo will be returned for Site C, and Bar will be returned +for Site B. + +If this isn’t set, then preference goes to the current site. + + + +::: code +```twig +{# Fetch unique entries from Site A, or Site B if they don’t exist in Site A #} +{% set entries = craft.entries() + .site('*') + .unique() + .preferSites(['a', 'b']) + .all() %} +``` + +```php +// Fetch unique entries from Site A, or Site B if they don’t exist in Site A +$entries = \craft\elements\Entry::find() + ->site('*') + ->unique() + ->preferSites(['a', 'b']) + ->all(); +``` +::: + + ### `prevSiblingOf` Narrows the query results to only the entry that comes immediately before another entry. @@ -985,6 +1156,128 @@ $entries = \craft\elements\Entry::find() ::: +### `revisionCreator` + +Narrows the query results to only revisions created by a given user. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch revisions by the current user #} +{% set entries = craft.entries() + .revisionCreator(currentUser) + .all() %} +``` + +```php +// Fetch revisions by the current user +$entries = \craft\elements\Entry::find() + ->revisionCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `revisionId` + +Narrows the query results based on the entries’ revision’s ID (from the `revisions` table). + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the revision with an ID of 1. + + + +::: code +```twig +{# Fetch a revision #} +{% set entries = craft.entries() + .revisionId(10) + .all() %} +``` + +```php +// Fetch a revision +$entries = \craft\elements\Entry::find() + ->revisionIf(10) + ->all(); +``` +::: + + +### `revisionOf` + +Narrows the query results to only revisions of a given entry. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the entry with an ID of 1. +| a [Entry](api:craft\elements\Entry) object | for the entry represented by the object. + + + +::: code +```twig +{# Fetch revisions of the entry #} +{% set entries = craft.entries() + .revisionOf(myEntry) + .all() %} +``` + +```php +// Fetch revisions of the entry +$entries = \craft\elements\Entry::find() + ->revisionOf($myEntry) + ->all(); +``` +::: + + +### `revisions` + +Narrows the query results to only revision entries. + + + + + +::: code +```twig +{# Fetch a revision entry #} +{% set entries = {twig-function} + .revisions() + .id(123) + .one() %} +``` + +```php +// Fetch a revision entry +$entries = \craft\elements\Entry::find() + ->revisions() + ->id(123) + ->one(); +``` +::: + + ### `search` Narrows the query results to only entries that match a search query. @@ -1135,7 +1428,7 @@ Possible values include: ::: tip If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you -only want unique elements to be returned, use `\craft\elements\db\unique()` in conjunction with this. +only want unique elements to be returned, use [unique](#unique) in conjunction with this. ::: @@ -1414,6 +1707,36 @@ $entry = \craft\elements\Entry::find() ::: +### `unique` + +Determines whether only elements with unique IDs should be returned by the query. + + + +This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not +desired. + + + +::: code +```twig +{# Fetch unique entries across all sites #} +{% set entries = craft.entries() + .site('*') + .unique() + .all() %} +``` + +```php +// Fetch unique entries across all sites +$entries = \craft\elements\Entry::find() + ->site('*') + ->unique() + ->all(); +``` +::: + + ### `uri` Narrows the query results based on the entries’ URIs. diff --git a/docs/dev/element-queries/global-set-queries.md b/docs/dev/element-queries/global-set-queries.md index 732d4124d00..0bfb9ae2ecf 100644 --- a/docs/dev/element-queries/global-set-queries.md +++ b/docs/dev/element-queries/global-set-queries.md @@ -177,6 +177,128 @@ $globalSets = \craft\elements\GlobalSet::find() ::: +### `draftCreator` + +Narrows the query results to only drafts created by a given user. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch drafts by the current user #} +{% set globalSets = craft.globalSets() + .draftCreator(currentUser) + .all() %} +``` + +```php +// Fetch drafts by the current user +$globalSets = \craft\elements\GlobalSet::find() + ->draftCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `draftId` + +Narrows the query results based on the global sets’ draft’s ID (from the `drafts` table). + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the draft with an ID of 1. + + + +::: code +```twig +{# Fetch a draft #} +{% set globalSets = craft.globalSets() + .draftId(10) + .all() %} +``` + +```php +// Fetch a draft +$globalSets = \craft\elements\GlobalSet::find() + ->draftIf(10) + ->all(); +``` +::: + + +### `draftOf` + +Narrows the query results to only drafts of a given global set. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the global set with an ID of 1. +| a [GlobalSet](api:craft\elements\GlobalSet) object | for the global set represented by the object. + + + +::: code +```twig +{# Fetch drafts of the global set #} +{% set globalSets = craft.globalSets() + .draftOf(myGlobalSet) + .all() %} +``` + +```php +// Fetch drafts of the global set +$globalSets = \craft\elements\GlobalSet::find() + ->draftOf($myGlobalSet) + ->all(); +``` +::: + + +### `drafts` + +Narrows the query results to only drafts global sets. + + + + + +::: code +```twig +{# Fetch a draft global set #} +{% set globalSets = {twig-function} + .drafts() + .id(123) + .one() %} +``` + +```php +// Fetch a draft global set +$globalSets = \craft\elements\GlobalSet::find() + ->drafts() + ->id(123) + ->one(); +``` +::: + + ### `fixedOrder` Causes the query results to be returned in the order specified by [id](#id). @@ -276,6 +398,20 @@ This can be combined with [fixedOrder](#fixedorder) if you want the results to b ::: +### `ignorePlaceholders` + +Causes the query to return matching global sets as they are stored in the database, ignoring matching placeholder +elements that were set by [craft\services\Elements::setPlaceholderElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-setplaceholderelement). + + + + + + + + + + ### `inReverse` Causes the query results to be returned in reverse order. @@ -370,6 +506,41 @@ $globalSets = \craft\elements\GlobalSet::find() ::: +### `preferSites` + +If [unique](#unique) is set, this determines which site should be selected when querying multi-site elements. + + + +For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, +and this is set to `['c', 'b', 'a']`, then Foo will be returned for Site C, and Bar will be returned +for Site B. + +If this isn’t set, then preference goes to the current site. + + + +::: code +```twig +{# Fetch unique global sets from Site A, or Site B if they don’t exist in Site A #} +{% set globalSets = craft.globalSets() + .site('*') + .unique() + .preferSites(['a', 'b']) + .all() %} +``` + +```php +// Fetch unique global sets from Site A, or Site B if they don’t exist in Site A +$globalSets = \craft\elements\GlobalSet::find() + ->site('*') + ->unique() + ->preferSites(['a', 'b']) + ->all(); +``` +::: + + ### `relatedTo` Narrows the query results to only global sets that are related to certain other elements. @@ -397,6 +568,128 @@ $globalSets = \craft\elements\GlobalSet::find() ::: +### `revisionCreator` + +Narrows the query results to only revisions created by a given user. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch revisions by the current user #} +{% set globalSets = craft.globalSets() + .revisionCreator(currentUser) + .all() %} +``` + +```php +// Fetch revisions by the current user +$globalSets = \craft\elements\GlobalSet::find() + ->revisionCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `revisionId` + +Narrows the query results based on the global sets’ revision’s ID (from the `revisions` table). + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the revision with an ID of 1. + + + +::: code +```twig +{# Fetch a revision #} +{% set globalSets = craft.globalSets() + .revisionId(10) + .all() %} +``` + +```php +// Fetch a revision +$globalSets = \craft\elements\GlobalSet::find() + ->revisionIf(10) + ->all(); +``` +::: + + +### `revisionOf` + +Narrows the query results to only revisions of a given global set. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the global set with an ID of 1. +| a [GlobalSet](api:craft\elements\GlobalSet) object | for the global set represented by the object. + + + +::: code +```twig +{# Fetch revisions of the global set #} +{% set globalSets = craft.globalSets() + .revisionOf(myGlobalSet) + .all() %} +``` + +```php +// Fetch revisions of the global set +$globalSets = \craft\elements\GlobalSet::find() + ->revisionOf($myGlobalSet) + ->all(); +``` +::: + + +### `revisions` + +Narrows the query results to only revision global sets. + + + + + +::: code +```twig +{# Fetch a revision global set #} +{% set globalSets = {twig-function} + .revisions() + .id(123) + .one() %} +``` + +```php +// Fetch a revision global set +$globalSets = \craft\elements\GlobalSet::find() + ->revisions() + ->id(123) + ->one(); +``` +::: + + ### `search` Narrows the query results to only global sets that match a search query. @@ -450,7 +743,7 @@ Possible values include: ::: tip If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you -only want unique elements to be returned, use `\craft\elements\db\unique()` in conjunction with this. +only want unique elements to be returned, use [unique](#unique) in conjunction with this. ::: @@ -549,6 +842,36 @@ $globalSet = \craft\elements\GlobalSet::find() ::: +### `unique` + +Determines whether only elements with unique IDs should be returned by the query. + + + +This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not +desired. + + + +::: code +```twig +{# Fetch unique global sets across all sites #} +{% set globalSets = craft.globalSets() + .site('*') + .unique() + .all() %} +``` + +```php +// Fetch unique global sets across all sites +$globalSets = \craft\elements\GlobalSet::find() + ->site('*') + ->unique() + ->all(); +``` +::: + + ### `with` Causes the query to return matching global sets eager-loaded with related elements. diff --git a/docs/dev/element-queries/matrix-block-queries.md b/docs/dev/element-queries/matrix-block-queries.md index de769089d77..322c36e1e3a 100644 --- a/docs/dev/element-queries/matrix-block-queries.md +++ b/docs/dev/element-queries/matrix-block-queries.md @@ -180,6 +180,128 @@ $MatrixBlocks = \craft\elements\MatrixBlock::find() ::: +### `draftCreator` + +Narrows the query results to only drafts created by a given user. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch drafts by the current user #} +{% set MatrixBlocks = craft.matrixBlocks() + .draftCreator(currentUser) + .all() %} +``` + +```php +// Fetch drafts by the current user +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->draftCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `draftId` + +Narrows the query results based on the Matrix blocks’ draft’s ID (from the `drafts` table). + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the draft with an ID of 1. + + + +::: code +```twig +{# Fetch a draft #} +{% set MatrixBlocks = craft.matrixBlocks() + .draftId(10) + .all() %} +``` + +```php +// Fetch a draft +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->draftIf(10) + ->all(); +``` +::: + + +### `draftOf` + +Narrows the query results to only drafts of a given Matrix block. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the Matrix block with an ID of 1. +| a [MatrixBlock](api:craft\elements\MatrixBlock) object | for the Matrix block represented by the object. + + + +::: code +```twig +{# Fetch drafts of the Matrix block #} +{% set MatrixBlocks = craft.matrixBlocks() + .draftOf(myBlock) + .all() %} +``` + +```php +// Fetch drafts of the Matrix block +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->draftOf($myBlock) + ->all(); +``` +::: + + +### `drafts` + +Narrows the query results to only drafts Matrix blocks. + + + + + +::: code +```twig +{# Fetch a draft Matrix block #} +{% set MatrixBlocks = {twig-function} + .drafts() + .id(123) + .one() %} +``` + +```php +// Fetch a draft Matrix block +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->drafts() + ->id(123) + ->one(); +``` +::: + + ### `fieldId` Narrows the query results based on the field the Matrix blocks belong to, per the fields’ IDs. @@ -279,6 +401,20 @@ This can be combined with [fixedOrder](#fixedorder) if you want the results to b ::: +### `ignorePlaceholders` + +Causes the query to return matching Matrix blocks as they are stored in the database, ignoring matching placeholder +elements that were set by [craft\services\Elements::setPlaceholderElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-setplaceholderelement). + + + + + + + + + + ### `inReverse` Causes the query results to be returned in reverse order. @@ -428,6 +564,41 @@ $MatrixBlocks = \craft\elements\MatrixBlock::find() ::: +### `preferSites` + +If [unique](#unique) is set, this determines which site should be selected when querying multi-site elements. + + + +For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, +and this is set to `['c', 'b', 'a']`, then Foo will be returned for Site C, and Bar will be returned +for Site B. + +If this isn’t set, then preference goes to the current site. + + + +::: code +```twig +{# Fetch unique Matrix blocks from Site A, or Site B if they don’t exist in Site A #} +{% set MatrixBlocks = craft.matrixBlocks() + .site('*') + .unique() + .preferSites(['a', 'b']) + .all() %} +``` + +```php +// Fetch unique Matrix blocks from Site A, or Site B if they don’t exist in Site A +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->site('*') + ->unique() + ->preferSites(['a', 'b']) + ->all(); +``` +::: + + ### `relatedTo` Narrows the query results to only Matrix blocks that are related to certain other elements. @@ -455,6 +626,128 @@ $MatrixBlocks = \craft\elements\MatrixBlock::find() ::: +### `revisionCreator` + +Narrows the query results to only revisions created by a given user. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch revisions by the current user #} +{% set MatrixBlocks = craft.matrixBlocks() + .revisionCreator(currentUser) + .all() %} +``` + +```php +// Fetch revisions by the current user +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->revisionCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `revisionId` + +Narrows the query results based on the Matrix blocks’ revision’s ID (from the `revisions` table). + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the revision with an ID of 1. + + + +::: code +```twig +{# Fetch a revision #} +{% set MatrixBlocks = craft.matrixBlocks() + .revisionId(10) + .all() %} +``` + +```php +// Fetch a revision +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->revisionIf(10) + ->all(); +``` +::: + + +### `revisionOf` + +Narrows the query results to only revisions of a given Matrix block. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the Matrix block with an ID of 1. +| a [MatrixBlock](api:craft\elements\MatrixBlock) object | for the Matrix block represented by the object. + + + +::: code +```twig +{# Fetch revisions of the Matrix block #} +{% set MatrixBlocks = craft.matrixBlocks() + .revisionOf(myBlock) + .all() %} +``` + +```php +// Fetch revisions of the Matrix block +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->revisionOf($myBlock) + ->all(); +``` +::: + + +### `revisions` + +Narrows the query results to only revision Matrix blocks. + + + + + +::: code +```twig +{# Fetch a revision Matrix block #} +{% set MatrixBlocks = {twig-function} + .revisions() + .id(123) + .one() %} +``` + +```php +// Fetch a revision Matrix block +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->revisions() + ->id(123) + ->one(); +``` +::: + + ### `search` Narrows the query results to only Matrix blocks that match a search query. @@ -503,12 +796,12 @@ Possible values include: | `'foo'` | from the site with a handle of `foo`. | `['foo', 'bar']` | from a site with a handle of `foo` or `bar`. | `['not', 'foo', 'bar']` | not in a site with a handle of `foo` or `bar`. -| a [craft\models\Site](api:craft\models\Site) object | from the site represented by the object. +| a `\craft\elements\db\Site` object | from the site represented by the object. | `'*'` | from any site. ::: tip If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you -only want unique elements to be returned, use `\craft\elements\db\unique()` in conjunction with this. +only want unique elements to be returned, use [unique](#unique) in conjunction with this. ::: @@ -704,6 +997,36 @@ $MatrixBlock = \craft\elements\MatrixBlock::find() ::: +### `unique` + +Determines whether only elements with unique IDs should be returned by the query. + + + +This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not +desired. + + + +::: code +```twig +{# Fetch unique Matrix blocks across all sites #} +{% set MatrixBlocks = craft.matrixBlocks() + .site('*') + .unique() + .all() %} +``` + +```php +// Fetch unique Matrix blocks across all sites +$MatrixBlocks = \craft\elements\MatrixBlock::find() + ->site('*') + ->unique() + ->all(); +``` +::: + + ### `with` Causes the query to return matching Matrix blocks eager-loaded with related elements. diff --git a/docs/dev/element-queries/tag-queries.md b/docs/dev/element-queries/tag-queries.md index b809639a26d..8ae545e3a34 100644 --- a/docs/dev/element-queries/tag-queries.md +++ b/docs/dev/element-queries/tag-queries.md @@ -176,6 +176,128 @@ $tags = \craft\elements\Tag::find() ::: +### `draftCreator` + +Narrows the query results to only drafts created by a given user. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch drafts by the current user #} +{% set tags = craft.tags() + .draftCreator(currentUser) + .all() %} +``` + +```php +// Fetch drafts by the current user +$tags = \craft\elements\Tag::find() + ->draftCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `draftId` + +Narrows the query results based on the tags’ draft’s ID (from the `drafts` table). + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the draft with an ID of 1. + + + +::: code +```twig +{# Fetch a draft #} +{% set tags = craft.tags() + .draftId(10) + .all() %} +``` + +```php +// Fetch a draft +$tags = \craft\elements\Tag::find() + ->draftIf(10) + ->all(); +``` +::: + + +### `draftOf` + +Narrows the query results to only drafts of a given tag. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the tag with an ID of 1. +| a [Tag](api:craft\elements\Tag) object | for the tag represented by the object. + + + +::: code +```twig +{# Fetch drafts of the tag #} +{% set tags = craft.tags() + .draftOf(myTag) + .all() %} +``` + +```php +// Fetch drafts of the tag +$tags = \craft\elements\Tag::find() + ->draftOf($myTag) + ->all(); +``` +::: + + +### `drafts` + +Narrows the query results to only drafts tags. + + + + + +::: code +```twig +{# Fetch a draft tag #} +{% set tags = {twig-function} + .drafts() + .id(123) + .one() %} +``` + +```php +// Fetch a draft tag +$tags = \craft\elements\Tag::find() + ->drafts() + ->id(123) + ->one(); +``` +::: + + ### `fixedOrder` Causes the query results to be returned in the order specified by [id](#id). @@ -308,6 +430,20 @@ This can be combined with [fixedOrder](#fixedorder) if you want the results to b ::: +### `ignorePlaceholders` + +Causes the query to return matching tags as they are stored in the database, ignoring matching placeholder +elements that were set by [craft\services\Elements::setPlaceholderElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-setplaceholderelement). + + + + + + + + + + ### `inReverse` Causes the query results to be returned in reverse order. @@ -402,6 +538,41 @@ $tags = \craft\elements\Tag::find() ::: +### `preferSites` + +If [unique](#unique) is set, this determines which site should be selected when querying multi-site elements. + + + +For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, +and this is set to `['c', 'b', 'a']`, then Foo will be returned for Site C, and Bar will be returned +for Site B. + +If this isn’t set, then preference goes to the current site. + + + +::: code +```twig +{# Fetch unique tags from Site A, or Site B if they don’t exist in Site A #} +{% set tags = craft.tags() + .site('*') + .unique() + .preferSites(['a', 'b']) + .all() %} +``` + +```php +// Fetch unique tags from Site A, or Site B if they don’t exist in Site A +$tags = \craft\elements\Tag::find() + ->site('*') + ->unique() + ->preferSites(['a', 'b']) + ->all(); +``` +::: + + ### `relatedTo` Narrows the query results to only tags that are related to certain other elements. @@ -429,6 +600,128 @@ $tags = \craft\elements\Tag::find() ::: +### `revisionCreator` + +Narrows the query results to only revisions created by a given user. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | created by the user with an ID of 1. +| a `\craft\elements\db\User` object | by the user represented by the object. + + + +::: code +```twig +{# Fetch revisions by the current user #} +{% set tags = craft.tags() + .revisionCreator(currentUser) + .all() %} +``` + +```php +// Fetch revisions by the current user +$tags = \craft\elements\Tag::find() + ->revisionCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `revisionId` + +Narrows the query results based on the tags’ revision’s ID (from the `revisions` table). + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the revision with an ID of 1. + + + +::: code +```twig +{# Fetch a revision #} +{% set tags = craft.tags() + .revisionId(10) + .all() %} +``` + +```php +// Fetch a revision +$tags = \craft\elements\Tag::find() + ->revisionIf(10) + ->all(); +``` +::: + + +### `revisionOf` + +Narrows the query results to only revisions of a given tag. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the tag with an ID of 1. +| a [Tag](api:craft\elements\Tag) object | for the tag represented by the object. + + + +::: code +```twig +{# Fetch revisions of the tag #} +{% set tags = craft.tags() + .revisionOf(myTag) + .all() %} +``` + +```php +// Fetch revisions of the tag +$tags = \craft\elements\Tag::find() + ->revisionOf($myTag) + ->all(); +``` +::: + + +### `revisions` + +Narrows the query results to only revision tags. + + + + + +::: code +```twig +{# Fetch a revision tag #} +{% set tags = {twig-function} + .revisions() + .id(123) + .one() %} +``` + +```php +// Fetch a revision tag +$tags = \craft\elements\Tag::find() + ->revisions() + ->id(123) + ->one(); +``` +::: + + ### `search` Narrows the query results to only tags that match a search query. @@ -482,7 +775,7 @@ Possible values include: ::: tip If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you -only want unique elements to be returned, use `\craft\elements\db\unique()` in conjunction with this. +only want unique elements to be returned, use [unique](#unique) in conjunction with this. ::: @@ -618,6 +911,36 @@ $tag = \craft\elements\Tag::find() ::: +### `unique` + +Determines whether only elements with unique IDs should be returned by the query. + + + +This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not +desired. + + + +::: code +```twig +{# Fetch unique tags across all sites #} +{% set tags = craft.tags() + .site('*') + .unique() + .all() %} +``` + +```php +// Fetch unique tags across all sites +$tags = \craft\elements\Tag::find() + ->site('*') + ->unique() + ->all(); +``` +::: + + ### `uri` Narrows the query results based on the tags’ URIs. diff --git a/docs/dev/element-queries/user-queries.md b/docs/dev/element-queries/user-queries.md index 30c5eb5a58f..ff5e8b0c13c 100644 --- a/docs/dev/element-queries/user-queries.md +++ b/docs/dev/element-queries/user-queries.md @@ -224,6 +224,128 @@ $elements = ElementClass::find() ::: +### `draftCreator` + +Narrows the query results to only drafts created by a given user. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | created by the user with an ID of 1. +| a [craft\elements\User](api:craft\elements\User) object | by the user represented by the object. + + + +::: code +```twig +{# Fetch drafts by the current user #} +{% set elements = craft.queryFunction() + .draftCreator(currentUser) + .all() %} +``` + +```php +// Fetch drafts by the current user +$elements = ElementClass::find() + ->draftCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `draftId` + +Narrows the query results based on the elements’ draft’s ID (from the `drafts` table). + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the draft with an ID of 1. + + + +::: code +```twig +{# Fetch a draft #} +{% set elements = craft.queryFunction() + .draftId(10) + .all() %} +``` + +```php +// Fetch a draft +$elements = ElementClass::find() + ->draftIf(10) + ->all(); +``` +::: + + +### `draftOf` + +Narrows the query results to only drafts of a given element. + + + +Possible values include: + +| Value | Fetches drafts… +| - | - +| `1` | for the element with an ID of 1. +| a ElementClass object | for the element represented by the object. + + + +::: code +```twig +{# Fetch drafts of the element #} +{% set elements = craft.queryFunction() + .draftOf(myElement) + .all() %} +``` + +```php +// Fetch drafts of the element +$elements = ElementClass::find() + ->draftOf($myElement) + ->all(); +``` +::: + + +### `drafts` + +Narrows the query results to only drafts elements. + + + + + +::: code +```twig +{# Fetch a draft element #} +{% set elements = {twig-function} + .drafts() + .id(123) + .one() %} +``` + +```php +// Fetch a draft element +$elements = ElementClass::find() + ->drafts() + ->id(123) + ->one(); +``` +::: + + ### `email` Narrows the query results based on the users’ email addresses. @@ -417,6 +539,20 @@ This can be combined with [fixedOrder](#fixedorder) if you want the results to b ::: +### `ignorePlaceholders` + +Causes the query to return matching elements as they are stored in the database, ignoring matching placeholder +elements that were set by [craft\services\Elements::setPlaceholderElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-setplaceholderelement). + + + + + + + + + + ### `inReverse` Causes the query results to be returned in reverse order. @@ -576,6 +712,41 @@ $elements = ElementClass::find() ::: +### `preferSites` + +If [unique()](https://docs.craftcms.com/api/v3/craft-elements-db-elementquery.html#method-unique) is set, this determines which site should be selected when querying multi-site elements. + + + +For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, +and this is set to `['c', 'b', 'a']`, then Foo will be returned for Site C, and Bar will be returned +for Site B. + +If this isn’t set, then preference goes to the current site. + + + +::: code +```twig +{# Fetch unique elements from Site A, or Site B if they don’t exist in Site A #} +{% set elements = craft.queryFunction() + .site('*') + .unique() + .preferSites(['a', 'b']) + .all() %} +``` + +```php +// Fetch unique elements from Site A, or Site B if they don’t exist in Site A +$elements = ElementClass::find() + ->site('*') + ->unique() + ->preferSites(['a', 'b']) + ->all(); +``` +::: + + ### `relatedTo` Narrows the query results to only elements that are related to certain other elements. @@ -603,6 +774,128 @@ $elements = ElementClass::find() ::: +### `revisionCreator` + +Narrows the query results to only revisions created by a given user. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | created by the user with an ID of 1. +| a [craft\elements\User](api:craft\elements\User) object | by the user represented by the object. + + + +::: code +```twig +{# Fetch revisions by the current user #} +{% set elements = craft.queryFunction() + .revisionCreator(currentUser) + .all() %} +``` + +```php +// Fetch revisions by the current user +$elements = ElementClass::find() + ->revisionCreator(Craft::$app->user->identity) + ->all(); +``` +::: + + +### `revisionId` + +Narrows the query results based on the elements’ revision’s ID (from the `revisions` table). + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the revision with an ID of 1. + + + +::: code +```twig +{# Fetch a revision #} +{% set elements = craft.queryFunction() + .revisionId(10) + .all() %} +``` + +```php +// Fetch a revision +$elements = ElementClass::find() + ->revisionIf(10) + ->all(); +``` +::: + + +### `revisionOf` + +Narrows the query results to only revisions of a given element. + + + +Possible values include: + +| Value | Fetches revisions… +| - | - +| `1` | for the element with an ID of 1. +| a ElementClass object | for the element represented by the object. + + + +::: code +```twig +{# Fetch revisions of the element #} +{% set elements = craft.queryFunction() + .revisionOf(myElement) + .all() %} +``` + +```php +// Fetch revisions of the element +$elements = ElementClass::find() + ->revisionOf($myElement) + ->all(); +``` +::: + + +### `revisions` + +Narrows the query results to only revision elements. + + + + + +::: code +```twig +{# Fetch a revision element #} +{% set elements = {twig-function} + .revisions() + .id(123) + .one() %} +``` + +```php +// Fetch a revision element +$elements = ElementClass::find() + ->revisions() + ->id(123) + ->one(); +``` +::: + + ### `search` Narrows the query results to only elements that match a search query. diff --git a/src/behaviors/DraftBehavior.php b/src/behaviors/DraftBehavior.php index 1c529a91923..e0236094dcc 100644 --- a/src/behaviors/DraftBehavior.php +++ b/src/behaviors/DraftBehavior.php @@ -91,7 +91,8 @@ public function getSource() return null; } - return $this->owner::find() + $owner = $this->owner; + return $owner::find() ->id($this->sourceId) ->siteId($this->owner->siteId) ->anyStatus() diff --git a/src/behaviors/RevisionBehavior.php b/src/behaviors/RevisionBehavior.php index f160e2a2d97..a23e89abb55 100644 --- a/src/behaviors/RevisionBehavior.php +++ b/src/behaviors/RevisionBehavior.php @@ -74,7 +74,8 @@ public function handleDelete() */ public function getSource(): ElementInterface { - return $this->owner::find() + $owner = $this->owner; + return $owner::find() ->id($this->sourceId) ->siteId($this->owner->siteId) ->anyStatus() diff --git a/src/console/controllers/MailerController.php b/src/console/controllers/MailerController.php index 0b7bde912a3..dd2be2c47ea 100644 --- a/src/console/controllers/MailerController.php +++ b/src/console/controllers/MailerController.php @@ -103,8 +103,9 @@ public function actionTest(): int } // Otherwise we let the user decide.... + $transportType = $settingsModel->transportType; $transportAdapters = array_unique([ - $settingsModel->transportType::displayName() => $settingsModel->transportType, + $transportType::displayName() => $settingsModel->transportType, 'Smtp' => Smtp::class, 'Gmail' => Gmail::class, 'Sendmail' => Sendmail::class, diff --git a/src/controllers/ElementIndexesController.php b/src/controllers/ElementIndexesController.php index c6230c27db1..1b94c477911 100644 --- a/src/controllers/ElementIndexesController.php +++ b/src/controllers/ElementIndexesController.php @@ -370,7 +370,8 @@ protected function elementQuery(): ElementQueryInterface $collapsedElementIds = $request->getParam('collapsedElementIds'); if ($collapsedElementIds) { - $descendantQuery = (clone $query) + $descendantQuery = clone $query; + $descendantQuery ->offset(null) ->limit(null) ->orderBy(null) @@ -380,7 +381,8 @@ protected function elementQuery(): ElementQueryInterface // Get the actual elements /** @var Element[] $collapsedElements */ - $collapsedElements = (clone $descendantQuery) + $collapsedElementsQuery = clone $descendantQuery; + $collapsedElements = $collapsedElementsQuery ->id($collapsedElementIds) ->orderBy(['lft' => SORT_ASC]) ->all(); @@ -394,7 +396,8 @@ protected function elementQuery(): ElementQueryInterface continue; } - $elementDescendantIds = (clone $descendantQuery) + $elementDescendantsQuery = clone $descendantQuery; + $elementDescendantIds = $elementDescendantsQuery ->descendantOf($element) ->ids(); diff --git a/src/controllers/EntryRevisionsController.php b/src/controllers/EntryRevisionsController.php index 48db8084531..3dc19dd8ab7 100644 --- a/src/controllers/EntryRevisionsController.php +++ b/src/controllers/EntryRevisionsController.php @@ -336,7 +336,7 @@ public function actionPublishDraft() /** @var Entry|null $entry */ $entry = ElementHelper::sourceElement($draft); $this->enforceEditEntryPermissions($entry); - $section = ($entry)->getSection(); + $section = $entry->getSection(); // Is this another user's entry (and it's not a Single)? $userId = Craft::$app->getUser()->getId(); diff --git a/src/elements/actions/SetStatus.php b/src/elements/actions/SetStatus.php index 6f149db8122..a0c736b2f3a 100644 --- a/src/elements/actions/SetStatus.php +++ b/src/elements/actions/SetStatus.php @@ -50,7 +50,8 @@ public function init() { // Only allow the "Disabled for site" option if there are multiple sites and the element type is localized if ($this->allowDisabledForSite) { - $this->allowDisabledForSite = $this->elementType && $this->elementType::isLocalized() && Craft::$app->getIsMultiSite(); + $elementType = $this->elementType; + $this->allowDisabledForSite = $elementType && $elementType::isLocalized() && Craft::$app->getIsMultiSite(); } parent::init(); diff --git a/src/elements/db/ElementQuery.php b/src/elements/db/ElementQuery.php index ed65afdeb83..da30a74aad9 100644 --- a/src/elements/db/ElementQuery.php +++ b/src/elements/db/ElementQuery.php @@ -2483,7 +2483,8 @@ private function _applySearchParam(Connection $db) if ($this->search) { // Get the element IDs - $elementIds = (clone $this) + $elementIdsQuery = clone $this; + $elementIds = $elementIdsQuery ->search(null) ->offset(null) ->limit(null) @@ -2717,7 +2718,8 @@ private function _applyUniqueParam(Connection $db) } $caseSql .= ' else ' . count($preferSites) . ' end'; - $subSelectSql = (clone $this->subQuery) + $subSelectSqlQuery = clone $this->subQuery; + $subSelectSql = $subSelectSqlQuery ->select(['elements_sites.id']) ->andWhere('[[subElements.id]] = [[tmpElements.id]]') ->orderBy([ diff --git a/src/fields/BaseRelationField.php b/src/fields/BaseRelationField.php index c9ad0e9c8cc..2be13af6972 100644 --- a/src/fields/BaseRelationField.php +++ b/src/fields/BaseRelationField.php @@ -959,7 +959,8 @@ protected function availableSources(): array */ private function _all(ElementQueryInterface $query): ElementQueryInterface { - return (clone $query) + $clone = clone $query; + return $clone ->anyStatus(); } } diff --git a/src/fields/Matrix.php b/src/fields/Matrix.php index aa5d305cbe0..57e21cd568a 100644 --- a/src/fields/Matrix.php +++ b/src/fields/Matrix.php @@ -699,25 +699,14 @@ public function getSearchKeywords($value, ElementInterface $element): string $contentService = Craft::$app->getContent(); foreach ($value->all() as $block) { - $originalContentTable = $contentService->contentTable; - $originalFieldColumnPrefix = $contentService->fieldColumnPrefix; - $originalFieldContext = $contentService->fieldContext; - - $contentService->contentTable = $block->getContentTable(); - $contentService->fieldColumnPrefix = $block->getFieldColumnPrefix(); - $contentService->fieldContext = $block->getFieldContext(); - - foreach (Craft::$app->getFields()->getAllFields() as $field) { + $fields = Craft::$app->getFields()->getAllFields($block->getFieldContext()); + foreach ($fields as $field) { /** @var Field $field */ if ($field->searchable) { $fieldValue = $block->getFieldValue($field->handle); $keywords[] = $field->getSearchKeywords($fieldValue, $element); } } - - $contentService->contentTable = $originalContentTable; - $contentService->fieldColumnPrefix = $originalFieldColumnPrefix; - $contentService->fieldContext = $originalFieldContext; } return parent::getSearchKeywords($keywords, $element); @@ -984,7 +973,7 @@ public function afterElementRestore(ElementInterface $element) parent::afterElementRestore($element); } - + // Private Methods // ========================================================================= diff --git a/src/helpers/ChartHelper.php b/src/helpers/ChartHelper.php index dd9e42abfaf..cf710a1e246 100644 --- a/src/helpers/ChartHelper.php +++ b/src/helpers/ChartHelper.php @@ -90,7 +90,8 @@ public static function getRunChartDataFromQuery(Query $query, DateTime $startDat while ($cursorDate->getTimestamp() < $endTimestamp) { $cursorEndDate = clone $cursorDate; $cursorEndDate->modify('+1 ' . $intervalUnit); - $total = (float)(clone $query) + $totalQuery = clone $query; + $total = (float)$totalQuery ->andWhere(['>=', $dateColumn, Db::prepareDateForDb($cursorDate)]) ->andWhere(['<', $dateColumn, Db::prepareDateForDb($cursorEndDate)]) ->$func($q); diff --git a/src/helpers/Template.php b/src/helpers/Template.php index 696c00df1d5..4e99d30255e 100644 --- a/src/helpers/Template.php +++ b/src/helpers/Template.php @@ -121,7 +121,8 @@ public static function attribute(Environment $env, Source $source, $object, $ite public static function paginateCriteria(QueryInterface $query): array { /** @var Query $query */ - $paginator = new Paginator((clone $query)->limit(null), [ + $paginatorQuery = clone $query; + $paginator = new Paginator($paginatorQuery->limit(null), [ 'currentPage' => Craft::$app->getRequest()->getPageNum(), 'pageSize' => $query->limit ?: 100, ]); diff --git a/src/queue/jobs/PropagateElements.php b/src/queue/jobs/PropagateElements.php index f9f79d2bea7..4b7c9f0ad15 100644 --- a/src/queue/jobs/PropagateElements.php +++ b/src/queue/jobs/PropagateElements.php @@ -101,7 +101,8 @@ protected function defaultDescription(): string */ private function _query(): ElementQueryInterface { - $query = $this->elementType::find(); + $elementType = $this->elementType; + $query = $elementType::find(); if (!empty($this->criteria)) { Craft::configure($query, $this->criteria); diff --git a/src/queue/jobs/ResaveElements.php b/src/queue/jobs/ResaveElements.php index 9b151fa66e8..31246c54885 100644 --- a/src/queue/jobs/ResaveElements.php +++ b/src/queue/jobs/ResaveElements.php @@ -93,7 +93,8 @@ protected function defaultDescription(): string */ private function _query(): ElementQueryInterface { - $query = $this->elementType::find(); + $elementType = $this->elementType; + $query = $elementType::find(); if (!empty($this->criteria)) { Craft::configure($query, $this->criteria); diff --git a/src/services/Images.php b/src/services/Images.php index 943b3d0af97..329767f6f57 100644 --- a/src/services/Images.php +++ b/src/services/Images.php @@ -147,7 +147,7 @@ public function getImageMagickApiVersion(): string // Taken from Imagick\Imagine() constructor. // Imagick::getVersion() is static only since Imagick PECL extension 3.2.0b1, so instantiate it. /** @noinspection PhpStaticAsDynamicMethodCallInspection */ - $versionString = (new \Imagick)::getVersion()['versionString']; + $versionString = \Imagick::getVersion()['versionString']; list($this->_imagickVersion) = sscanf($versionString, 'ImageMagick %s %04d-%02d-%02d %s %s'); return $this->_imagickVersion; diff --git a/src/services/Matrix.php b/src/services/Matrix.php index 05fb705db1e..dd3953a6e37 100644 --- a/src/services/Matrix.php +++ b/src/services/Matrix.php @@ -748,7 +748,10 @@ public function saveField(MatrixField $field, ElementInterface $owner) /** @var MatrixBlockQuery $query */ $query = $owner->getFieldValue($field->handle); /** @var MatrixBlock[] $blocks */ - $blocks = $query->getCachedResult() ?? (clone $query)->anyStatus()->all(); + if (($blocks = $query->getCachedResult()) === null) { + $blocksQuery = clone $query; + $blocks = $blocksQuery->anyStatus()->all(); + } $blockIds = []; $collapsedBlockIds = []; @@ -797,7 +800,8 @@ public function saveField(MatrixField $field, ElementInterface $owner) // Duplicate Matrix blocks, ensuring we don't process the same blocks more than once $handledSiteIds = []; - $cachedQuery = (clone $query)->anyStatus(); + $cachedQuery = clone $query; + $cachedQuery->anyStatus(); $cachedQuery->setCachedResult($blocks); $owner->setFieldValue($field->handle, $cachedQuery); @@ -851,7 +855,10 @@ public function duplicateBlocks(MatrixField $field, ElementInterface $source, El /** @var MatrixBlockQuery $query */ $query = $source->getFieldValue($field->handle); /** @var MatrixBlock[] $blocks */ - $blocks = $query->getCachedResult() ?? (clone $query)->anyStatus()->all(); + if (($blocks = $query->getCachedResult()) === null) { + $blocksQuery = clone $query; + $blocks = $blocksQuery->anyStatus()->all(); + } $newBlockIds = []; $transaction = Craft::$app->getDb()->beginTransaction(); diff --git a/src/test/Fixture.php b/src/test/Fixture.php index f59e358cca8..74fec4489c7 100644 --- a/src/test/Fixture.php +++ b/src/test/Fixture.php @@ -77,8 +77,9 @@ public function load() */ public function unload() { + $modelClass = $this->modelClass; foreach ($this->ids as $id) { - $arInstance = $this->modelClass::find() + $arInstance = $modelClass::find() ->where(['id' => $id]) ->one(); diff --git a/src/test/console/CommandTest.php b/src/test/console/CommandTest.php index d58cb75b6a2..331d4e8caae 100644 --- a/src/test/console/CommandTest.php +++ b/src/test/console/CommandTest.php @@ -126,10 +126,9 @@ public function run() } $exitCode = $this->controller->run($this->actionId, $this->parameters); - - $this->test::assertSame($this->desiredExitCode, $exitCode); - - $this->test::assertCount($this->eventChainItemsHandled, $this->eventChain); + $test = $this->test; + $test::assertSame($this->desiredExitCode, $exitCode); + $test::assertCount($this->eventChainItemsHandled, $this->eventChain); } /** @@ -270,12 +269,12 @@ protected function outputCommandHandler(): Closure { return function($out, $withScriptName = true) { $nextItem = $this->runHandlerCheck($out, self::OUTPUT_COMMAND); - - $this->test::assertSame( + $test = $this->test; + $test::assertSame( $nextItem->withScriptName, $withScriptName ); - $this->test::assertSame( + $test::assertSame( $nextItem->desiredOutput, $out ); @@ -290,7 +289,8 @@ protected function stdOutHandler(): Closure return function($out) { if (!$this->ignoreStdOut) { $nextItem = $this->runHandlerCheck($out, self::STD_OUT); - $this->test::assertSame($nextItem->desiredOutput, $out); + $test = $this->test; + $test::assertSame($nextItem->desiredOutput, $out); } }; } @@ -302,8 +302,8 @@ protected function stderrHandler(): Closure { return function($out) { $nextItem = $this->runHandlerCheck($out, self::STD_ERR); - - $this->test::assertSame( + $test = $this->test; + $test::assertSame( $nextItem->desiredOutput, $out ); @@ -317,17 +317,15 @@ protected function promptHandler(): Closure { return function($text, $options = []) { $nextItem = $this->runHandlerCheck('A prompt with value: ' . $text, self::PROMPT); - - $this->test::assertSame( + $test = $this->test; + $test::assertSame( $nextItem->prompt, $text ); - - $this->test::assertSame( + $test::assertSame( $nextItem->options, $options ); - return $nextItem->returnValue; }; } @@ -339,17 +337,15 @@ protected function confirmHandler(): Closure { return function($message, $default = false) { $nextItem = $this->runHandlerCheck('A confirm with value: ' . $message, self::CONFIRM); - - $this->test::assertSame( + $test = $this->test; + $test::assertSame( $nextItem->message, $message ); - - $this->test::assertSame( + $test::assertSame( $nextItem->default, $default ); - return $nextItem->returnValue; }; } @@ -361,17 +357,15 @@ protected function selectHandler(): Closure { return function($prompt, $options = []) { $nextItem = $this->runHandlerCheck('A select with value: ' . $prompt, self::SELECT); - - $this->test::assertSame( + $test = $this->test; + $test::assertSame( $nextItem->prompt, $prompt ); - - $this->test::assertSame( + $test::assertSame( $nextItem->options, $options ); - return $nextItem->returnValue; }; } @@ -385,17 +379,14 @@ protected function selectHandler(): Closure protected function runHandlerCheck($out, $type): CommandTestItem { $nextItem = $this->getNextItem(); - + $test = $this->test; if (!$nextItem) { - $this->test::fail("There are no more items however: $out was printed"); + $test::fail("There are no more items however: $out was printed"); } - if ($nextItem->type !== $type) { - $this->test::fail("A $type message was expected but $nextItem->type was given"); + $test::fail("A $type message was expected but $nextItem->type was given"); } - $this->eventChainItemsHandled++; - return $nextItem; } diff --git a/src/test/fixtures/elements/ElementFixture.php b/src/test/fixtures/elements/ElementFixture.php index 35e58e9dad9..0092349b5a2 100644 --- a/src/test/fixtures/elements/ElementFixture.php +++ b/src/test/fixtures/elements/ElementFixture.php @@ -171,7 +171,8 @@ public function getElement(array $data = null) */ public function generateElementQuery(array $data) : ElementQuery { - $query = $this->modelClass::find()->anyStatus()->trashed(null); + $modelClass = $this->modelClass; + $query = $modelClass::find()->anyStatus()->trashed(null); foreach ($data as $key => $value) { if ($this->isPrimaryKey($key)) { diff --git a/src/web/assets/assetindexes/dist/AssetIndexesUtility.js b/src/web/assets/assetindexes/dist/AssetIndexesUtility.js index f4a9bbe6024..86d1b7c0a35 100644 --- a/src/web/assets/assetindexes/dist/AssetIndexesUtility.js +++ b/src/web/assets/assetindexes/dist/AssetIndexesUtility.js @@ -129,8 +129,8 @@ $body = $('
').appendTo($modal).html(data.confirm), $footer = $('').appendTo($modal), $buttons = $('').appendTo($footer), - $cancelBtn = $('