0

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?

SSS
  • 703
  • 4
  • 11
  • 23

2 Answers2

1

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

Salines
  • 5,674
  • 3
  • 25
  • 50
  • Thanks that works. I have another doubt, can you help me with that? – SSS Aug 30 '16 at 07:28
  • Yeah, waiting for 90 minutes to complete. – SSS Aug 30 '16 at 08:47
  • I've asked it, please check it out: http://stackoverflow.com/questions/39225722/display-data-from-two-different-tables-in-the-index-using-cakephp – SSS Aug 30 '16 at 11:01
  • I've posted the question: http://stackoverflow.com/questions/39225722/display-data-from-two-different-tables-in-the-index-using-cakephp – SSS Aug 30 '16 at 11:02
0
//write your query like this

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