# Replacing the id column with something more meaningful

**URL:** <https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726>\
**Category:** Need Help\
**Created:** [December 13, 2016, 9:44am UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726 "2016-12-13T09:44:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnny2shoes](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@johnny2shoes](https://discourse.cakephp.org/u/johnny2shoes)\
**Post date:** [December 13, 2016, 9:44am UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/1 "2016-12-13T09:44:31Z")

</div>

Cakephp , for foreign keys, uses the id, which is meaningless ( id’s of 1,2,3,4 is meaningless to a user), so , I want to replace them ( with the username ):  
add.ctp file:  
echo $this-\>Form-\>input(‘clients.\_ids’, [‘options’ =\> $clients]);

client, has an autocrement id ( int), and a username ( varchar )

so, replace alist of Ids with the username, how do I change the above line to do that?

thanks in advance

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [December 13, 2016, 10:05am UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/2 "2016-12-13T10:05:12Z")

</div>

Unclear a add actions for Clients controller?  
if Yes, How you can show list of clients when add new client.  
if No, you have to set model relations .

I guess you need :  
echo $this-\>Form-\>input(‘clients.\_ids’, [‘options’ =\> $clients]); where $clients = array( 1 =\> ‘Client 1’, 2 =\> ‘Client2’);

---

<div class="post-metadata">

**Author:** ![johnny2shoes](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@johnny2shoes](https://discourse.cakephp.org/u/johnny2shoes)\
**Post date:** [December 13, 2016, 10:35am UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/3 "2016-12-13T10:35:23Z")

</div>

thanks for the reply.

say, staff have many clients  
client is just an id ( int ) and username ( varchar )  
when I created a new staff ( in the add.ctp), cakephp will show me a list of clients I can add to the staff ( but it will display a list of Ids 1,2,3,4,5, instead of the client’s usernames )  
how can I change it to display a list of usernames instead?  
regards

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [December 13, 2016, 11:40am UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/4 "2016-12-13T11:40:11Z")

</div>

All should works easy in Cake style.  
echo $this-\>Form-\>input('clients.\_id);  
But check relations in models Staff ad User , if Staff hasMany Users.  
I think this view you can cake bake .

---

<div class="post-metadata">

**Author:** ![johnny2shoes](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@johnny2shoes](https://discourse.cakephp.org/u/johnny2shoes)\
**Post date:** [December 13, 2016, 11:48am UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/5 "2016-12-13T11:48:08Z")

</div>

I don’t think you understand. It’s very easy, and the solution is also easy ( but I don’t know what it is because I’m new to cakephp )  
I have the hasMany relation. I’ve used cake bake all staffs / clients  
Cakephp ( by default ) will display ( in the add.ctp for staff) all the client Ids. But displaying Ids ( 1,2,3,4,5) doesn’t mean anything to anyone , so I want to display the username instead of Id.

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [December 13, 2016, 1:16pm UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/6 "2016-12-13T13:16:30Z")

</div>

Show your tables and models (Staff and User).

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [December 13, 2016, 2:39pm UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/7 "2016-12-13T14:39:32Z")

</div>

@johnny2shoes Set the [displayField()](http://api.cakephp.org/3.3/class-Cake.ORM.Table.html#_displayField) to `username` in your `ClientsTable::initialize()`.

---

<div class="post-metadata">

**Author:** ![Ruslan](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Ruslan](https://discourse.cakephp.org/u/Ruslan)\
**Post date:** [December 13, 2016, 6:35pm UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/8 "2016-12-13T18:35:55Z")

</div>

It’s additional manipulation 🙂  
Cake can not recognize display field with word “name” ?

---

<div class="post-metadata">

**Author:** ![johnny2shoes](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@johnny2shoes](https://discourse.cakephp.org/u/johnny2shoes)\
**Post date:** [December 13, 2016, 8:22pm UTC](https://discourse.cakephp.org/t/replacing-the-id-column-with-something-more-meaningful/1726/9 "2016-12-13T20:22:55Z")

</div>

ok, thanks to all.  
That worked ( thanks ADmad).  
Can you please explain, ADmad, does changing displayField, in ClientsTable::initialize, only change that one view, add.ctp, for staff, when displaying clients, or does it change every view that displays clients, with Ids, to Username?

Again, how, do I get rid of that red strip at the bottom?
