I am new to Cakephp 3.0 and i don't know how to get all the products of the contracts in my example.
Models :
SalesContracts:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class SalesContractsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->table('sales_contracts');
$this->belongsToMany('SalesProducts',[
'className' => 'SalesProducts',
'type' => 'INNER',
'joinTable' => 'sales_contracts_products',
'foreignKey' => 'sales_contracts_id',
'targetForeignKey' => 'sales_products_id'
]);
}
}
SalesProducts:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class SalesProductsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->table('sales_products');
$this->belongsToMany('SalesContracts',[
'className' => 'SalesContracts',
'joinTable' => 'sales_contracts_products',
'type' => 'INNER',
'foreignKey' => 'sales_products_id',
'targetForeignKey' => 'sales_contracts_id'
]);
}
}
This method receives the ID of the contract in parameter and I need to use it to get his products.
public function show_products($id = null){
$products $this->SalesContracts->find('all')->contain(['SalesProducts'])->where(['SalesContractsProducts.sales_contracts_id' => $id]);
}
But I cannot write a proper query about that..
Any solutions ?
Thanks for your help!