# Retrieve related entity data in entity

**URL:** <https://discourse.cakephp.org/t/retrieve-related-entity-data-in-entity/10492>\
**Category:** Need Help\
**Tags:** help\
**Created:** [July 1, 2022, 3:42pm UTC](https://discourse.cakephp.org/t/retrieve-related-entity-data-in-entity/10492 "2022-07-01T15:42:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bistro](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@bistro](https://discourse.cakephp.org/u/bistro)\
**Post date:** [July 1, 2022, 3:42pm UTC](https://discourse.cakephp.org/t/retrieve-related-entity-data-in-entity/10492/1 "2022-07-01T15:42:18Z")

</div>

Hi, I use CakePHP 4.

I have two related classes, _addresses_ and _nations_ for the tables:  
Nations  
Addresses (FK nation\_id)

I have created an virtual field \_getFullAddress() in the _Address_ entitiy and want to display the name of the realted _Nation_. Is this possible? How can I access the related entity data in an entity file?

Thanks!

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [July 1, 2022, 5:32pm UTC](https://discourse.cakephp.org/t/retrieve-related-entity-data-in-entity/10492/2 "2022-07-01T17:32:20Z")

</div>

If you retrieve your addresses so that they `contain` **Nations** , then the data will be available for your `_getFullAddress()` method on `$this->nation` in your **Address** entity.

```php
//In Address entity, something like this
public function _getFullAddress() {
  return $this->city . ' ' .$this->nation->name;
}

```

If you neglect to `contain` **Nations** the method will throw an error so you might want to work out a way to guarantee **Address** entities always contain the expected data or make your entity method tolerate missing contained data.

---

<div class="post-metadata">

**Author:** ![bistro](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@bistro](https://discourse.cakephp.org/u/bistro)\
**Post date:** [July 1, 2022, 7:52pm UTC](https://discourse.cakephp.org/t/retrieve-related-entity-data-in-entity/10492/3 "2022-07-01T19:52:23Z")

</div>

Thanks! I did not understand where I have to use `contain` so that the data is available in the entity file (e.g. in the Controller where I use `$tmp->address->full_address`) … simple as that; problem solved!
