2016-08-04 8 views
0

ich in controller diesen Code bin mit verbundenem Ergebnis zu erhalten, anstatt in CakePHP 3,2join nicht in CakePHP Arbeits 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' 
      ] 
      ] 
     ]); 

Aber auf debug assoziiert enthält, gibt es nur Daten aus SellerProducts und schließt nicht (beitreten) anderen Tabellen.

Ich möchte Ergebnis verbunden und nicht zugeordnetes Ergebnis mit contain Methode, weil contain gibt Multilevel-Array, die Sammlung von verknüpften Daten aus Multilevel-Array ist schwierig.

Edit 2: Ergebnis des Debug ($ 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

auf dem Debug zeigt die Abfrage die 'Join's, die Sie angewendet haben? – Riad

+0

Siehe Edit 2, es verbindet, aber nur sellerProducts Spalten –

+0

wählen Sie die Felder zum Abrufen anderer Spalten. 'SellerProducts-> find ('alle', array ('fields' => array() ...)' – Riad

Antwort

0

Ich glaube, Sie wie dies für alle Felder schreiben kann:

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

ODER

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

See Cakephp 3 join

0

Versuchen Sie, diese

$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);