简体   繁体   中英

how to create the find all condition in one field on pagination in cakephp?

I want to find all contact groups name related to contact_id in pagination, i tried a lot but didn't get success, i have many to many relation, i just want to display all contact groups name in pagination against contact_id, how can i do that? if any body's can help, it will be appreciated, thanks in advance.

this is my pagination code

            $this->paginate = array(
                    'fields' => array(
                                    'DISTINCT Contact.contact_id',
                                    'Contact.first_name',
                                    'Contact.last_name',
                                    'Contact.email',
                                    'Contact.created',
                                    'ContactGroup.name',

                                        ),  
                    'conditions' => array(
                                    $this->conditions,
                                    $query,
                                    array(
                                        'OR' => array(
                                                    array('Contact.first_name LIKE' => "$keyword%"),
                                                    array('Contact.last_name LIKE'  => "$keyword%"),
                                                    array('Contact.middle_name LIKE'=> "$keyword%")
                                                    )
                                            )                       
                                        ),
                    'limit'      => 5,
                    'group'      => array('Contact.contact_id'),

                    );

I am not sure what you are asking here. But by assumption I have come up with this

You should not be using both DISTINCT and GROUP BY together

$this->paginate = array(
       'fields' => array(
             'Contact.first_name',
             'Contact.last_name',
             'Contact.email',
             'Contact.created',
       ),  
       'conditions' => array(
           'OR' => array(
             'Contact.first_name LIKE' => $keyword. "%",
             'Contact.last_name LIKE'  => $keyword. "%",
             'Contact.middle_name LIKE'=> $keyword. "%"
            )
        ),
        'limit' => 5,
        'group' => array('Contact.contact_id'),
        'contain' => array('ContactGroup.name')
);

I am not sure what is $this->conditions and $query.

PS, make sure you have this line in your Model to include the Containable Behavior

public $actsAs = array('Containable'/*, other behaviors */);

OR you can include the containable behavior on the fly from the controller before $this->paginate

$this->Post->Behaviors->load('Containable');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM