1

I am using the Dukt Follow plugin to return 2 arrays which both hold User objects, I am then using the without filter to remove users from the array which are considered friends. Friends are determined by using the intersect filter on the 2 arrays.

Everything works apart from the second without filter which does not work, and returns objects that are in the friends array.

{# Friends, Followers & Following #}
{% set userFollowing = craft.follow.getFollowing(user.id) %}
{% set userFollowers = craft.follow.getFollowers(user.id) %}

{% set friends = userFollowing|length and userFollowers|length ? userFollowing|intersect(userFollowers) : null %}
{% set following = userFollowing|without(friends) %}
{% set followers = userFollowers|without(friends) %}

Is there an obvious reason why this might not be working? How is best to debug something like this?

I have checked the output of all the arrays and objects and they do appear to hold the data that they should.

Basic data stored in each array:

User Following:
Array: ["Craft\\UserModel: bencallaway","Craft\\UserModel: davidwearn","Craft\\UserModel: shorn"]

User Followers:
Array: ["Craft\\UserModel: bencallaway","Craft\\UserModel: davidwearn"]

Friends:
Array: ["Craft\\UserModel: bencallaway","Craft\\UserModel: davidwearn"]

Following:
Array: {"2":"Craft\\UserModel: shorn"}

Followers:
Array: ["Craft\\UserModel: bencallaway","Craft\\UserModel: davidwearn"]

Full variable output can be accessed here: http://cl.ly/2o292X3O2r0M

  • Cannot really reproduce this. Would be helpful if you could post the data inside friends, userFollowing and userFollowers. – Moin Qidwai Feb 16 '15 at 10:43
  • A bit confused — the output data that you posted contains 'userFollowing', 'userFollowers' (neither of which intersect btw), and 'friends' — not 'following', 'followers', and 'friends' as I would expect. Keep in mind that the without filter is returning a copy of the array — not modifying the original. In principle, this should work though. You might have something else going on with your data — I would check the values again carefully. – Douglas McDonald Feb 16 '15 at 16:52
  • The data provided contains what was requested, can you confirm what you mean by 'neither of which intersect btw'? The friends array does return the required users which are in both arrays hence following each other? – Sam Hibberd Feb 16 '15 at 17:33
  • @DouglasMcDonald I have added more debug data, does this help explain the issue any better? – Sam Hibberd Feb 16 '15 at 17:45
1

Try using |without based on arrays of user IDs, not entire UserModel objects. Behind the scenes, it's just using PHP's in_array method, which works much better on simple primitives like ints, strings, etc. instead of complex objects like Models (that has magic methods, other objects, primitives, etc.). in_array probably doesn't know how deep of a comparison to make on those objects.

| improve this answer | |
  • Ok I will try that, strange that it's working on one but not the other array (both of which contain the same data). – Sam Hibberd Feb 16 '15 at 20:55
1

Hey Sam the answer is in your own dump file. Just look at the userModel Numbers in userFollowing, userFollowers and friends.

UserFollowing has [379, 381, 393], UserFollowers has [446, 454] and friends has [379, 381]

Friends has the same numbers as userFollowing but not the same as userFollowers. Hence when you do the without filter on userFollowers you are basically doing [446, 454]|without([379, 381]) Which will ofcourse return [446, 454].

You could ofcourse get the username out for each model and perform the operations on that and since those would be strings this would work.

| improve this answer | |
  • Ah, exactly what I've been trying to get my head around. So do model numbers not relate to the user object? – Sam Hibberd Feb 17 '15 at 7:23
  • You are correct. There is no connection between model number and user model. The same user can have different model numbers at different times because each time a new model will be created for that user. You can even try this to see the model number change on each refresh: {{ dump(craft.users.first()) }} – Moin Qidwai Feb 17 '15 at 8:59

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.