0

I have problem in filtering records, I don't want any records have empty models.

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

I have checked a condition in Orderstatus table for selecting orderstatustype_id LIKE 5 which is Shipped.

I need only shipping records but its pulling all the records like the following

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

        )

)

I need to filter only the shipped records.. please help me..

msmohan
  • 27
  • 9

2 Answers2

0

It is possible to remove empty contains results from the paginate by including a join as well as the contain and checking for null values.

For example:-

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

This would ensure that only orders with order items would be returned.

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
0

This is the intended behaviour of Containable. When there are no records for the given model, an empty array is returned.

The only way to get rid of them is to loop over the resulting array and filter out the empty children. This article proposes a solution involving afterFind().

mensch
  • 4,411
  • 3
  • 28
  • 49
  • Thanks for your reply. I have a doubt that aftefind() will support paginate option because if I remove unwanted records in the afterfind function then how it will calculate next 10 records for next page? – msmohan Jan 05 '12 at 10:07
  • Is containable breaking pagination when used with the empty model arrays still present in the find data? – mensch Jan 05 '12 at 10:11
  • It's is? Because if it isn't I would just ignore the empty model arrays. – mensch Jan 05 '12 at 10:40
  • Paginate and containable working properly except unwanted records BUT find with contain breaking the pagination. – msmohan Jan 05 '12 at 10:41
  • Strange, containable and pagination are supposed to work in conjunction. Your problem might merit another question. – mensch Jan 05 '12 at 13:07
  • Really strange containable and paginate are not working in the scene i can't set the limit per page.. its not working.. – msmohan Jan 06 '12 at 04:57