一次标准的关联查询

        $this->MerchantProduct->recursive = -1;
        $data = $this->MerchantProduct->find('first',
                array(
            'fields' => array(
                'MerchantProduct.*', 'Merchant.*'
            ),
            'joins' => array(
                array(
                    'table' => 'merchants',
                    'alias' => 'Merchant',
                    'type' => 'inner',
                    'conditions' => array(
                        'Merchant.id = MerchantProduct.merchant_id',
                    )
                ),
            ),
            'order' => 'MerchantProduct.id desc',
            'conditions' => array(
            'MerchantProduct.id' => $merchant_product_id,
            'MerchantProduct.active>=0',
        ))
        );
        $this->set(compact('data'));
        $this->loadModel('MerchantProductPicture');
        $this->MerchantProductPicture->cache = false;
        $carousel = $this->MerchantProductPicture->find('all', array(
            'limit' => 5,
            'fields' => array(
                'MerchantProduct.*','MerchantProductPicture.*',
            ),
            'joins' => array(
                array(
                    'table' => 'merchant_products',
                    'alias' => 'MerchantProduct',
                    'type' => 'inner',
                    'conditions' => array(
                        'MerchantProduct.id = MerchantProductPicture.product_id'
                    )
                ),
                array(
                    'table' => 'merchants',
                    'alias' => 'Merchant',
                    'type' => 'inner',
                    'conditions' => array(
                        'Merchant.id = MerchantProduct.merchant_id'
                    )
                )
            ),
            'group' => 'MerchantProductPicture.product_id',
            'conditions' => array(
                'MerchantProduct.active > 0',
                'Merchant.user_id' => $user_id,
                'MerchantProductPicture.use'=>'slider',
            )
        ));
        $this->set(compact('carousel'));

下面这个是三张表关联

 

posted @ 2013-04-11 10:53  linksgo2011  阅读(137)  评论(0编辑  收藏  举报