Cakephp查询无法呈现正确的数据

时间:2013-10-24 09:27:28

标签: php mysql cakephp

我在cakephp中是全新的,并且在查询时提取数据

我试过找出类别/仓库表格信息但失败了..

    $cart_products = $this->Order->OrdersProduct->find('all', array(
                'fields' => array('*'),
                'contain' => array('Category'),
                'joins' => array(
                    array(
                        'table' => 'products',
                        'alias' => 'Product',
                        'type' => 'LEFT',
                        'conditions' => array('Product.id = OrdersProduct.product_id')
                    ),
                    array(
                        'table' => 'orders',
                        'alias' => 'Order',
                        'type' => 'LEFT',
                        'conditions' => array('Order.id = OrdersProduct.order_id')
                    )
                ),
                'conditions' => array(
                    'Order.store_id' => $store_id,
                    'Order.order_status' => 'in_cart'
                )
            ));

I need the result something like this...
Array
(
    [0] => Array
        (
            [OrdersProduct] => Array
                (
                    [id] => 1
                    [order_id] => 1
                    [product_id] => 16
                    [qty] => 10
                    [created] => 2013-10-24 08:04:33
                    [modified] => 2013-10-24 08:04:33
                )

            [Product] => Array
                (
                    [id] => 16
                    [part] => 56-987xyz
                    [title] => iPhone 5 battery
                    [description] => iPhone 5c description
                    [wholesale_price] => 4
                    [retail_price] => 8
                    [purchase_cost] => 2
                    [sort_order] => 
                    [Category] => array(
                           [id] => 1,
                           [name] => Iphone 5
                    )
                    [Warehouse] => array(
                           [id] => 1,
                           [name] => Warehouse1
                    )
                    [weight] => 
                    [created] => 2013-10-22 12:14:57
                    [modified] => 2013-10-22 12:14:57
                )
      )
)

我怎样才能找到这个?有谁能够帮我?感谢

2 个答案:

答案 0 :(得分:0)

您应该在其模型文件中的每个表之间设置关系,而不是在查询中进行连接。创建这些关系后,只需在正常情况下对一个表进行查找即可收集相关数据。

关系设置简单,但特定于您的表结构,所以我建议您在这里阅读它们并自行决定哪些表之间需要关系:

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

答案 1 :(得分:0)

为您的表定义正确的模型关联,并尝试这样的事情:

$this->Order->Behaviors->attach('containable');
$cart_products = $this->Order->OrdersProduct->find('all', array(                
                'contain' => array('Product', 'Product.Category', 'Product.Warehouse'),                
                'conditions' => array(
                    'Order.store_id' => $store_id,
                    'Order.order_status' => 'in_cart'
                )
            ));

模型关联参考:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

可包含行为的参考:http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html