cakePHP包含模型不应为空

时间:2012-01-05 07:24:48

标签: cakephp paginate containable

我在过滤记录时遇到问题,我不希望任何记录都有空模型。

       $this->paginate = array('fields' =>
            array('DISTINCT Joborder.id',
                'Joborder.job_type',
                'Joborder.payment_status',
                'Joborder.estimate_id',
                'Joborder.quantity',
                'Joborder.date_created'
            ),
            'contain' => array(
                'Workorder.id',
                'Workorder.joborder_id',
                'Joborderpart.id',
                'Joborderpart.binding',
                'Orderstatus.id',
                'Estimate' => array(
                    'Client' => array(
                        'fields' => array(
                            'Client.company_name'
                        )
                    )
                ),
                'Orderstatus' => array(
                    'fields' => array('Orderstatus.id'),
                    'order' => array(
                        'Orderstatus.id DESC'
                    ),
                    'conditions' => array(
                        'Orderstatus.Orderstatustype_id LIKE' => 5
                    ),
                    'Orderstatustype' => array(
                        'fields' => array(
                            'Orderstatustype.id',
                            'Orderstatustype.orderstatus'
                        )
                    ),
                    'limit' => 1
                )
            )
        );

我已经在Orderstatus表中检查了一个条件,用于选择发货的ordertatustype_id LIKE 5。

我只需要发送记录,但它会提取所有记录,如下所示

Array
(
    [0] => Array
        (
            [Joborder] => Array
                (
                    [id] => 1
                    [job_type] => postcard
                    [payment_status] => Billed
                    [estimate_id] => 4
                    [quantity] => 
                    [date_created] => 2011-12-12
                )

            [Estimate] => Array
                (
                    [id] => 4
                    [client_id] => 4
                    [custom_estimate] => 0
                    [custom_description] => 
                    [clientuser_id] => 40
                    [admin_id] => 3
                    [date_created] => 2012-01-01
                    [time_created] => 06:27:26 AM
                    [pricing_details] => 0
                    [wastesheet] => 12
                    [job_type] => postcard
                    [client_percentage] => 
                    [tax_rate] => 0.00
                    [Client] => Array
                        (
                            [company_name] => erte
                        )

                )

            [Workorder] => Array
                (
                    [id] => 1
                    [joborder_id] => 1
                )

            [Joborderpart] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [binding] => None
                            [joborder_id] => 1
                        )

                )

            [Orderstatus] => Array
                (
                )

        )

    [1] => Array
        (
            [Joborder] => Array
                (
                    [id] => 2
                    [job_type] => asdf
                    [payment_status] => 
                    [estimate_id] => 5
                    [quantity] => 
                    [date_created] => 2011-10-12
                )

            [Estimate] => Array
                (
                    [id] => 5
                    [client_id] => 4
                    [custom_estimate] => 0
                    [custom_description] => 
                    [clientuser_id] => 40
                    [admin_id] => 3
                    [date_created] => 2012-01-02
                    [time_created] => 06:29:03 AM
                    [pricing_details] => 0
                    [wastesheet] => 12
                    [job_type] => asdf
                    [client_percentage] => 
                    [tax_rate] => 0.00
                    [Client] => Array
                        (
                            [company_name] => erte
                        )

                )

            [Workorder] => Array
                (
                    [id] => 2
                    [joborder_id] => 2
                )

            [Joborderpart] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [binding] => None
                            [joborder_id] => 2
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [binding] => None
                            [joborder_id] => 2
                        )

                )

            [Orderstatus] => Array
                (
                    [0] => Array
                        (
                            [id] => 13
                            [orderstatustype_id] => 5
                            [joborder_id] => 2
                            [Orderstatustype] => Array
                                (
                                    [id] => 5
                                    [orderstatus] => Shipped
                                )

                        )

                )

        )

    [2] => Array
        (
            [Joborder] => Array
                (
                    [id] => 3
                    [job_type] => postcard
                    [payment_status] => 
                    [estimate_id] => 6
                    [quantity] => 
                    [date_created] => 2012-01-01
                )

            [Estimate] => Array
                (
                    [id] => 6
                    [client_id] => 4
                    [custom_estimate] => 0
                    [custom_description] => 
                    [clientuser_id] => 40
                    [admin_id] => 3
                    [date_created] => 2012-01-03
                    [time_created] => 06:30:30 AM
                    [pricing_details] => 1
                    [wastesheet] => 12
                    [job_type] => postcard
                    [client_percentage] => 
                    [tax_rate] => 0.00
                    [Client] => Array
                        (
                            [company_name] => erte
                        )

                )

            [Workorder] => Array
                (
                    [id] => 3
                    [joborder_id] => 3
                )

            [Joborderpart] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [binding] => None
                            [joborder_id] => 3
                        )

                    [1] => Array
                        (
                            [id] => 5
                            [binding] => None
                            [joborder_id] => 3
                        )

                )

            [Orderstatus] => Array
                (
                )

        )

)

我只需要过滤发货的记录..请帮帮我..

2 个答案:

答案 0 :(得分:0)

这是Containable的预期行为。如果给定模型没有记录,则返回一个空数组。

摆脱它们的唯一方法是遍历生成的数组并过滤掉空子。 This article提出了一个涉及afterFind()的解决方案。

答案 1 :(得分:0)

可以通过包含连接以及包含和检查空值来从分页中删除空包含结果。

例如: -

$this->paginate = array(
    'fields' => array(
        'DISTINCT Order.id',
        'Order.*'
    ),
    'conditions' => array(
        'OrderItem.id IS NOT NULL'
    ),
    'joins' => array(
        array(
            'table' => 'order_items',
            'alias' => 'OrderItem',
            'type' => 'LEFT',
            'conditions' => array('Order.id = OrderItem.order_id')
        )
    ),
    'contain' => array(
        'OrderItem'
    )
)

这将确保只返回包含订单商品的订单。