2017-12-28 28 views
0

CakePHP 3 - 他のテーブルを介して結合?私は、次のDB構造を作成しようとしています

私は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' 
      ] 
     ]); 

私はこのエラーを取得: error

+0

本当に私たちに何も伝えていない "それだけで作業aintの"。あなたが抱えている問題は何ですか?ここに質問はありません。 – cgTag

+0

私は文と比較してケーキの中に関係が設定されていることを確認する必要があります。私はもう少し2秒を追加します –

+0

追加情報 –

答えて

1

あなたのSalesTransactionモデルでhasMany関連を使用しています。

それはbelongsToMany次のようになります。

SalesTransactionTable

$this->belongsToMany('Products', [ 
    'through' => 'ProductsInTransaction' 
]); 
関連する問題