Question

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!

Was it helpful?

Solution

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'
     )
  );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top