# Replace id colum in drop-down by name column

**URL:** <https://discourse.cakephp.org/t/replace-id-colum-in-drop-down-by-name-column/4502>\
**Category:** Uncategorized\
**Created:** [July 2, 2018, 8:12am UTC](https://discourse.cakephp.org/t/replace-id-colum-in-drop-down-by-name-column/4502 "2018-07-02T08:12:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zara](https://avatars.discourse-cdn.com/v4/letter/z/b9bd4f/32.png) [@zara](https://discourse.cakephp.org/u/zara)\
**Post date:** [July 2, 2018, 8:12am UTC](https://discourse.cakephp.org/t/replace-id-colum-in-drop-down-by-name-column/4502/1 "2018-07-02T08:12:59Z")

</div>

after using: bin \ cake bake all name\_table1 and bin \ cake bake all name\_table2, i have a drop-down list with the id of my table1 in the view of table 2. I would like to display the column name 1 instead of id 1 in the drop-down list, but when I save that I memorize the id.  
I use cakephp 3.

**villeController :**  
`

\<?php namespace App\Controller; use App\Controller\AppController; /\*\* \* Ville Controller \* \* @property \App\Model\Table\VilleTable $Ville \* \* @method \App\Model\Entity\Ville[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) \*/ class VilleController extends AppController { /\*\* \* Index method \* \* @return \Cake\Http\Response|void \*/ public function index() { $ville = $this-\>paginate($this-\>Ville); $this-\>set(compact('ville')); } /\*\* \* View method \* \* @param string|null $id Ville id. \* @return \Cake\Http\Response|void \* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. \*/ public function view($id = null) { $ville = $this-\>Ville-\>get($id, ['contain' =\> [] ]); $this-\>set('ville', $ville); } /\*\* \* Add method \* \* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. \*/ public function add() { $ville = $this-\>Ville-\>newEntity(); if ($this-\>request-\>is('post')) { $ville = $this-\>Ville-\>patchEntity($ville, $this-\>request-\>getData()); if ($this-\>Ville-\>save($ville)) { $this-\>Flash-\>success(\_\_('Sauvegardé')); return $this-\>redirect(['action' =\> 'index']); } $this-\>Flash-\>error(\_\_('Echec, veuillez essayer encore !')); } $this-\>set(compact('ville')); } /\*\* \* Edit method \* \* @param string|null $id Ville id. \* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. \* @throws \Cake\Network\Exception\NotFoundException When record not found. \*/ public function edit($id = null) { $ville = $this-\>Ville-\>get($id, ['contain' =\> [] ]); if ($this-\>request-\>is(['patch', 'post', 'put'])) { $ville = $this-\>Ville-\>patchEntity($ville, $this-\>request-\>getData()); if ($this-\>Ville-\>save($ville)) { $this-\>Flash-\>success(\_\_('The ville has been saved.')); return $this-\>redirect(['action' =\> 'index']); } $this-\>Flash-\>error(\_\_('Echec, veuillez essayer encore !')); } $this-\>set(compact('ville')); } /\*\* \* Delete method \* \* @param string|null $id Ville id. \* @return \Cake\Http\Response|null Redirects to index. \* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. \*/ public function delete($id = null) { $this-\>request-\>allowMethod(['post', 'delete']); $ville = $this-\>Ville-\>get($id); if ($this-\>Ville-\>delete($ville)) { $this-\>Flash-\>success(\_\_('Sauvegardé')); } else { $this-\>Flash-\>error(\_\_('Echec, veuillez essayer encore !')); } return $this-\>redirect(['action' =\> 'index']); } /\*\* \* export method \* exporter en fichier excel \* \*/ public function exporteData() { $datatable = ''; $datatable = ''; $datatable .= ''; $ville = $this-\>Ville-\>find('all')-\>toArray(); foreach ($ville as $ville) { $id\_ville = $ville['id\_perso']; $nom = $ville['nom']; $codePostal = $ville['codePostal']; $datatable .= ''; } $datatable .= "

| S.No | NOM | CODE POSTAL |
| --- | --- | --- |
| '. $id\_ville. ' | '. $nom. ' | '. $codePostal. ' |

"; header('Content-Type: application/force-download'); header('Content-disposition: attachemnt; filename= liste\_des\_villes\_p1App.xls'); header("Pragma: "); header("Cache-Control: "); echo $datatable; die; } } ` **add.ctp:** ` \<?php /\*\* \* @var \App\View\AppView $this \* @var \App\Model\Entity\Ville $ville \*/ ?\> 
- \<?= \_\_('Actions') ?\>
- \<?= $this-\>Html-\>link(\_\_('Liste des Villes'), ['action' =\> 'index']) ?\>

 \<?= $this-\>Form-\>create($ville) ?\> \<?= \_\_('Nouvelle Ville') ?\> \<?php echo $this-\>Form-\>control('id\_dept'); echo $this-\>Form-\>control('nom'); echo $this-\>Form-\>control('codePostal');

```
    ?>
</fieldset>
<?= $this->Form->button(__('Valider')) ?>
<?= $this->Form->end() ?>

```

`

view.ctp  
`

\<?php /\*\* \* @var \App\View\AppView $this \* @var \App\Model\Entity\Ville $ville \*/ ?\> 
- \<?= \_\_('Actions') ?\>
- \<?= $this-\>Html-\>link(\_\_('Modification'), ['action' =\> 'edit', $ville-\>id\_ville]) ?\> 
- \<?= $this-\>Form-\>postLink(\_\_('Suppression'), ['action' =\> 'delete', $ville-\>id\_ville], ['confirm' =\> \_\_('Voulez-vous vraiment supprimer ?')]) ?\> 
- \<?= $this-\>Html-\>link(\_\_('Liste des Villes'), ['action' =\> 'index']) ?\> 
- \<?= $this-\>Html-\>link(\_\_('Nouvelle Ville'), ['action' =\> 'add']) ?\> 

| \<?= \_\_('Id Dept') ?\> | \<?= $this-\>Number-\>format($ville-\>id\_dept) ?\> |
| \<?= \_\_('Nom') ?\> | \<?= h($ville-\>nom) ?\> |
| \<?= \_\_('CodePostal') ?\> | \<?= $this-\>Number-\>format($ville-\>codePostal) ?\> |

`  
Picture :  
 ![img](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/2X/b/b974f459102248563a5a7383198de7fbbf6d4314.png)
