Jump to content

Favorite function


Frank Vèssia
 Share

Recommended Posts

Hi guys, just a simple logic question:
I'm going to create a list of favorites pictures for each user, using a page field.
my question is: 
it's better to add a page field to the user template and add all favorites pictures there, or add the page field to the picture template and add there the list of all users that added that picture? :)

Link to comment
Share on other sites

In the database it’s going to look the same either way: a table with two columns, user id and picture id. But since you probably want to show “this image has been favorited by 43 users”, rather than “this user has favorited 29 pictures”, I’d put it in the picture template. That way you can simply go $page->favs->count(), rather than $users->count("favs={$page->id}"). It would also be easier to select and sort pictures by number of favs.

Have you considered storing the time of the favorite? Maybe this module will be of interest: http://modules.processwire.com/modules/fieldtype-page-with-date/

Link to comment
Share on other sites

thanks for the reply, yes I'm gonna use that module, I already used in the past.
regarding the "display of favs" I will show both, the picture page will display all users that added that picture, and in the profile page each user will display the list of favorite pictures, I think the real questions is: there will be more users of pictures in the database? or it doesn't matter for the query?

option A (favs field inside user template)
user profile $user->favs->count()
picture page $users->count("favs={$page->id}") or find("favs={$page->id}")
 

option B (favs field inside picture template)
user profile $pages->find("template=picture,favs=$user")
picture page $page->favs->count()
 

Link to comment
Share on other sites

I would probably add it to the picture template. Main reason is that it may be easier in the future if you introduce a different page template that also needs favourite functionality.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...