【问题标题】:Dynamically binding the select dropdown in cakephp动态绑定 cakephp 中的选择下拉菜单
【发布时间】:2016-08-30 06:35:59
【问题描述】:

大家好,我正在使用 cakephp 创建一个表单。在这里,我使用了一个选择下拉菜单。此下拉列表是从数据库中填充的。我有两张表,Branch 和 Employee。 Branch 表中的 PrimaryContactID 是外键。此选择下拉列表中填充了 Employee 表中的 EmployeeID(截至目前)。但是,我需要这样,下拉列表应该显示名称,并且在保存时我应该保存 ID。

我是这样写的:

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>

到目前为止,下拉列表中填充了 EmployeeID 的值。我需要的是,我需要同时拥有 EmployeeID 和 EmployeeName。当我单击下拉列表时,我应该会看到 EmployeeName,并且在保存时我应该保存 ID。

如何做到这一点?我应该做哪些改变?

【问题讨论】:

    标签: php cakephp cakephp-3.2


    【解决方案1】:

    调用列表时,您可以配置用于键的字段和 value 分别带有 keyField 和 valueField 选项:

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

    阅读更多如何检索数据和结果:

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

    【讨论】:

    【解决方案2】:
    //write your query like this
    
    $employee = $this->Branch->Employee->find('list')->select(['EmployeeID','EmployeeName']));
    

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      • 2011-08-25
      相关资源
      最近更新 更多