1

In UsersController:

$query = $this->User->query("SELECT name FROM Type");
$this->set('type',$query);

In view.ctp:

echo $this->Form->input('Type_id',array('options'=>$type));

This adds Admin,Type,1 and User, Type to dropdown.

I want to display only Admin and User in Dropdown.

Type Table contains only Two Columns id and type.

Thanks in advance.

4
  • what $query will give you here ? Commented Nov 11, 2013 at 6:34
  • $query contains result of "SELECT name FROM Type" Commented Nov 11, 2013 at 6:36
  • using json_encode, [{"Type":{"name":"Admin"}},{"Type":{"name":"User"}}] Commented Nov 11, 2013 at 6:39
  • Is Type a table here? and is it associated with User Model ? Commented Nov 11, 2013 at 6:40

2 Answers 2

1

Try with this

$query = $this->User->query("SELECT id,name FROM Type");    
$query = Hash::combine($query, '{n}.Type.id', '{n}.Type.name');
$this->set('type',$query);

OR

You can also do the above thing if there is a association between the User and Type Models

i.e.

$types = $this->User->Type->find('list', array('fields' => array('id', 'name')));

and in view.ctp

echo $this->Form->input('type_id',array('options'=>$types)); // here better to use type _id instead of Type_id

Sign up to request clarification or add additional context in comments.

1 Comment

Glad it helped, Better if you set $query using some associations between users and types.
0

Use the conventions

Try to avoid plain SQL.

You should use Model::find('list') and adapt it to your needs

Here is the documentation you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.