# How to get associated table's displayField value instead foreignkey in controller

**URL:** <https://discourse.cakephp.org/t/how-to-get-associated-tables-displayfield-value-instead-foreignkey-in-controller/9291>\
**Category:** Need Help\
**Created:** [April 24, 2021, 10:50am UTC](https://discourse.cakephp.org/t/how-to-get-associated-tables-displayfield-value-instead-foreignkey-in-controller/9291 "2021-04-24T10:50:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kani/32/515_2.png) [@kani](https://discourse.cakephp.org/u/kani)\
**Post date:** [April 24, 2021, 10:50am UTC](https://discourse.cakephp.org/t/how-to-get-associated-tables-displayfield-value-instead-foreignkey-in-controller/9291/1 "2021-04-24T10:50:02Z")

</div>

Cakephp 4  
PHP 8  
Not in view!  
For example  
id | post\_id | user\_id  
1| 1| 1  
2| 1| 3

expected  
1 | post\_title\_1 | username1  
2 | post\_title\_1 | username3

---

<div class="post-metadata">

**Author:** ![mds2018](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mds2018](https://discourse.cakephp.org/u/mds2018)\
**Post date:** [April 24, 2021, 12:51pm UTC](https://discourse.cakephp.org/t/how-to-get-associated-tables-displayfield-value-instead-foreignkey-in-controller/9291/2 "2021-04-24T12:51:00Z")

</div>

This is super easy. You want to get the data from the entity for the table which your table has other records associated with using a contain for those associations. For example:

You have the table “records” which contains (id, post\_id, user\_id). You created a function in your RecordEntity class to get the name of the “record”. In this case you would create:

```
protected function _getName() {
  //do your magic here and return the name

```

//See what data is available:  
pr($this-\>\_fields);  
//You can access the post like this too: $this-\>get(‘post’), $this-\>get(‘user’)-\>get(‘username’) and so on.  
}

And in your controller you would make sure that your find operation would contain the Posts and Users in the query to find the records. Like this: $recordsQuery = $this-\>Records-\>find()-\>contain([‘Posts’, ‘Users’])-\>all();

In the RecordsTable add this: $this-\>setDisplayField(‘name’); //Remember you need to create the function in the entity to get the name. And in that function you could access the Post and User data too since you have contained it in your find operation.

That is how you do it. Cheers

---

<div class="post-metadata">

**Author:** ![kani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kani/32/515_2.png) [@kani](https://discourse.cakephp.org/u/kani)\
**Post date:** [April 24, 2021, 11:48pm UTC](https://discourse.cakephp.org/t/how-to-get-associated-tables-displayfield-value-instead-foreignkey-in-controller/9291/3 "2021-04-24T23:48:29Z")

</div>

Thanks. I found another solution  
$query  
-\>select([‘country\_name’ =\> ‘Countries.name’])  
-\>innerJoinWith(‘Countries’);
