0
votes

I'm currently developing a project using CakePHP where users can create projects. Each project has only one administrator user, but other users should be able to "view" the project. How should I create this "view" option to users? I've already done the following model associations: [Users Model] hasMany [Projects] (by the time each user can have many projects).

I'm kinda perplexed now and I don't know exactly what additional model association i should create :/ I think a belongsTo or a HasAndBelongsToMany but i'm not sure :/ Can you help me out here?

thank you in advance!

1
you can add new field is_admin in Users to make a difference between administrator user and normal usersMoyed Ansari
Can any user view any project or is there another piece of logic that limits which projects a user can view?AgRizzo
this way, any user can view any project (and just have a difference between admin and normal user). What I need is each project to be viewable from it's admin and it's members (not any other user in the platform). {Till now, only admins can view their own projects, and of course not other's admins' projects}Kostas Livieratos
Your data model is missing a piece - the part regarding projects having members. Can a user be a member of more than 1 project?AgRizzo

1 Answers

1
votes

You need to have 3 database tables

  1. users
  2. projects - This table needs to have a column (administrator) which contains the user_id representing the user who is the administrator
  3. usersprojects - This table is cross reference table between the users and projects representing the members of a project

CakePHP Models

User

public $useTable = 'users';
public $hasMany = array(
    'ProjectAsAdmin' => array(
       'className' => 'Project')
  );
public $hasAndBelongsToMany = array(
    'ProjectAsMember' => array(
       'className' => 'Project')
  );

Project

public $useTable = 'projects';
public $hasAndBelongsToMany = array(
    'Member' => array(
       'className' => 'User')
 );
public $belongsTo = array(
    'Administrator' => array(
       'className' => 'User',
       'foreignKey' => 'administrator'
     )
  );