I'm considering using CakePHP (2.x) for a new project I'm working on. The project would have users that have clients. There would be default fields on the client model like name, address, email, etc.. but my client would like users to be able to add custom fields to their clients.
So User1 might want to add a "favorite color" field to their clients and User2 might want to add a "favorite sport" field to their clients. (These are just examples)
Is there a way to do this with CakePHP? What is the best solution for adding fields to models after their controllers, models, and views have already been baked?
My first thought was to set up the following models
User ( id, username, password ) hasMany: Clients
Client ( id, first_name, last_name, user_id ) belongsTo: User hasMany:ClientFields
ClientFieldKey ( id, field_name, user_id ) belongsTo: User hasMany:ClientFieldValues
ClientFieldValue ( id, client_field_key_id, field_value ) belongsTo: ClientFieldKey
ClientField ( id, client_id, client_field_value_id ) belongsTo: Client, ClientFieldValue
then I could have something like the following
Client ( id: 1, username: user1, password: pass1, user_id: 1)
Client ( id: 2, username: user2, password: pass2, user_id: 1)
Client ( id: 3, username: user3, password: pass3, user_id: 2)
Client ( id: 4, username: user4, password: pass4, user_id: 2)
ClientFieldKey ( id: 1, field_name: Color, user_id: 1 )
ClientFieldKey ( id: 1, field_name: Sport, user_id: 2 )
ClientFieldValue ( id: 1, client_field_key_id: 1, field_value: red )
ClientFieldValue ( id: 2, client_field_key_id: 1, field_value: blue )
ClientFieldValue ( id: 3, client_field_key_id: 2, field_value: hockey )
ClientFieldValue ( id: 4, client_field_key_id: 2, field_value: football )
ClientField ( id: 1, client_id: 1, client_field_value_id: 2 )
ClientField ( id: 2, client_id: 2, client_field_value_id: 1 )
ClientField ( id: 3, client_id: 3, client_field_value_id: 3 )
ClientField ( id: 4, client_id: 4, client_field_value_id: 4 )
Which would mean that user 1 created client 1 who likes blue and client 2 who likes red. User 2 created client 3 who likes hockey and client 4 likes football.
Would a model structure like this work in CakePHP? If so, how would I add the fields to the client add and edit forms dynamically and what should I call the tables?
client fields
andclient field values
be enough, whereClient
has manyClientField
andClientField
has manyClientFieldValue
, assuming that a client is exclusive to a user? And what exactly do you mean with "add the fields to the client add and edit forms dynamically"? Do you want to be able to add new fields dynamically in the client add/edit forms (ie adding inputs to a form via JavaScript), or are you talking about generating forms in the view using the existing client fields? – ndm