Cakephp 2.x - 从多个表中查询

时间:2015-08-30 07:19:58

标签: cakephp

我有3张桌子: 表catproducts:

id  |  name  | parent_id
1   |  cat 1 |  null
2   |  cat 2 |  1

表产品:

id  |  name      |  cat_id  |  address_id
1   |  product1  |    2     | 7

表格地址

id  |  name
7   |  L.A

示例:cat 2是cat 1的子目录.product1属于cat 2。

现在我要列出cat 1目录中的所有产品,其中包含address_id = products.address.id,包括cat 2上的所有产品(cat 1的孩子) 也许我的描述很复杂,但我希望你们能提供帮助。谢谢!

这是我的代码,但它只列出了cat 2上的产品

public function index() {
        $this->autoRender = true;

        $catproduct = $this->Catproduct->find("all",array(
            'fields'=>array(
                'Catproduct.name',
                'Catproduct.slug',
                'Catproduct.id',
            ),
            'conditions'=>array(
                'Catproduct.status'=>1,
                'Catproduct.home'=>1,
            ),
            'order'=>'Catproduct.pos DESC, Catproduct.id DESC'
        ));
        //pr($catproduct);
        //die;
        foreach($catproduct as $key => $value){
            $catcon = $this->Catproduct->find("list",array(
            'fields'=>array(
                'Catproduct.id',
                'Catproduct.id',                
            ),
            'conditions'=>array(
                'Catproduct.status'=>1,
                'Catproduct.parent_id'=>$value["Catproduct"]["id"],
            )            
        ));

        $catproduct[$key]["Catproduct"]["id"]=$catcon;      
        }

        //pr($catcon);
        //die;

        foreach($catproduct as $key => $catproducts){
            $newProduct = $this->Product->find("all", array(
                'fields' => array(
                    'Product.slug',
                    'Product.name',
                    'Product.price',                    
                    'Product.images',
                    'Product.pricetype',
                    'Product.livingspace',
                    'Province.name',
                    'Subprovince.name'
                ),              
                'conditions' => array(
                    'Product.status' => 1,
                    'Product.cat_id' => $catproducts["Catproduct"]["id"],
                ),
                'joins' => array(
                    array(
                        'table' => 'provinces',
                        'alias' => 'Province',
                        'type' => 'left outer',
                        'conditions'=> array('Province.id = Product.province_list')
                    ),
                    array(
                        'table' => 'subprovinces',
                        'alias' => 'Subprovince',
                        'type' => 'left outer',
                        'conditions'=> array('Subprovince.id = Product.subProvince')
                    )
                ),
                'order'=> 'Product.id DESC, Product.pos DESC, Product.created',
                'limit'=>'8'
            ));
            $catproduct[$key]["Catproduct"]["con"] = $newProduct;
        }
        $this->set('newProduct', $catproduct);  
        //pr($catproduct);
        //die;
    }

0 个答案:

没有答案