1

In Twig, we can do this like this:

craft.entries({
    id: 11,
    relatedTo: 22,
})

But in PHP code, this does not work:

\craft\elements\Entry::find(
            [
                'id' => 11,
                'relatedTo' => 22,
            ],
        )

I think that only way would be loop through array and apply to initial query object each array position as element query method. Or maybe am I wrong?

I checked Craft source code and in file base/Element.php, in method find() there is no param to pass array of element query methods into.

2 Answers 2

2

The Element::find() method does not take any parameters, it just serves as the factory method for the appropriate element query for the given element type (for example, Entry::find() returns an EntryQuery). You can chain additional methods to the query afterwards:

use craft\elements\Entry;

$query = Entry::find()->id(11)->relatedTo(22);
$results = $query->all();

See Element Queries and Querying Entries for reference.

2

This is because it's a twig feature in Craft. If I remember correctly it's done in the CraftVariable and look something like this under the hood:

use Craft;
use craft\elements\Entry;

$query = Entry::find();
Craft::configure($query, $criteria);
return $query;

Where the $criteria is the "query config".

Edit: Some references for you.

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.