【问题标题】:join not working in CakePHP 3加入不在 CakePHP 3 中工作
【发布时间】:2016-08-04 10:57:45
【问题描述】:

我在 controller 中使用此代码来获取连接结果,而不是在 CakePHP 3.2 中包含关联

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

【问题讨论】:

  • 在调试时查询是否显示您应用的joins?
  • 查看编辑2,它加入但只选择sellerProducts列
  • 指定字段以获取其他列。 SellerProducts->find('all', array('fields' => array()...)
  • 谢谢,但是如果我想从几个表中选择所有字段怎么办?
  • 我认为您可以使用fieldsjoin() 来获取所有字段。

标签: cakephp inner-join jointable cakephp-3.2


【解决方案1】:

我认为你可以对所有领域都这样写:

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

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

See Cakephp 3 join

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      相关资源
      最近更新 更多