2016-08-30 3 views
0

안녕하세요 여러분, cakephp를 사용하여 양식을 만들고 있습니다. 여기서는 선택 드롭 다운을 사용했습니다. 이 드롭 다운은 데이터베이스에서 채워집니다. 나는 두 개의 테이블, 지부와 직원을 가지고있다. 분기 테이블의 PrimaryContactID는 외래 키입니다. 이 선택 드롭 다운은 Employee 테이블의 EmployeeID로 채워 집니 다. 하지만 드롭 다운에 이름이 표시되어야하고 저장하는 동안 ID를 저장해야합니다.cakephp에서 선택 드롭 다운을 동적으로 바인딩

이 내가 쓴 방법은 다음과 같습니다

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> 

은 지금부터, 드롭 다운은 직원 ID의 값으로 채워집니다. 필자가 요구하는 것은 EmployeeID와 EmployeeName을 모두 가지고 있어야합니다. 드롭 다운을 클릭하면 EmployeeName이 표시되고 저장하는 동안 ID를 저장해야합니다.

어떻게 수행하나요? 내가해야 할 모든 변화는 무엇입니까?

답변

1

목록을 호출 할 때 당신은 키에 사용되는 필드와 각각 키 필드와 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

+0

고마워요. 나는 의심의 여지가있다. 그걸 도와 줄 수 있니? – SSS

+0

새로운 질문 추가 – Salines

+0

예, 90 분을 기다리는 중입니다. – SSS

0
//write your query like this 

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