0
votes

Puling my hair out with this, I know its something very simple but I've been struggling to resolve it. For the record I'm using cakePHP 2.2.

I'm trying to populate a select input with related model data.

Basically I have users and user_statuses. The user model belongsTo the UserStatus model. Within the user/add() function I want to have a drop down select box populated with the data from the UserStatus model.

Heres the code:

UsersController/Add()

$groups = $this->User->Group->find('list');
$UserStatus = $this->User->UserStatus->find('list');
$this->set(compact('groups', 'UserStatus'));

View/Users/add.ctp

echo $this->Form->input('user_status_id', array('class' => 'span9'));

The field in the users table making the relationship is user_status_id which links to the id in the user_statuses table.

Based on the above the select box will NOT populate with the statuses from the user_statuses table. Interestingly enough, it will if I change the view code to just :

echo $this->Form->input('user_status_id', array('class' => 'span9'));

However, when I do this is will NOT write to the database.

Any help will be gratefully received.

Thanks in advance.

2

2 Answers

2
votes

The options param should solve the problems for display and as long as there is a user_status_id in your User table, the below code should save the value in your User model on save.

echo $this->Form->input('User.user_status_id', array('options'=>$UserStatus,'class' => 'span9'));
2
votes

Should be:

$userStatus = $this->User->UserStatus->find('list');

(note the lowercase 'u')