Calling foreign key columns (not id)

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

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

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

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’);
}

Thank you

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 and retrieving data.