Relationship between tables using session

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

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

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!

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’));
}
1 Like

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!

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']
]);

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 …

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

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