SSS
SSS

Reputation: 713

Dynamically binding the select dropdown in cakephp

Hi guys I'm creating a form using cakephp. Here I've used a select dropdown. This dropdown is filled from the database. I have two tables, Branch and Employee. PrimaryContactID in Branch table is the foreign key. This select dropdown is filled with the EmployeeID from the Employee table(as of now). But, I need like, the dropdown should show the name and while saving I should save the ID.

This is how I've written:

controller:
public function add()
    {
        $branch = $this->Branch->newEntity();
        if ($this->request->is('post')) {
            $branch = $this->Branch->patchEntity($branch, $this->request->data);
            if ($this->Branch->save($branch)) {
                $this->Flash->success(__('The branch has been saved.'));

                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The branch could not be saved. Please, try again.'));
            }
        }
        $employee = $this->Branch->Employee->find ( 'list', [ 
                'limit' => 200 
        ] );
        $this->set(compact('branch', 'employee'));
        $this->set('_serialize', ['branch']);
    }
BranchTable.php:
class BranchTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('branch');
        $this->displayField('BranchID');
        $this->primaryKey('BranchID');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Employee', [
                'foreignKey' => 'EmployeeID',
                'joinType' => 'INNER'
        ]);
    }
}
  add.ctp:
<div class="branch form large-9 medium-8 columns content">
    <?= $this->Form->create($branch) ?>
    <fieldset>
        <legend><?= __('Add Branch') ?></legend>
        <?php
            echo $this->Form->input('BranchName');
            echo $this->Form->input('BranchCode');
            echo $this->Form->input('Address');
            echo $this->Form->input('Telephone');
            echo $this->Form->input('PrimaryContactID', ['options' => $employee]);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

As of now, the dropdown is filled with the values of EmployeeID. What I require is, I need to have both EmployeeID and EmployeeName. When I click the dropdown, I should see the EmployeeName and while saving I should save the ID.

How to do that? What are all the changes I should do?

Upvotes: 0

Views: 1019

Answers (2)

rv_stack
rv_stack

Reputation: 1

//write your query like this

$employee = $this->Branch->Employee->find('list')->select(['EmployeeID','EmployeeName']));

Upvotes: 0

Salines
Salines

Reputation: 5767

When calling list you can configure the fields used for the key and value with the keyField and valueField options respectively:

$employee = $this->Branch->Employee->find('list', [ 
    'keyField' => 'EmployeeID',
    'valueField' => 'EmployeeName',
    //'limit' => 200
]);

Read more how to retrieving data & results:

http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

Upvotes: 1

Related Questions