1

Hi everyone I am facing problem assigning array(fetched from database) to dropdown list. Here is the Code

Controller Code

$skill_rows = $this->Skill->find("all");
$this->set(compact("skill_rows"));

CTP Code

$skills = $skill_rows;
echo $this->Form->select("Seeker.skills",$skills,array("empty"=>"Select"));

Database

id   skill
1    PHP
2    CakePHP

I wish to display only skill column in drop down list but it is displaying complete table.

1 Answer 1

2

Use $this->Skill->find("list"); to return an array of primary key-display field values that can be used in your dropdown. You might need to specify skill as the displayField for this to work correctly.

CakePHP 2

In CakePHP 2 the displayField is an attribute of the model:-

class Skill extends AppModel {

    public $displayField = 'skill';

}

Alternatively you can define the columns you want to be returned in the list when you perform your find():-

$this->Skill->find('all', [
    'fields' => [
        'Skill.id',
        'Skill.skill'
    ]
]);

CakePHP 3

In CakePHP 3 you can define the displayField in the model:-

class SkillsTable extends Table
{

    public function initialize(array $config)
    {
        $this->displayField('skill');
    }
}

If you want to do this on the find() query in CakePHP 3 you need to define the keyField and valueField:-

$this->Skill->find('list', [
    'keyField' => 'id',
    'valueField' => 'skill'
]);
Sign up to request clarification or add additional context in comments.

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.