Ace Eusebio
Ace Eusebio

Reputation: 401

CakePHP: How to view details from a foreign table?

I have two models: member & vocation. They are structured this way:

MEMBER
member_id
last_name
first_name
vocation_id (foreign key)

VOCATION
vocation_id
description

In the view page of the member, I want to show the last name, first name and the description of its vocation. I couldn't seem to find it in the documentation.

I tried the basic CakePHP viewing but it returns just the ID of the vocation. I'm guessing I need to make a new function in my MembersController to find the vocation details he has. I just need the standard on how to do it. I will look more in the documentation. Perhaps I just missed something there. I'll just update this question if I find the answer. For now, here's my current code:

<h1>#<?php echo h($member['Member']['member_id']); ?></h1>
<table>
    <tr>
        <td>Last Name</td>
        <td><?php echo $member['Member']['last_name']; ?></td>
    </tr>
    <tr>
        <td>First Name</td>
        <td><?php echo $member['Member']['first_name']; ?></td>
    </tr>
    <tr>
        <td>Vocation</td>
        <td><?php echo $member['Member']['vocation_id']; ?></td>
    </tr>
</table>

Upvotes: 0

Views: 35

Answers (1)

Salines
Salines

Reputation: 5767

<tr>
    <td>Vocation</td>
    <td><?php echo $member['Vocation']['vocation_id']; ?></td>
</tr>
<tr>
    <td>Vocation description</td>
    <td><?php echo $member['Vocation']['description']; ?></td>
</tr>

Upvotes: 2

Related Questions