Problem filtering orders from user who owns the product - Cakephp 3

Friends, I have an order table, where I record the orders made by a user. The table has these fields:

id, quantity, product_id, user_id

In this way, the user can register his product and consult his history. The problem is that I want to do this on the side of the company that registered the product. I want to filter by product_id only for the user who owns the product.

My OrdersTable.php

.
.
    $this->belongsTo('Products', [
            'foreignKey' => 'product_id',
            'joinType' => 'INNER',
        ]);
    $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER',
        ]);
    }

My method is below. Note that I enter the id manually as an example. The idea is to capture this information from the user who is logged in. I appreciate if anyone can review or comment. I don’t know how to solve this detail!

    public function index()
    {
        $this->paginate = ['contain' => ['Users','Products']];
        $query = $this->Orders->find('all')->where(['Orders.product_id' => 76]);

        $this->set('orders', $this->paginate($query));
        $this->set('_serialize', ['order']);
    }

You haven’t said anything about which Authentication system you are using.

Whatever it is, its documentation will tell you how to get the user id from the session.

I get the user id like this: $ this-> getRequest () -> getSession () -> read (‘Auth.User.id’). But I believe that is not the problem. The idea is as follows:

Company A has products with id equal to 1.2 and 3. Company B has a product with id equal to 4. If company A logs in to the system, it will be able to filter orders with product_id equal to 1.2 and 3. Orders with product_id equal to 4 do not enter the history of company A. –

To do this:

you are going to have to associate your Products with Companies.

  • Companies hasMany Products
  • Products belongsTo Companies

This will give you the link you need to access the products for a single Company.

This relationship already exists. Company, customer, super administrator … all are users. In the user table, I have a group_id where I separate its functions.

The customer buys a product and his user_id is saved in the order table. Thus, he can consult his order history. The company must also filter its order history. This is my problem. The product id is saved in the order table, but I can’t access only the orders containing products from the company that is logged in

Have you looked at this section of the book

It sounds like the first example would provided a starting point:

$query = $orders->find();
$query->matching('Products', function ($q) {
    return $q->where(['Products.company_id' => $userId]);
});

Thank you friend.

        $query = $this->Orders->find();
        $query->contain('Products', function ($q) {
            return $q->where(['Products.user_id' => $this->getRequest()->getSession()->read('Auth.User.id')]);
        });