# Relationship between tables using session

**URL:** <https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537>\
**Category:** Need Help\
**Created:** [March 25, 2020, 3:21am UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537 "2020-03-25T03:21:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecei](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@ecei](https://discourse.cakephp.org/u/ecei)\
**Post date:** [March 25, 2020, 3:21am UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537/1 "2020-03-25T03:21:42Z")

</div>

I’m setting up a shopping cart using sessions. It relates to a product table. I need to access, for example, the product name.

When I use $cart-\>amount, I get the result. But when I try for example, $cart-\>product-\>product\_name, does not work.

How do I create a session with related tables?  
thanks for the help

---

<div class="post-metadata">

**Author:** ![vovo98er](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/vovo98er/32/1721_2.png) [@vovo98er](https://discourse.cakephp.org/u/vovo98er)\
**Post date:** [March 25, 2020, 3:28am UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537/2 "2020-03-25T03:28:52Z")

</div>

Please show us related code and we try help you. ^^

---

<div class="post-metadata">

**Author:** ![ecei](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@ecei](https://discourse.cakephp.org/u/ecei)\
**Post date:** [March 25, 2020, 4:28am UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537/3 "2020-03-25T04:28:21Z")

</div>

I create the session like this:

public function add()  
{  
$request = $this-\>Requests-\>newEntity();  
if ($this-\>request-\>is(‘post’)) {  
$productTable = TableRegistry::getTableLocator()-\>get(‘Products’);  
$products = $productTable-\>newEntity();  
$request = $this-\>Requests-\>patchEntity($request, $this-\>request-\>getData());  
$session = $this-\>request-\>session();  
$cart = $session-\>read(‘cart’);  
$cart[] = $cart;  
$session-\>write(‘cart’, $cart);  
$this-\>Flash-\>success(\_\_(‘Pedido Adicionado’));  
return $this-\>redirect([‘action’ =\> ‘index’]);  
}  
$products = $this-\>Requests-\>Products-\>find(‘list’, [‘limit’ =\> 200]);  
$users = $this-\>Products-\>Users-\>find(‘list’, [‘limit’ =\> 200]);  
$this-\>set(compact(‘pedido’, ‘produtos’, ‘usuarios’));  
}

Here I cannot add the product information in the session.

In the index.ctp:

\<?php foreach($this-\>Session-\>read('cart') as $cart): ?\> \<?= $carrinho-\>quantidade;?\> \<?= $carrinho-\>product\_id;?\> \<?php endforeach; ?\>

The above information appears normally. Except with the product information!  
Thanks for listening!

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 25, 2020, 8:36pm UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537/4 "2020-03-25T20:36:55Z")

</div>

```auto
public function add()
{
    $request = $this->Requests->newEntity();
    if ($this->request->is(‘post’)) {
        // You never use this information at all, these two lines can go away.
        $productTable = TableRegistry::getTableLocator()->get(‘Products’);
        $products = $productTable->newEntity();

        $request = $this->Requests->patchEntity($request, $this->request->getData());
        // Add something like this:
        $request->product = $this->Requests->Products->get($request->product_id);

        $session = $this->request->session();
        $cart = $session->read(‘cart’);
        $cart[] = $cart;
        $session->write(‘cart’, $cart);
        $this->Flash->success(__(‘Pedido Adicionado’));
        return $this->redirect([‘action’ => ‘index’]);
    }
    $products = $this->Requests->Products->find(‘list’, [‘limit’ => 200]);
    $users = $this->Products->Users->find(‘list’, [‘limit’ => 200]);

    // You've got variables called request, products and users, but not pedido, produtos and usuarios.
    $this->set(compact(‘pedido’, ‘produtos’, ‘usuarios’));
}
```

---

<div class="post-metadata">

**Author:** ![ecei](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@ecei](https://discourse.cakephp.org/u/ecei)\
**Post date:** [March 26, 2020, 12:38am UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537/5 "2020-03-26T00:38:43Z")

</div>

Thanks. Worked perfectly!  
I take this opportunity to add a detail:

I followed the same idea to display the user who registered the product.  
The user table is not related to order, but related to product.  
I did it this way:

$request-\>user= $this-\>Requests-\>Products-\>Users-\>get($request-\>product-\>user-\>id);

Error: Record not found in table “usuarios” with primary key [NULL]  
I would like to access this way in the index.ctp:  
$cart-\>product-\>user-\>user\_name

When saved to the database it works. My problem is in the session.  
I appreciate any comments. Thank you!

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 26, 2020, 1:35am UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537/6 "2020-03-26T01:35:18Z")

</div>

Well, you don’t have `$product->user`, so `$product->user->id` will be null, and hence the error. What you presumably meant was `$product->user_id`. But that’s not the best way to go. Instead, use

```
$request->product = $this->Requests->Products->get($request->product_id, [
    'contain' => ['Users']
]);
```

---

<div class="post-metadata">

**Author:** ![ecei](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@ecei](https://discourse.cakephp.org/u/ecei)\
**Post date:** [March 26, 2020, 4:52pm UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537/7 "2020-03-26T16:52:03Z")

</div>

Thanks for the comment.  
I tried that way, but I can’t display the user name in index.ctp, for example.

I Tried:

\<?= $cart-\>has('user') ? $this-\>Html-\>link($cart-\>product-\>user-\>user\_name, ['controller' =\> 'Users', 'action' =\> 'view', $cart-\>product-\>user-\>id]): '' ?\>

Unsuccessfully.  
I keep trying …

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 26, 2020, 9:59pm UTC](https://discourse.cakephp.org/t/relationship-between-tables-using-session/7537/8 "2020-03-26T21:59:22Z")

</div>

That’s because `$cart` doesn’t have a `user`. It has a `product`, and _that_ has a `user`.

`$cart->product->has('user') ? ...`
