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