2016-08-04 70 views
0

我用了controller此代碼來獲取連接的結果,而不是包含在CakePHP的3.2聯接不CakePHP中工作3

$abc = $this->Products->SellerProducts->find('all', [ 
      'conditions' => [ 
      'Products.subcategory_id' => $id, 
      'SellerProducts.stock >' => 0, 
      ], 
      'join' => [ 
      'products' => [ 
       'table' => 'Products', 
       'type' => 'INNER', 
       'conditions' => [ 
       'products.id = SellerProducts.product_id' 
       ] 
      ], 
      'brands' => [ 
       'table' => 'Brands', 
       'type' => 'INNER', 
       'conditions' => 'brands.id = products.brand_id' 
      ], 
      'product_colors' => [ 
       'table' => 'ProductColors', 
       'type' => 'INNER', 
       'conditions' => 'product_colors.product_id = products.id' 
      ], 
      'colors' => [ 
       'table' => 'Colors', 
       'type' => 'INNER', 
       'conditions' => 'colors.id = product_colors.color_id' 
      ] 
      ] 
     ]); 

但在debug相關,它SellerProducts僅給出的數據,不包括(加入)其他表格。

我想要得到的加入結果,而不是使用contain方法,因爲contain給多級陣列是很難從多級陣列獲取相關數據的收集相關的結果。

編輯2:調試結果($ abc);

SELECT SellerProducts.id AS `SellerProducts__id`, SellerProducts.seller_id AS `SellerProducts__seller_id`, SellerProducts.product_id AS `SellerProducts__product_id`, SellerProducts.selling_price AS `SellerProducts__selling_price` FROM seller_products SellerProducts INNER JOIN Products products ON products.id = SellerProducts.product_id INNER JOIN Brands brands ON brands.id = products.brand_id INNER JOIN ProductColors product_colors ON product_colors.product_id = products.id INNER JOIN Colors colors ON colors.id = product_colors.color_id WHERE (Products.subcategory_id = :c0 AND SellerProducts.stock > :c1) 
+0

關於調試請問顯示你應用的'join's? – Riad

+0

請參閱編輯2,它會加入但只選擇sellerProducts列 –

+0

指定要提取其他列的字段。 'SellerProducts-> find('all',array('fields'=> array()...)' – Riad

回答

0

我想你可能會這樣寫的所有領域:

$this->SellerProducts->find('all',['fields' => [], 'conditions' => []] 

OR

$this->SellerProducts->find('all', [ 
     'conditions' => [ 
     'Products.subcategory_id' => $id, 
     'SellerProducts.stock >' => 0, 
     ]])->join(.....); 

​​

0

試試這個

$SellerProducts = TableRegistry::get('SellerProducts'); 
$resultSet = $SellerProducts->find('all')->hydrate(false) 
        ->select(['SellerProducts.id'])       
        ->join([ 
         'Products'=> [ 
          'table'  => 'products', 
          'type'  => 'INNER', 
          'conditions' => 'Products.id = SellerProducts.product_id', 
         ], 
         'Brands'=> [ 
          'table'  => 'brands', 
          'type'  => 'INNER', 
          'conditions' => 'Brands.id = Products.brand_id', 
         ], 
         'ProductColors'=> [ 
          'table'  => 'product_colors', 
          'type'  => 'INNER', 
          'conditions' => 'ProductColors.product_id = Products.id', 
         ], 
         'Colors'=> [ 
          'table'  => 'colors', 
          'type'  => 'INNER', 
          'conditions' => 'Colors.id = ProductColors.color_id', 
         ] 
        ]) 
        ->where([ 
         'Products.subcategory_id' => $id, 
         'SellerProducts.stock >' => 0 
        ]) 
        ->toArray(); 
    print_r($resultSet);