:CakePHP 3 - 他のテーブルを介して結合?私は、次のDB構造を作成しようとしています
sales_transaction
products
- :
http://www.databaseanswers.org/data_models/generic_pos/index.htm
具体的には、私はとの関係で探しています
products_in_transaction
私はsales_transaction
DBへの呼び出しを行い、その後、ちょうどproducts
を含み、そしてそれは1つのクエリですべてをフェッチすることができるように私には、関係のセットアップをしたいです。
public function initialize(array $config)
{
parent::initialize($config); // TODO: Change the autogenerated stub
$this->belongsToMany('SalesTransaction', [
'through' => 'ProductsInTransaction'
]);
}
ProductsTable
SalesTransactionTable
public function initialize(array $config)
{
parent::initialize($config); // TODO: Change the autogenerated stub
$this->hasMany('Products', [
'through' => 'ProductsInTransaction'
]);
}
: しかし、それだけでAINT作業なので:-)ここ
は私のコードで私を助けてください製品内取引テーブルpublic function initialize(array $config)
{
parent::initialize($config); // TODO: Change the autogenerated stub
$this->belongsTo('SalesTransaction', [
'foreignKey' => 'transaction_id',
'joinType' => 'inner'
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'inner'
]);
}
上記の関係から明らかになるはずです。本質的には、SalesTransaction
にはすべての取引が含まれており、products_in_transaction
は私の参加テーブルであり、products
と一緒に橋渡しをしていますが、完全な仕組みを得るためのリンクを見てください。
私はそれを取得するために、このコードを実行しよう:
$sales_transactions = $sales_transactions_table->find('all', [
'conditions' => [
'device_id IN' => $deviceIdsInDepartment
],
'contain' => [
'Products'
]
]);
本当に私たちに何も伝えていない "それだけで作業aintの"。あなたが抱えている問題は何ですか?ここに質問はありません。 – cgTag
私は文と比較してケーキの中に関係が設定されていることを確認する必要があります。私はもう少し2秒を追加します –
追加情報 –