i am working on a Cakephp 2.x i am newbie to cakephp and getting a very hard time to grasp the cake query methods i have implemented a join query but the distinct is not working here is my code
what actually i am trying to accomplish :
i have a table name Messages in which there are duplicate numbers in field Message.mobileNo ... i just want to pick the distinct numbers and then on the bases of those distinct numbers i am comparing or joining the contacts table in which there are mobileNo,workNo etc fields and checking that wether those numbers are in the contacts table or not .. if they are grab the contact name for me ..hope you understand it
so what i did is ... i am trying to get the rows of contacts table against the distnct numbers of Messages table..
function getRecentMessages($userid){
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'AND' =>
array(
array('OR' => array(
array('Message.mobileNo = Contact.mobileNo'),
array('Message.mobileNo = Contact.workNo'),
array('Message.mobileNo = Contact.homeNo'),
array('Message.mobileNo = Contact.other'),
)),
)
),
'type' => 'LEFT',
'order'=>'Message.idTextMessage DESC',
)
)
), false);
return $this->find('all', array('conditions' => array('Message.User_id' => $userid),
'contain' => array('Contact' ),
'fields' => array(' DISTINCT Message.mobileNo',//distnct not working
'Contact.mobileNo',
'Contact.workNo',
'Contact.homeNo',
'Contact.other',
'Contact.name',
'Message.dateTime',
'Message.type',
'Message.body'),
'limit' => 6));
}
i have tried this
DISTINCT (Message.mobileNo) as mobileNo
but didnt work too
i have tried
'group' => 'Message.mobileNo',
by using this it works but then the query is not getting the required results .. i mean the DESC condition not works then