# Relate the 2 tables

**URL:** https://discourse.cakephp.org/t/relate-the-2-tables/5143
**Category:** Need Help
**Created:** [October 23, 2018, 7:00pm UTC](https://discourse.cakephp.org/t/relate-the-2-tables/5143 "2018-10-23T19:00:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bichomen](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bichomen/32/1070_2.png) [@bichomen](https://discourse.cakephp.org/u/bichomen)
#### Post date: [October 23, 2018, 7:00pm UTC](https://discourse.cakephp.org/t/relate-the-2-tables/5143/1 "2018-10-23T19:00:49Z")

</div>

Hi, I do not know how to do the following:

I have 2 tables:

- Users
- Profile

Users:

- id  
-profile\_id
- First name
- Surname
- Mail
- Password  
…

Profile:

- id
- Photo
- First name
- Surname  
…

I want to relate the 2 tables, recover the data of the 2 tables when I consult the id of the user.

With other tables I would have no problem with a join, but I have no idea how to do it with UsersController:

```
 public function login() {
   $isPost = $this->request->is('POST');
        if($isPost) {
         $user = $this->Auth->identify();
         if($user) {
           $this->Auth->setUser($user);
           $this->set('user', $user);
           return $this->redirect($this->Auth->redirectUrl());
         }
         else {
           $this->Flash->error(__('Email o contraseña incorrecta.', ['key' => 'auth']));
         }
       }
       if($this->Auth->user()) {
         $this->Auth->logout();
         $this->redirect($this->referer());
       }
  }
```

---

<div class="post-metadata">

### Author: ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)
#### Post date: [October 24, 2018, 4:33am UTC](https://discourse.cakephp.org/t/relate-the-2-tables/5143/2 "2018-10-24T04:33:30Z")

</div>

you can use [https://book.cakephp.org/3.0/en/controllers/components/authentication.html#customizing-find-query](https://book.cakephp.org/3.0/en/controllers/components/authentication.html#customizing-find-query)  
and put any `->contain` in there

---

<div class="post-metadata">

### Author: ![bichomen](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bichomen/32/1070_2.png) [@bichomen](https://discourse.cakephp.org/u/bichomen)
#### Post date: [October 24, 2018, 8:25am UTC](https://discourse.cakephp.org/t/relate-the-2-tables/5143/3 "2018-10-24T08:25:41Z")

</div>

"`contain` option have been deprecated as of 3.1"

---

<div class="post-metadata">

### Author: ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)
#### Post date: [October 24, 2018, 4:05pm UTC](https://discourse.cakephp.org/t/relate-the-2-tables/5143/4 "2018-10-24T16:05:27Z")

</div>

you are mistaking `contain` option of Auth component with `->contain` function of query builder:

```auto
public function findAuth(\Cake\ORM\Query $query, array $options)
{
    $query
        ->contain(['Profiles']);

    return $query;
}

```

---

<div class="post-metadata">

### Author: ![bichomen](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bichomen/32/1070_2.png) [@bichomen](https://discourse.cakephp.org/u/bichomen)
#### Post date: [October 24, 2018, 6:07pm UTC](https://discourse.cakephp.org/t/relate-the-2-tables/5143/5 "2018-10-24T18:07:07Z")

</div>

Yes, in the end I built the query with join:

```
public function findAuth(\Cake\ORM\Query $usuario, array $options){
$usuario->join(['Perfil'=> [
                                    'table' => 'perfil',
                                    'type' => 'LEFT',
                                    'conditions' => 'Perfil.id = perfil_id'
                                   ]
                     ])
              ->join(['Imagenes'=> [
                                    'table' => 'imagenes',
                                    'type' => 'LEFT',
                                    'conditions' => 'Imagenes.id = Users.imagen_id'
                                   ]
                     ])
            ->select([
                      'Users.id', 'Users.persona_id', 'Users.nombre', 'Users.apellido', 'Users.email', 'Users.password', 'Users.role', 'Users.active', 'Users.creado', 'Users.modificado',
                      'usuarioNombre' => 'Perfil.nombre',
                      'usuarioPrimerApellido' => 'Perfil.primer_apellido',
                      'usuarioSegundoApellido' => 'Perfil.segundo_apellido',
                      'imagen' => 'Imagenes.imagen'
                     ])
            ->first();
      return $usuario;
 }
}
```

---

<div class="post-metadata">

### Author: ![GenryMarquez](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@GenryMarquez](https://discourse.cakephp.org/u/GenryMarquez)
#### Post date: [October 30, 2018, 7:38am UTC](https://discourse.cakephp.org/t/relate-the-2-tables/5143/6 "2018-10-30T07:38:10Z")

</div>

hello, do not complicate yourself use the cakephp console creating the model and the user and profile controller and it will create the relationships in the model. otherwise you create the relationships manually in your belongsto and hasmany model.
