我是Cakephp 3.0的新手,我不知道如何在我的例子中获得合同的所有产品。
模特:
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'
]);
}
}
此方法在参数中接收合同的ID,我需要使用它来获取他的产品。
public function show_products($id = null){
$products $this->SalesContracts->find('all')->contain(['SalesProducts'])->where(['SalesContractsProducts.sales_contracts_id' => $id]);
}
但我不能写一个关于那个的正确查询..
任何解决方案?
感谢您的帮助!