# Read in multiples tables with Session (cakephp 4)

**URL:** <https://discourse.cakephp.org/t/read-in-multiples-tables-with-session-cakephp-4/8012>\
**Category:** Need Help\
**Created:** [July 1, 2020, 9:11am UTC](https://discourse.cakephp.org/t/read-in-multiples-tables-with-session-cakephp-4/8012 "2020-07-01T09:11:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Virginie](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/virginie/32/1898_2.png) [@Virginie](https://discourse.cakephp.org/u/Virginie)\
**Post date:** [July 1, 2020, 9:11am UTC](https://discourse.cakephp.org/t/read-in-multiples-tables-with-session-cakephp-4/8012/1 "2020-07-01T09:11:19Z")

</div>

Hello !  
I come back to you for a Session problem. I connect to the identification table, and I manage to retrieve the information from this table of the connected person. I would now like to be able to read all the information about the person. I can do it wihout a session, bit with the session, I can’ do it. I tried with write, but I don’t really undersant if it’s really that, because it displays string. Do you have a lead please ? Thanks !  
Ps: I try to do it without using helpers. My mistake right now, and that it requires me to create a helper.

public function login()  
{  
if ($this-\>request-\>is(‘post’)) {  
$user = $this-\>Auth-\>identify();  
if ($user) {  
$this-\>Auth-\>setUser($user);  
return $this-\>redirect($this-\>Auth-\>redirectUrl(’./identification/welcome’));  
} else {  
$this-\>Flash-\>error(‘Nom d’utilisateur ou mot de passe incorrect’);  
}  
}  
}

public function welcome()  
{  
$this-\>Auth-\>user(‘id’);  
$this-\>Auth-\>user(‘username’);  
$this-\>Auth-\>user(‘candidate.lastname’);  
}

View :

$session = $this-\>getRequest()-\>getSession();  
$session-\>write(‘Candidate’, $this-\>User-\>Candidate-\>findById($this-\>Auth-\>user(‘id’)));

\<?= h($session-\>read('Auth.User.username')) ?\> \<?= h($session-\>read('Auth.User.Candidate.lastname')) ?\>

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [July 1, 2020, 1:47pm UTC](https://discourse.cakephp.org/t/read-in-multiples-tables-with-session-cakephp-4/8012/2 "2020-07-01T13:47:24Z")

</div>

Hi  
Try to post code with formatting, so its easier to read. See [Creating and highlighting code blocks](https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks)

You should read the session in the controller, and use [View Variables](https://book.cakephp.org/4/en/controllers.html#setting-view-variables)  
You should **never** use models/sessions directly in the view

something like this

```php
// in controller
public function login()
{
    if ($this->request->is(‘post’)) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl(’./identification/welcome’));
        } else {
            $this->Flash->error(‘Nom d’utilisateur ou mot de passe incorrect’);
        }
    }
}

public function welcome()
{
    $userId = $this->Auth->user('id');
    $userName = $this->Auth->user('username');
    $lastName = $this->Auth->user(‘candidate.lastname’);

    $session = $this->getRequest()->getSession();
    $candidate = $this->Users->Candidates->findById($userId)->first();
    $session->write(‘Candidate’, $candiate);

    $this->set(compact('userId', 'userName', 'lastName', 'candidate'));
}

```

In the template (welcome.ctp)

```auto
UserId <?= h($userId) ?>
userName <?= h($userName) ?>
Candidate <?= h($candidate->lastname) ?>

```

---

<div class="post-metadata">

**Author:** ![Virginie](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/virginie/32/1898_2.png) [@Virginie](https://discourse.cakephp.org/u/Virginie)\
**Post date:** [July 1, 2020, 10:51pm UTC](https://discourse.cakephp.org/t/read-in-multiples-tables-with-session-cakephp-4/8012/4 "2020-07-01T22:51:55Z")

</div>

Thank you, I thought ``` it was only on discord 😅  
Okay. We don’t use read anymore?  
With this code, it tells me: Call to a member function findById () on null.  
I recover the value of $userId just the line before that of findById, but if I put the debug just below it does not recover it …

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [July 1, 2020, 11:38pm UTC](https://discourse.cakephp.org/t/read-in-multiples-tables-with-session-cakephp-4/8012/5 "2020-07-01T23:38:10Z")

</div>

> [@Virginie](#):
>
> Call to a member function findById () on null.

That depends on the name of the model.  
If you have `src/Model/Table/CandidatesTable.php` you can Call

```php
$this->loadModel('Candidates');
$candidate = $this->Candidates->findById($userId)->first();

```

In your original code you had User instead of Users.

```auto
$this->User->Candidates->findById($userId)->first();

```

Most examples you’ll find will be following [conventions](https://book.cakephp.org/4/en/intro/conventions.html#model-conventions), I’ll be easier to call your table `Users` instead of `User`

> [@Virginie](#):
>
> We don’t use read anymore?

You can use session read/write. It’s best on a controller/cell.
