# Calling foreign key columns (not id)

**URL:** <https://discourse.cakephp.org/t/calling-foreign-key-columns-not-id/6761>\
**Category:** Need Help\
**Created:** [October 15, 2019, 5:27am UTC](https://discourse.cakephp.org/t/calling-foreign-key-columns-not-id/6761 "2019-10-15T05:27:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James1](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James1](https://discourse.cakephp.org/u/James1)\
**Post date:** [October 15, 2019, 5:27am UTC](https://discourse.cakephp.org/t/calling-foreign-key-columns-not-id/6761/1 "2019-10-15T05:27:18Z")

</div>

How would I display the foreign key’s column, “SupplierName”, from the foreign key “Supplier\_fk”?

I’m new to php, I’ve tried things like:

$book-\>Supplier\_fk.SupplierName  
$book-\>Supplier\_fk-\>SupplierName

Thank you

Code

```auto
    <td>
        <?= $this->Html->link($book->Supplier_fk, ['action' => 'view', $book->id]) ?>
    </td>

```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 16, 2019, 1:37pm UTC](https://discourse.cakephp.org/t/calling-foreign-key-columns-not-id/6761/2 "2019-10-16T13:37:13Z")

</div>

If you have associations and containment set up correctly, it would be `$book->supplier->SupplierName`.

---

<div class="post-metadata">

**Author:** ![James1](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James1](https://discourse.cakephp.org/u/James1)\
**Post date:** [November 1, 2019, 5:45am UTC](https://discourse.cakephp.org/t/calling-foreign-key-columns-not-id/6761/3 "2019-11-01T05:45:34Z")

</div>

Hi, I’m still having trouble.

First for all shouldn’t it be `$book-Supplier_fk->SupplierName`? Since Supplier\_fk is a column name and supplier is not?

I’ve tried to add the associations. What do you mean by containment?

class BooksTable extends Table  
{  
public function initialize(array $config)  
{  
$this-\>hasOne(‘Suppliers’);  
}  
}

class SuppliersTable extends Table  
{  
public function initialize(array $config)  
{  
$this-\>hasMany(‘Books’);  
}

 ![tempPic3](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/c/c9741e71e59ed70b667991c03a5ba0d58c139564.png)

Thank you

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 1, 2019, 7:17pm UTC](https://discourse.cakephp.org/t/calling-foreign-key-columns-not-id/6761/4 "2019-11-01T19:17:50Z")

</div>

`Supplier_fk` is an integer, not an object. If you use something like

```
$books = $this->Books->find()
    ->where( /* your conditions go here */ )
    ->contain(['Suppliers'])

```

then each `Book` entity that you get will have `Supplier_fk` (e.g. `$book->Supplier_fk`) just as the plain old integer, but also `supplier` (e.g. `$book->supplier`) will be the entity with the relevant `Supplier` entity in it. This is all well covered in the manual sections on [associations](https://book.cakephp.org/3/en/orm/associations.html) and [retrieving data](https://book.cakephp.org/3/en/orm/retrieving-data-and-resultsets.html).
