Jpsh
Jpsh

Reputation: 1726

Modeling a M-N Relationship in OOP

This is the closest thing I could find to my problem on here

I'm working on a projects and I want to add a security model to it, so since I have experience first hand how bad jumping into coding and skipping the planning phase is I decided to do that first. So I created an ERD, cool do it all the time, then the UML Class diagram, haven't done one of these since college, ok a little bit of google, go it.

See ERD And UML Class Diagram Here

The image above is an exert of what I have so far, I know that I certainly need a User Class and a Permission Class but I'm not sure how to handle the relationship between the two. I know generally in an M-N relationship you model it with a property that is a collection of the related class but what about the properties of the related class? Below is my best guess, if anyone has corrections, comments, or links to material to read that would be awesome. My goal is the proper implementation of OOP thanks in advance.

class User{
    private $id;
    private $password;
    private $active;
    private $permissions;
    /* skip getters and setters */

    function getUserPermissions(){
        return UserPermission[];
    }

}

class UserPermission{
    private $id;
    private $deny;
    private $grant;
    private $active;
    /* skip getters and setters */

     function getPermissions(){
        return Permission[];
     }
}

class Permission{
    private $id;
    private $name;
    private $description;
    private $active;

    /* skip getters and setters */      
}

Upvotes: 1

Views: 137

Answers (1)

Gangnus
Gangnus

Reputation: 24464

  • You could have these arrays that you return directly, as arrays or lists. But that is not so important.

  • What is more important, every UserPermission, that is a class association, should have an array of User's and an array of Permission's.

  • Also, every User should have his UserPermission and every Permission should have its UserPermission, too.

  • And User should have no array for UserPermission's. Their association is 1:n, with n on the side of User. That means: UserPermission has many User's, User has 1 UserPermission.

Upvotes: 1

Related Questions