2010-05-28 13 views
13

CakePHP 1.2 का उपयोग करके, मैं एक GROUP BY क्वेरी उत्पादन कोशिश कर रहा हूँ? अगर मैं मदद करता हूं तो मैं 'कंटेनएबल' का उपयोग कर रहा हूं।CakePHP और ग्रुप

+0

आप हमें दे सकते हैं अपने मॉडल और उनके रिश्ते कृपया? चूंकि यह जानने में मदद करेगा कि कौन से मॉडल आपके 'ढूंढें() ' –

उत्तर

25

यह है कि मैं क्या साथ eneded है

$params = 
     array(
     'conditions' => array('Model.field' => $thisValue), //array of conditions 
     'recursive' => 1, //int 
     //array of field names 
     'fields' => array('Model.field1', 'DISTINCT Model.field2'), 
     //string or array defining order 
     'order' => array('Model.created', 'Model.field3 DESC'), 
     'group' => array('Model.field'), //fields to GROUP BY 
     'joins' => array('Join relations here'), // for ex: LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id` 
     'limit' => n, //int 
     'page' => n, //int 
     'offset' => n, //int 
     'callbacks' => true //other possible values are false, 'before', 'after' 
    ); 

क्वेरी:

$resp = find('all', $params); 

debug($resp); 
+0

में एक दूसरे से पहले और एक दूसरे का अनुसरण करते हैं, मुझे' समूह 'परम, चीयर्स नहीं पता था! – Nicolas

+2

आपका स्वागत है। – webbiedave

8

Model->find() में group परम है। डिफ़ॉल्ट रूप से

$options = array(
        'conditions' => $conditions, 
        'fields'=>array('Category.*','COUNT(`Entity`.`id`) as `entity_count`'), 
        'joins' => array('LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`'), 
        'group' => '`Category`.`id`', 
        'contain' => array('Domain' => array('fields' => array('title'))) 
       ); 

       return $this->find('all', $options); 
5

संभव कुंजी है, जो सभी वैकल्पिक हैं::