9 votes
Accepted

Craft 3 - Save new Entry with Relations (Categories/Assets)

This should be as simple as setting the relation field (category, asset, etc.) value on the entry as an array of related element IDs before saving. $entry->setFieldValue('categoryFieldHandle', [$...
Ben Croker's user avatar
  • 7,401
7 votes

"Eager loading" of relations like Yii's "with" function

This feature has now made it into Craft core as of version 2.6. See this link for documentation on the subject: https://craftcms.com/docs/templating/eager-loading-elements
naboovalley's user avatar
  • 2,844
7 votes
Accepted

Filtering multiple categories with URL queries

The issue with your current code is that you're looping over each type/size category, creating a parameter in your relatedTo query for each one. This means that you're currently creating a query that ...
Mats Mikkel Rummelhoff's user avatar
6 votes
Accepted

Many db requests with relationship fields

This is how relationships work in Craft at the moment. Each relation equals +1 query. If you don't access your relation on an entry the query isn't executed. So by only fetching title and/or id you ...
naboovalley's user avatar
  • 2,844
5 votes
Accepted

.relatedTo() not returning tags with spaces

In your working example, you’re mapping request.tags to the search param, but in the non-working example, you’re mapping it to the slug param. Those two params are handled in very different ways. ...
Brandon Kelly's user avatar
5 votes
Accepted

Foreign Key column pointing to `craft_relations` table

I might be wrong, but I don't believe you'll be able to do that within the defineRelations() method – Craft needs a record class for the table you want to create a relation with. A workaround is to ...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

Combining Multiple Relation

You might have seen Passing Multiple Relation Criteria in the docs {% set entries = craft.entries.section('flightcompany').relatedTo([ 'and', { targetElement: models, field: 'partsModels....
Robin Schambach's user avatar
4 votes

Create new entry from related Entry field

Not sure when this was added, but this is now available in Craft core. The select modal has a button to create a new entry of that type.
Marion Newlevant's user avatar
4 votes
Accepted

Get random entries, excluding an array of entry IDs, where a relational field is not empty

That seems to be a bug / limitation of the :notempty: statement, it's probably adding its own ID criteria in the background and the merge doesn't work as expected. I'd suggest you file a bug report. ...
carlcs's user avatar
  • 36.2k
4 votes
Accepted

How to remove relationships between entries

A relationship field expects an array of element IDs. So to remove a user, you'd just get all the current IDs and remove the one you want to delete. // Get the current member IDs $members = $entry-&...
carlcs's user avatar
  • 36.2k
4 votes
Accepted

Prettiest way to loop through nested relations?

Would this work? {% set teams = craft.entries.section('team').relatedTo(studios) %} <h2>All teams in {{entry.title}}</h2> {% for team in teams %} <h3>{{team.title}}</h3> {...
Jamie Pittock's user avatar
4 votes
Accepted

Loop through entries that share current entry category and restrict to Cat Group Id

You can make use of 'not' to exclude items from your parameters. An example of how this would be implemented in the above use case would be as follows {% set categories = craft.categories.slug( 'not ...
Luke Pearce's user avatar
  • 3,863
4 votes
Accepted

Show all tags related to a category?

You can use the relatedTo param on craft.tags like you can with any Element inside Craft. All Elements inside Craft can be related to one another. There is a .relatedTo parameter available on all of ...
Luke Pearce's user avatar
  • 3,863
4 votes
Accepted

List only Orders containing specific Products

This is now possible as of Craft Commerce 1.1.1211 using the new hasPurchasables criteria attribute: https://craftcommerce.com/docs/craft-commerce-orders#haspurchasables
Luke Holder's user avatar
  • 6,827
4 votes
Accepted

How/Can does Craft handle ancestor/grandparent relationships?

As usual with Craft you have several options. If you didn't need the index pages, I would suggest a location category, which you organized into 3 levels: states, regions, counties. Selecting a county ...
Marion Newlevant's user avatar
4 votes
Accepted

Delete a relation in frontend form

I believe you're getting that error because you're supplying an array ([]) for the mother field and the only value in that array would be an empty string ("") if they select the There is no mother ...
Stephen Hamilton's user avatar
4 votes
Accepted

Editing entry in front-end

I managed to find a solution, using hidden input fields :) Thank you for all the help :) Edit: Ended up solving this by using tables and saving the username and ID in that. Used a plugin called Super ...
Filip Iversen's user avatar
4 votes
Accepted

Frontend form with relation to an entry

After research with lots of trail & error I found the solution. I just had to add [] to my hidden field to establish the relation: <input type="hidden" name="fields[assetId][]" value="1478" id=...
Marco's user avatar
  • 213
4 votes
Accepted

How do I query two relationship fields where one relationship MUST be empty?

Splitting the criteria up like this works. $criteria->relatedTo = [ 'targetElement' => anElementId, 'field' => 'relationshipOne' ]; $criteria->relationshipTwo = ':empty:'; ...
Benjamin Kohl's user avatar
4 votes
Accepted

Find entries from related section after certain date

You’d first get all the relevant entries of your “trainings” section, or better get just their IDs as that’s way better on performance. Then on another element query you set the relatedTo parameter to ...
carlcs's user avatar
  • 36.2k
4 votes

With a categories field, how do I query only the *selected* categories?

Here's how to isolate only your chosen categories. This works off the premise that if a related category is a parent of one of the other related categories, it is not a category you chose. {% set ...
Evan Warner's user avatar
4 votes
Accepted

Craft 4: using the new laravel collections and related methods (in a performant way)

Syntax for Laravel methods using arrow functions in twig ? You're not doing anything wrong, this approach is simply not supported by Twig. Arrow functions in Twig can only be used with built-in ...
MoritzLost's user avatar
  • 11.2k
3 votes

Relating a Category Entry to a Categories Field Type

Turns out I was overthinking it, I was able to connect it with the following piece of code. {% set levelOne = category.topLevelConnection %} {% for subCat in craft.categories.group('levelTwo')....
conormcafee's user avatar
3 votes
Accepted

Using the Search Filter with Entry Fields

{% set range = entry.model %} {% set categories = craft.categories.slug('not preferential') %} {% set entries = craft.entries.section('offers').search('title:' ~ range).relatedTo(categories) %} If ...
mcclaskiem's user avatar
3 votes

Querying relational data in plugin

I ended up doing it this way: $records = Plugin_CollectionSectionRecord::model() ->findAllByAttributes(['collectionId' => $collectionId]); } $ids = []; foreach($records as $value) { ...
sboucher's user avatar
3 votes

How to order by related entry type field

Try something slightly different in concept... On your Products template - /products/index.html - start with your Authors first and then look for the Products which are related to them. Hopefully ...
Matt P's user avatar
  • 1,538
3 votes

Working with multiple categories

I figured it out! If anyone is stuck at the same place here’s my solution: {# category is 'ford' #} {% set entries = craft.entries.section('support').relatedTo(category).find() %} {% set ...
Robin Sandborg's user avatar
3 votes

Get entries NOT related to categories

Another approach -- that might work for you -- is to filter on the output, rather than the query. For instance, using this query: {% set entries = craft.entries.section('news') %} Output using this ...
Brett Gullan's user avatar
3 votes

Inserting Relationship Into Entries Field - Plugin Not Saving Relation

You can also do it like this. This way is especially useful when creating a new entry with a required relationship field: public function createWorldConfig(UserModel $user, EntryModel $world) ...
Jay Haase's user avatar
  • 265
3 votes
Accepted

Get image from related entries asset field

Got a solution that is cleaner than nested for loops. {% set myRelatedImage = entry.relatedImages.first %} then I can do <img src='{{ myRelatedImage.images.first.url }}'> My use case was ...
Steven Grant's user avatar
  • 1,855

Only top scored, non community-wiki answers of a minimum length are eligible