nystudio107 / craft-similar

Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.
https://nystudio107.com/
MIT License
26 stars 5 forks source link

Criteria parameter isn't applied [Craft 4.4.9] #50

Closed croxton closed 4 months ago

croxton commented 1 year ago

Describe the bug

criteria doesn't seem to limit the resultset.

To reproduce

{% set ids = entry.relatedTopics.ids() %}
{% set limitCriteria = craft.entries.section(['awards']).limit(1) %}
{% set similarEntries = craft.similar.find({
   element: entry,
   context: ids,
   criteria: limitCriteria
}) %}

{# result: similarEntries has entries related to `ids` but contains entries in sections other than 'awards', and is not limited to 1 entry #}

Expected behaviour

criteria should be used as the base element query that Similar extends.

Versions

croxton commented 1 year ago

Interesting - this works as expected:


{% set ids = entry.relatedTopics.ids() %}
{% set similarEntries = craft.similar.find({
   element: entry,
   context: ids,
   criteria:  {
      limit : 1,
      section: 'awards'
   }
}) %}
khalwat commented 1 year ago

Hrm yeah not sure why that would be, but it sounds like we have a work-around?

I'll close this up, but if you want to look into why it wouldn't work for a query but would for an object, feel free. It'd be interesting to see what's going on.

croxton commented 1 year ago

Well that's the thing, I think you're doing the right thing here but it just doesn't seem to work anymore. Reasonably sure Similar was working with this pattern in earlier versions of Craft 4: https://github.com/nystudio107/craft-similar/blob/a8ce5c8cfa48c859d2b79e7d93a52b99799166af/src/services/Similar.php#L78

I'm happy with the workaround, but I guess this could cause issues for people upgrading.

khalwat commented 1 year ago

Yeah doesn't make sense to me. It's just converting it into an array. Maybe it contains some other properties that we causing an issue?

In any event, I've re-opened it. Lmk if you find anything more

khalwat commented 4 months ago

Will re-open if additional information is provided

khalwat commented 3 months ago

Figured it out while looking into an unrelated issue. Fixed in the following releases:

Craft 3: https://github.com/nystudio107/craft-similar/releases/tag/1.1.7

Craft 4: https://github.com/nystudio107/craft-similar/releases/tag/4.0.2

Craft 5: https://github.com/nystudio107/craft-similar/releases/tag/5.0.2