relations (dependencies) between objects

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hey guys,

I've started building my own framework/cms, I did have one in the past
but this one should be much more object oriented in order to keep
things organised (and a bunch of other reasons).

One of the problems I ran into was dependency between objects. I have
to following tables in my database:

- users
- roles
- roles_to_users

where ofcourse roles_to_users is the connection between users and
roles (which is an n:m relation). For the tables I have a
representation using an object. Therefor I will have the following

- user
- role
- user_roles

The question which occurs is the following: how do I get rid of the
dependencies between the objects? I don't mind the dependency of the
user_roles object, since the purpose of the objects is to actually
make a relation with other two. But how would I implement such a thing
(dependency only in the connecting object)?

Furthermore, I also have some 1:n relations in my database (like the
connection between the tables: articles and users. A user can have
written multiple articles, but an article belongs to only one user.)
Now I wonder how would you implement such a thing in your classes
without to much dependency?

Anyone who can help me out with this?

Help is much appreciated!

Re: relations (dependencies) between objects

Yorian wrote:
Quoted text here. Click to load it

You don't, completely.

But why would you have a user_roles object?  It's a valid relational
object, but not useful as a business object.  Rather, user should
contain a list of roles valid for that user.  And, if necessary, a role
should contain a list of users.

Quoted text here. Click to load it

As above, don't try to apply relational patterns to business objects.
They have two entirely different purposes.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: relations (dependencies) between objects

Quoted text here. Click to load it

But shouldn't there be something like a data access layer which does
the roles part?

In my opinion a role is also represented as an object. But do you want
the roles to be totally independent of the user? I guess in this case
that would not be much of a problem (although it wouldn't be to bad to
make the dependency smaller). And what happens in the case of the
articles, do you also want to put al their information inside the user
object? Seems a bit too much dependency if you ask me...

Maybe I'm wrong, but isn't it better to keep objects as independent as

Re: relations (dependencies) between objects

Yorian wrote:
Quoted text here. Click to load it

You can do that, if you wish.  However, then you should isolate your
data access layer from the rest of your code with a business layer.
However, for something like this, I would consider rolling them both
into one layer.

The idea is to isolate your code from the actual storage of the data.

Quoted text here. Click to load it

The role can definitely be an object.  For instance, if roles have
different access privileges, each role would contain a list of the valid
access privileges for that role.

And yes, the roles should be independent of the user, but the users
"has" roles - which indicates the user object should contain a list of
roles valid for that user.

Additionally, a role may be assigned to multiple users, so the reverse
is true - the roles may should have a list of valid users.

However, you now run into a problem.  If you fetch a user object, you
also need to fetch the appropriate roles for that user.  And when you
fetch the roles, you would also end up fetching all the users for that
role.  Then all the roles for each user.  And so on.  You can keep it
from looping forever, but you'll have a lot of database overhead and
eventually end up with all roles and all users, which is definitely NOT
what you want.

The solution to this would be to not fetch the roles when you fetch a
user.  Now, if you ask for the valid roles for that user, you can check
to see if they are already fetched.  If not, fetch them.  The same would
be true for fetching users for the role object.

Quoted text here. Click to load it

Yes, but you're always going to have interactions between object.
That's what OO programming is all about.  You have a relationship
between the users and the roles, so you need to model that into your

Now you can say something like:

$user = new User('tom');  // Fetch Tom
if ($user->hasRole('admin')) [ // Is Tom an Admin user?


$role = new Role('admin');  // Fetch admin role
$admins = $role->getUsers(); // Fetch list of admins

Notice how in either case that, while the actual storage is in three
separate tables, your code doesn't have to worry about that.

You can even have something like:

if ($user->canEdit()) { // Does user have update privileges?

This would go out to the $user object and look at the list of roles
(fetching the list, if necessary).  The code would then go through each
role and query the $role->canEdit() method.  If the latter returns true,
$user->canEdit() can return true.  Else, if no role returns true,
$user->canEdit() returns false.

Does this help?

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline