Stillat / relationships

Provides automated bi-directional @statamic entry relationships
MIT License
18 stars 1 forks source link

FR: Relate entries to multiple collections #18

Closed mikemartin closed 1 year ago

mikemartin commented 1 year ago

This might be a bit of a reach, but I thought I'd ask.

I have multiple product collections on a site, that act as my product types. For example, Pens, Markers, Accessories.

The collections all share the same product blueprint and use the same Category taxonomy.

Would it be possible to add support for relating taxonomy terms to entries from multiple collections?

I would expect the config to look a little like this:

Relate::manyToMany(
 'term:categories:products',
['pens.categories','markers.categories','accessories.categories']
);

I really want to use entry relationships for this project cause we have a lot of entries, so if it's not possible than I'll have to restructure my collections into a single "products" collection.

Curious to hear what you think. Thanks mate! 🙏

JohnathonKoster commented 1 year ago

HI there! In order for this to work you will need each of the collections to have their own categories field pointing to the Categories taxonomy. However, you can already accomplish this using set notation:

<?php

use Stillat\Relationships\Support\Facades\Relate;

Relate::manyToMany(
    'term:categories.products',
    'entry:{pens,markers,accessories}.categories'
);

I corrected a null-reference issue when testing this locally, so you might need to update to the latest version (update released for both Statamic v3 and v4 versions) 🙂

mikemartin commented 1 year ago

@JohnathonKoster Thank you! That's absolutely amazing.

mikemartin commented 1 year ago

@JohnathonKoster Can this be added to the docs?

mikemartin commented 1 year ago

Hey @JohnathonKoster I tried this out tonight and ran into a strange issue where the second relationships leftCollection is having the first and last character removed.

Relate::manyToMany(
            'term:categories.products',
            'entry:{pens,markers}.categories'
        );
+---+--------------------+--------------------+-----------------+---------------+--------------------+--------------+----------------+-----------------------+
|   | Primary Collection | Related Collection | Primary Field   | Related Field | Relationship       | With Events? | Allow Deletes? | Is Automatic Inverse? |
+---+--------------------+--------------------+-----------------+---------------+--------------------+--------------+----------------+-----------------------+
| 1 | [term].[term]      | [term].products    | [entry].pens    | categories    | Many to Many (*-*) | No           | Yes            | No                    |
| 2 | [entry].pens       | [entry].categories | [term].ategorie | products      | Many to Many (*-*) | No           | Yes            | Yes (1)               |
| 3 | [term].[term]      | [term].products    | [entry].markers | categories    | Many to Many (*-*) | No           | Yes            | No                    |
| 4 | [entry].markers    | [entry].categories | [term].ategorie | products      | Many to Many (*-*) | No           | Yes            | Yes (3)               |
+---+--------------------+--------------------+-----------------+---------------+--------------------+--------------+----------------+-----------------------+

From ray:


1 => Stillat\Relationships\EntryRelationship {#796 ▼
        +leftType: "entry"
        +rightType: "term"
        +index: 2
        +type: 1
        +leftCollection: "pens"
        +rightCollection: "ategorie"
        +leftField: "categories"
        +rightField: "products"
        +inverseIndex: 1
        +isAutomaticInverse: true
        +withEvents: false
        +allowDelete: true
      }
mikemartin commented 1 year ago

@JohnathonKoster As a workaround, it seems to work as expected when I also put the term in set notation:

Relate::manyToMany(
            'term:{categories}.products',
            'entry:{pens,markers}.categories'
        )
JohnathonKoster commented 1 year ago

@JohnathonKoster As a workaround, it seems to work as expected when I also put the term in set notation:

Relate::manyToMany(
            'term:{categories}.products',
            'entry:{pens,markers}.categories'
        )

Your original syntax will now work with v2.0.2 (Statamic 4) and v1.2.3 (Statamic 3). The docs have also been updated with a reference to this syntax 🙂