Isengo
Isengo

Reputation: 2083

CakePHP relations in Database

I baked two controllers users and moves. Now I want to link the moves the users is linked to (only one). The bake did most of the work for me (thank god).

        <td><?= $user->has('move') ? $this->Html->link($user->move->name,       ['controller' => 'Moves', 'action' => 'view', $user->move->id]) : '' ?></td>

It actually show nothing. I have a foreign key in my database and the move 1 is correctly linked to the user.

foreign key in users "move_id" - primary key in the moves is "id"

I get no error and also no debug call. Any ideas?

Upvotes: 0

Views: 38

Answers (1)

Indrasis Datta
Indrasis Datta

Reputation: 8606

It's showing nothing because in your ternary operator, it's executing this next portion >>>>>>>

    : ''

and therefore prints a blank. Your $user object probably doesn't have a field called "move".

You need to check the following:

  1. Are associations defined for these two models?

  2. In your controller, where you're fetching this $user before doing $this->set(.....), did you mention "contain"? Since you need to access the Users as well as the Moves models?

    For example:

     // If you're trying to find all users records
    
     $users = $this->Users->find('all')
            ->contain(['Moves']);        
    
     // For a single user record
    
     $users = $this->Users->get($this->Auth->user("id"))
            ->contain(['Moves']); 
    

Hope this helps.

Peace! xD

Upvotes: 1

Related Questions