Development

HowtoSelectDistinct (diff)

You must first sign up to be able to contribute.

Changes between Version 5 and Version 6 of HowtoSelectDistinct

Show
Ignore:
Author:
tom_ee (IP: 212.159.118.181)
Timestamp:
05/10/08 14:46:04 (10 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HowtoSelectDistinct

    v5 v6  
    3232 
    3333autant pour moi 
     34 
     35== DISTINCT results with Propel == 
     36 
     37You cannot hydrate objects from your models with just one column, but you can use the doSelectRS() function to return a resultset: 
     38{{{ 
     39<?php 
     40 
     41function getDistinctImageTypes()  
     42{ 
     43  $c = new Criteria(); 
     44  $c->clearSelectColumns();                      // remove all columns 
     45  $c->addSelectColumn(ImagesPeer::TYPE);         // use your own peer + column here, obviously ;-) 
     46  $c->setDistinct();                             // add DISTINCT to the query 
     47  $res = ImagesPeer::doSelectRS($c);             // the doSelectRS function from your peer 
     48  $data = array(); 
     49  while ($res->next()) // loop through the records 
     50  { 
     51    $data[] = $res->getString(1);                // fill up an array with the 1st column values 
     52  } 
     53  return $data;                                  // send it back 
     54} 
     55 
     56?> 
     57}}} 
     58The SQL generated:  
     59{{{ 
     60SELECT DISTINCT images.TYPE FROM images 
     61}}} 
     62 
     63The value returned by the function:  
     64{{{ 
     65Array 
     66( 
     67   [0] => 1 
     68   [1] => 2 
     69) 
     70}}} 
     71 
     72Another example, with a join to get the type of image rather than the id of the type: 
     73{{{ 
     74<?php 
     75 
     76function getDistinctImageTypes()  
     77{ 
     78  $c = new Criteria(); 
     79  $c->clearSelectColumns(); 
     80  $c->addSelectColumn(ImagesPeer::TYPE); 
     81  $c->addSelectColumn(ImagetypesPeer::TYPE);     // the table column that we want from the join 
     82  $c->addJoin(ImagesPeer::TYPE, ImagetypesPeer::ID, Criteria::LEFT_JOIN); 
     83  $c->setDistinct(); 
     84  $res = ImagesPeer::doSelectRS($c); 
     85  $data = array(); 
     86  while ($res->next()) 
     87  { 
     88    $data[] = $res->getString(2);                // fill up an array with the 2nd column values 
     89  } 
     90  return $data; 
     91} 
     92 
     93?> 
     94}}} 
     95The SQL generated:  
     96{{{ 
     97SELECT DISTINCT images.TYPE, imagetypes.TYPE FROM images LEFT JOIN imagetypes ON (images.TYPE=imagetypes.ID) 
     98}}} 
     99 
     100The value returned by the function:  
     101{{{ 
     102Array 
     103( 
     104   [0] => site 
     105   [1] => plot 
     106) 
     107}}} 
     108Hope it helps :-)