patricio t Tue May 14 21:28:43 -0400 2013

Subject: Associations problem

I am working on a book collectors website, and I am having trouble with the relationships.

A user can have many books, and a book can have many authors (for example, if it is a recopilation).

My tables (simplified) are:

users:
id
name

books:
id
user_id
title

authors
id
name
book_id

Right now, I can fetch all the books uploaded by an user and I can fetch all the authors from a book.

I would like to get all the authors from an user. I can do it via SQL, but I would like to take advantage from relationships and do something like:
$user = User::find(1)
$user->authors
That would be great (if it would have DISTINCT), and things like count how many authors an user has (with Distinct), and how many books from such author by an user, etc.

I can't find a simple way to do it with PHP Actiev Record, and I don't want to write the SQL like I am doing in the current version of the website (what is the idea to move to a ORM, then).

Thank you!