to clarify what my problem is, I would like to use the Profile <-> User example from the CakePHP documentation. The relationships defined there are:
- Profile belongsTo User
- User hasOne Profile
In the database, the profiles tables has a filed user_id that is used to store the relationship.
In most examples, users set or edit this relationship in the Profile area. Meaning: In the Profile's edit view there is an input field that allows for setting another User for a Profile. It worked automagically by putting the following in the Profiles/edit view:
echo $this->Form->input('user_id');
A change in our rights managment demands us to change the way this relationship can be set. We have to get it out of the Profiles area and instead present it in the Users area.
When reading the data of a User record in the UsersController ($this->request->data = $this->User->read(null, $id);), CakePHP automagically binds the Profile data to the User data (if existent). However, as there is no field profile_id in the users table (because the relationship is stored "on the other side"), obviously echo $this->Form->input('profile_id')); does not work in the Users/edit view.
I already learned that one has to build a list of profiles first in the Controller's edit action:
$this->set('profiles', $this->User->Profile->find('list'));
And then let CakePHP create a select list with:
echo $this->Form->input('Profile');
However, there seems to be something missing. In our case when I look at the HTML output CakePHP creates, several option elements in the select list have the selected attribute set, although there is only one particular profile for a user, and that profile is stored in $this->data.
Somehow I think I have to set some further option(s) in order to get it to work (if there is a way to get it to work automagically – maybe there isn't).
I mean I could set the 'selected' attribute of the Form->input() call. As the name of the form element CakePHP creates is "data[Profile]" I am not sure if CakePHP will automagically save changes correctly, or if I have to do it manually. The steps would be:
- Look for the (former and still) current profile record having the user_id set to the id of the current user record and set that user_id to 0 (or NULL).
- Look for the profile with the id set in data[Profile] and set it's user_id to the id of the current user.
But do I have to do it myself, or did I just miss something to enable CakePHP to do it?
$this->Form->input('profile_id'))
try$this->Form->input('Profile.id'))
– arilia