Changeset 1647

Show
Ignore:
Timestamp:
02/26/10 17:29:09 (5 months ago)
Author:
klemens
Message:

Fixed ullQuery + Test

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/plugins/ullCorePlugin/lib/generator/ullQuery.class.php

    r1611 r1647  
    55 * It allows giving related columns in the ull-relation syntax relative to a given base model. 
    66 *  
    7  * Example for ullVentoryItem: 'UllUser->username' selects the username of an item's owner 
     7 * Example for the base class ullVentoryItem: 'UllUser->username' selects the username of an item's owner 
    88 *  
    99 * It automatically adds the necessary "from" clauses (=joins) and it administrates 
     
    3737    $this->q = new ullDoctrineQuery(); 
    3838     
    39     $from = $this->baseModel . ' x'; 
    40     $from .= ($indexBy) ? (' INDEXBY ' . $indexBy) : ''; 
    41     $this->q->from($from); 
    42      
     39    $doctrineFromString = $this->baseModel . ' x'; 
     40    $doctrineFromString .= ($indexBy) ? (' INDEXBY ' . $indexBy) : ''; 
     41    $this->q->from($doctrineFromString); 
     42     
     43    // add discriminator column for base model 
    4344    $inheritanceKeyFields = $this->getInheritanceKeyField($this->baseModel); 
    4445    if ($inheritanceKeyFields != null) 
     
    4647      $this->addSelect($inheritanceKeyFields); 
    4748    } 
    48   } 
    49    
    50   /** 
    51    * Inspect the inheritance map of a model and 
    52    * retrieve keyFields, if any. 
    53    * @param unknown_type $modelName 
    54    * @return array keyFields, if there are none, null 
    55    */ 
    56   private function getInheritanceKeyField($modelName) 
    57   { 
    58     $inheritanceMap = Doctrine::getTable($modelName)->getOption('inheritanceMap'); 
    59     $inheritanceFieldKeys = array_keys($inheritanceMap); 
    60     if (count($inheritanceFieldKeys) > 0) 
    61     { 
    62       return $inheritanceFieldKeys; 
    63     } 
    64     else 
    65     { 
    66       return null; 
    67     }  
    68   } 
     49     
     50    // add indexby column 
     51    if ($indexBy) 
     52    { 
     53      $this->addSelect($indexBy); 
     54    } 
     55  } 
     56   
    6957   
    7058  /****************************************** 
     
    7260   ******************************************/ 
    7361   
    74    
    7562  /** 
    7663   * Add SELECT columns 
     
    130117  { 
    131118    $this->handleWhere($where, $params, true); 
    132      
    133     return $this; 
    134   } 
    135    
    136   /** 
    137    * Internal function which handles adding where clauses to 
    138    * the query; supports AND and OR. 
    139    *  
    140    * @param $where the where term to add 
    141    * @param $params 
    142    * @param $coordinatorIsOr true if OR, false if AND 
    143    * @return self 
    144    */ 
    145   protected function handleWhere($where, $params = array(), $coordinatorIsOr) 
    146   { 
    147     preg_match('/^([a-z>_-])+/i', $where, $matches); 
    148     $search = $matches[0]; 
    149     $replace = $this->relationStringToDoctrineQueryColumn($search); 
    150  
    151     $where = str_replace($search, $replace, $where); 
    152      
    153     if ($coordinatorIsOr) 
    154     { 
    155       $this->q->orWhere($where, $params); 
    156     } 
    157     else 
    158     { 
    159       $this->q->addWhere($where, $params); 
    160     } 
    161      
    162     $this->addRelationsToQuery(); 
    163119     
    164120    return $this; 
     
    287243  }   
    288244   
     245  /** 
     246   * SQL limit funtion 
     247   *  
     248   * @param int $limit 
     249   * @return self 
     250   */ 
     251  public function limit($limit) 
     252  { 
     253    $this->q->limit($limit); 
     254     
     255    return $this; 
     256  } 
     257   
    289258   
    290259  /** 
     
    319288    return $this->q->count($params); 
    320289  }     
    321    
    322   //TODO: add a indexBy($column) method. 
    323   //@see  http://www.doctrine-project.org/documentation/manual/1_2/pl/dql-doctrine-query-language:indexby-keyword 
    324   //It should work this way:  
    325   //  In ullQuery we have no addFrom, since this is handled automatically 
    326   //  $q->indexBy('slug') 
    327   //   
    328        
    329290   
    330291   
     
    391352   * Also registeres the necessary relations 
    392353   *  
    393    * Example for TestTable: 'UllUser->username' 
     354   * Example for base model TestTable: 'UllUser->username' 
    394355   * Returns 'x.ulluser.username' and adds the relation to UllUser 
    395356   *  
     
    482443   
    483444  /** 
    484    * Add the internally collected relations to the query 
    485    *  
    486    * @param string $alias 
    487    * @param array $relations 
     445   * Adds the internally collected relations to the query 
     446   *  
     447   * @param string $alias     Doctrine base alias 
     448   * @param array $relations  Tree of relations relative to the base model  
     449   * Example: 
     450   *  
     451   * array(2) { 
     452   *  ["UllLocation"]=> 
     453   *  array(1) { 
     454   *    ["Translation"]=> 
     455   *    array(0) { 
     456   *    } 
     457   *  } 
     458   * ["UllJobTitle"]=> 
     459   *  array(0) { 
     460   *  } 
     461   *} 
     462   * @param $baseModel        Class name of the base model 
     463   *  
    488464   * @return none 
    489465   */ 
    490   public function addRelationsToQuery($alias = 'x', $relations = array(), $fromModel = null) 
    491   { 
    492     if (!count($relations)) 
     466  public function addRelationsToQuery($alias = 'x', $relations = null, $baseModel = null) 
     467  { 
     468    if ($relations === null) 
    493469    { 
    494470      $relations = $this->relations; 
    495471    } 
    496472     
    497     if (!$fromModel) 
    498     { 
    499       $fromModel = $this->getBaseModel(); 
    500     }     
    501      
     473    if (!is_array($relations)) 
     474    { 
     475      throw new InvalidArgumentException('parameter "relations" must be an array'); 
     476    } 
     477     
     478    if (!$baseModel) 
     479    { 
     480      $baseModel = $this->getBaseModel(); 
     481    } 
     482 
    502483    foreach ($relations as $relation => $subRelations) 
    503484    { 
    504485      $newAlias = $alias . '_' . $this->relationStringToAlias($relation, false); 
    505486       
    506       $from = $alias . '.' . $relation . ' ' . $newAlias; 
     487      $doctrineFromString = $alias . '.' . $relation . ' ' . $newAlias; 
    507488       
    508489      $fromParts = $this->q->getDqlPart('from'); 
    509490       
    510       $doctrineRelation = Doctrine::getTable($fromModel)->getRelation($relation); 
    511        
    512       if (!in_array($from, $fromParts)) 
     491      $doctrineRelation = Doctrine::getTable($baseModel)->getRelation($relation); 
     492       
     493      if (!in_array($doctrineFromString, $fromParts)) 
    513494      {  
    514         $this->q->addFrom($from); 
    515          
     495        $this->q->addFrom($doctrineFromString); 
     496         
     497        // add discriminator column 
    516498        $inheritanceKeyFields = $this->getInheritanceKeyField($doctrineRelation->getClass()); 
    517499         
     
    524506        } 
    525507         
     508        // Hardcoded workaround for UllUser because we can't detect the discriminator column for the parent entity 
    526509        if ($doctrineRelation->getClass() == 'UllEntity') 
    527510        { 
     
    530513      } 
    531514       
    532       // This is necessary for Doctrine joins: 
    533       $selectId = $alias . '.' . $doctrineRelation->getLocalColumnName(); 
    534       $selectParts = $this->q->getDqlPart('select'); 
    535       if (!in_array($selectId, $selectParts)) 
    536       { 
    537         $this->q->addSelect($selectId); 
    538       } 
    539      
     515      // Select the local identifiers. This is necessary for Doctrine joins: 
     516      $identifiers = Doctrine::getTable($baseModel)->getIdentifier(); 
     517 
     518      if (!is_array($identifiers)) 
     519      { 
     520        $identifiers = array($identifiers); 
     521      } 
     522       
     523      foreach ($identifiers as $identifier) 
     524      { 
     525        $selectId = $alias . '.' .  $identifier; 
     526         
     527        $selectParts = $this->q->getDqlPart('select'); 
     528        if (!in_array($selectId, $selectParts)) 
     529        { 
     530          $this->q->addSelect($selectId); 
     531        } 
     532      } 
     533     
     534      // Call the method recursivly for subrelations 
    540535      if (count($subRelations)) 
    541536      { 
    542         $doctrineRelation = Doctrine::getTable($fromModel)->getRelation($relation); 
    543          
    544537        $this->addRelationsToQuery($newAlias, $subRelations, $doctrineRelation->getClass()); 
    545538      } 
    546539    } 
    547540  } 
     541   
     542   
     543  /** 
     544   * Inspect the inheritance map of a model and 
     545   * retrieve keyFields, if any. 
     546   *  
     547   * @param unknown_type $modelName 
     548   * @return array keyFields, if there are none, null 
     549   */ 
     550  protected function getInheritanceKeyField($modelName) 
     551  { 
     552    $inheritanceMap = Doctrine::getTable($modelName)->getOption('inheritanceMap'); 
     553    $inheritanceFieldKeys = array_keys($inheritanceMap); 
     554    if (count($inheritanceFieldKeys) > 0) 
     555    { 
     556      return $inheritanceFieldKeys; 
     557    } 
     558    else 
     559    { 
     560      return null; 
     561    }  
     562  }   
     563   
     564   
     565  /** 
     566   * Internal function which handles adding where clauses to 
     567   * the query; supports AND and OR. 
     568   *  
     569   * @param $where the where term to add 
     570   * @param $params 
     571   * @param $coordinatorIsOr true if OR, false if AND 
     572   * @return self 
     573   */ 
     574  protected function handleWhere($where, $params = array(), $coordinatorIsOr) 
     575  { 
     576    preg_match('/^([a-z>_-])+/i', $where, $matches); 
     577    $search = $matches[0]; 
     578    $replace = $this->relationStringToDoctrineQueryColumn($search); 
     579 
     580    $where = str_replace($search, $replace, $where); 
     581     
     582    if ($coordinatorIsOr) 
     583    { 
     584      $this->q->orWhere($where, $params); 
     585    } 
     586    else 
     587    { 
     588      $this->q->addWhere($where, $params); 
     589    } 
     590     
     591    $this->addRelationsToQuery(); 
     592     
     593    return $this; 
     594  }   
    548595   
    549596   
  • trunk/test/unit/ullCorePlugin/ullQueryTest.php

    r1610 r1647  
    99sfLoader::loadHelpers('I18N'); 
    1010 
    11 $t = new myTestCase(21, new lime_output_color, $configuration); 
     11$t = new myTestCase(24, new lime_output_color, $configuration); 
    1212$path = sfConfig::get('sf_root_dir') . '/plugins/ullCorePlugin/data/fixtures/'; 
    1313$t->setFixturesPath($path); 
     
    1616$t->begin('__construct()'); 
    1717  $q = new ullQuery('TestTable'); 
     18 
     19   
     20$t->begin('__construct() with INDEXBY | Also tests limit()'); 
     21  $q = new ullQuery('UllUserStatus', 'slug'); 
     22  $q->addSelect('is_absent'); 
     23  $q->limit(1); 
     24  $t->is( 
     25    $q->getSqlQuery(), 
     26    "SELECT u.id AS u__id, u.slug AS u__slug, u.is_absent AS u__is_absent FROM ull_user_status u LIMIT 1", 
     27    'Created the correct SQL query' 
     28  ); 
     29  $t->is( 
     30    $q->execute(array(), DOCTRINE::HYDRATE_ARRAY),  
     31    array('active' => array('id' => 1, 'slug' => 'active', 'is_absent' => false)),  
     32    'Returns an indexed by array' 
     33  ); 
    1834   
    1935   
     
    2945$t->diag('relationStringToAlias()'); 
    3046  $t->is( 
    31     $q->relationStringToAlias('UllUser->UllLocation->Translation'), 
    32     'x_ulluser_ulllocation_translation', 
     47    $q->relationStringToAlias('UllUser->UllUserStatus->Translation'), 
     48    'x_ulluser_ulluserstatus_translation', 
    3349    'Returns the correct string' 
    3450  ); 
    3551  $t->is( 
    36     $q->relationStringToAlias('UllUser->UllLocation', false), 
    37     'ulluser_ulllocation', 
     52    $q->relationStringToAlias('UllUser->UllUserStatus', false), 
     53    'ulluser_ulluserstatus', 
    3854    'Returns the correct string with option prependBaseAlias = false' 
    3955  );     
     
    4763$t->diag('nestPlainArray()'); 
    4864  $t->is( 
    49     $q->nestPlainArray(array('UllUser', 'UllLocation', 'Translation')), 
    50     array('UllUser' => array('UllLocation' => array('Translation' => array()))), 
     65    $q->nestPlainArray(array('UllUser', 'UllUserStatus', 'Translation')), 
     66    array('UllUser' => array('UllUserStatus' => array('Translation' => array()))), 
    5167    'Creates the correct nested array' 
    5268  ); 
     
    5470   
    5571$t->diag('addRelations / getRelations()'); 
    56   $q->addRelations(array('UllUser', 'UllLocation', 'Translation')); 
     72  $q->addRelations(array('UllUser', 'UllUserStatus', 'Translation')); 
    5773  $t->is( 
    5874    $q->getRelations(), 
    59     array('UllUser' => array('UllLocation' => array('Translation' => array()))), 
     75    array('UllUser' => array('UllUserStatus' => array('Translation' => array()))), 
    6076    'Returns the correct relations' 
    6177  ); 
     
    6884      'UllUser' =>  
    6985        array( 
    70           'UllLocation' =>  
     86          'UllUserStatus' =>  
    7187            array( 
    7288              'Translation' => array() 
     
    87103      0 => 'TestTable x', 
    88104      1 => 'x.UllUser x_ulluser', 
    89       2 => 'x_ulluser.UllLocation x_ulluser_ulllocation', 
    90       3 => 'x_ulluser_ulllocation.Translation x_ulluser_ulllocation_translation', 
     105      2 => 'x_ulluser.UllUserStatus x_ulluser_ulluserstatus', 
     106      3 => 'x_ulluser_ulluserstatus.Translation x_ulluser_ulluserstatus_translation', 
    91107      4 => 'x_ulluser.UllJobTitle x_ulluser_ulljobtitle', 
    92108      5 => 'x.Creator x_creator', 
     
    107123  $t->is( 
    108124    $q->getSqlQuery(), 
    109     "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' WHERE (t2.lang = ? AND u3.lang = ?)", 
     125    "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u.type AS u__type, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, u4.type AS u4__type, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' WHERE (t2.lang = ? AND u3.lang = ?)", 
    110126    'Returns the correct query' 
    111127  ); 
     
    116132  $t->is( 
    117133    $q->getSqlQuery(), 
    118     "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' LEFT JOIN ull_location u5 ON u.ull_location_id = u5.id WHERE (t2.lang = ? AND u3.lang = ? AND t2.lang = ? AND u3.lang = ?) ORDER BY t2.my_string asc, u3.name desc, u5.name asc", 
    119     'Returns the correct query' 
    120   ); 
     134    "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u.type AS u__type, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, u4.type AS u4__type, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' LEFT JOIN ull_location u5 ON u.ull_location_id = u5.id WHERE (t2.lang = ? AND u3.lang = ? AND t2.lang = ? AND u3.lang = ?) ORDER BY t2.my_string asc, u3.name desc, u5.name asc", 
     135    'Returns the correct query' 
     136  ); 
     137 
    121138   
    122139$t->diag('addOrderByPrefix()'); 
     
    124141  $t->is( 
    125142    $q->getSqlQuery(), 
    126     "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' LEFT JOIN ull_location u5 ON u.ull_location_id = u5.id WHERE (t2.lang = ? AND u3.lang = ? AND t2.lang = ? AND u3.lang = ?) ORDER BY t.my_email asc, u5.short_name asc, t2.my_string asc, u3.name desc, u5.name asc", 
    127     'Returns the correct query' 
    128   ); 
     143    "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u.type AS u__type, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, u4.type AS u4__type, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' LEFT JOIN ull_location u5 ON u.ull_location_id = u5.id WHERE (t2.lang = ? AND u3.lang = ? AND t2.lang = ? AND u3.lang = ?) ORDER BY t.my_email asc, u5.short_name asc, t2.my_string asc, u3.name desc, u5.name asc", 
     144    'Returns the correct query' 
     145  ); 
     146 
    129147   
    130148$t->diag('addWhere()'); 
     
    133151  $t->is( 
    134152    $q->getSqlQuery(), 
    135     "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' LEFT JOIN ull_location u5 ON u.ull_location_id = u5.id WHERE (t2.lang = ? AND u3.lang = ? AND t2.lang = ? AND u3.lang = ? AND t.my_email = ? AND u3.lang = ? AND u3.name = ?) ORDER BY t.my_email asc, u5.short_name asc, t2.my_string asc, u3.name desc, u5.name asc", 
     153    "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u.type AS u__type, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, u4.type AS u4__type, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' LEFT JOIN ull_location u5 ON u.ull_location_id = u5.id WHERE (t2.lang = ? AND u3.lang = ? AND t2.lang = ? AND u3.lang = ? AND t.my_email = ? AND u3.lang = ? AND u3.name = ?) ORDER BY t.my_email asc, u5.short_name asc, t2.my_string asc, u3.name desc, u5.name asc", 
    136154    'Returns the correct query' 
    137155  ); 
     
    142160  $t->is( 
    143161    $q->getSqlQuery(), 
    144     "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' LEFT JOIN ull_location u5 ON u.ull_location_id = u5.id WHERE (t2.lang = ? AND u3.lang = ? AND t2.lang = ? AND u3.lang = ? AND t.my_email = ? AND u3.lang = ? AND u3.name = ? OR t.my_email = ? AND u3.lang = ? OR u3.name = ?) ORDER BY t.my_email asc, u5.short_name asc, t2.my_string asc, u3.name desc, u5.name asc", 
    145     'Returns the correct query' 
    146   ); 
     162    "SELECT t.id AS t__id, t.my_email AS t__my_email, t2.id AS t2__id, t2.lang AS t2__lang, t2.my_string AS t2__my_string, u.id AS u__id, u.username AS u__username, u.type AS u__type, u2.id AS u2__id, u3.id AS u3__id, u3.lang AS u3__lang, u3.name AS u3__name, u4.id AS u4__id, u4.username AS u4__username, u4.type AS u4__type, CONCAT('Write an email to ', t.my_email) AS t__0 FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id LEFT JOIN ull_entity u4 ON t.creator_user_id = u4.id AND u4.type = 'user' LEFT JOIN ull_location u5 ON u.ull_location_id = u5.id WHERE (t2.lang = ? AND u3.lang = ? AND t2.lang = ? AND u3.lang = ? AND t.my_email = ? AND u3.lang = ? AND u3.name = ? OR t.my_email = ? AND u3.lang = ? OR u3.name = ?) ORDER BY t.my_email asc, u5.short_name asc, t2.my_string asc, u3.name desc, u5.name asc", 
     163    'Returns the correct query' 
     164  ); 
     165 
    147166 
    148167  $t->isa_ok($q->execute(), 'Doctrine_Collection', 'Successfully executes the query'); 
     
    155174  $t->is( 
    156175    $q->getSqlQuery(), 
    157     "SELECT t.id AS t__id, u.id AS u__id, u2.id AS u2__id FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id WHERE (t2.lang = ? AND u3.lang = ? AND (t.my_email LIKE ? OR t2.my_string LIKE ? OR u.username LIKE ? OR u3.name LIKE ?))", 
    158     'Returns the correct query' 
    159   ); 
     176    "SELECT t.id AS t__id, u.id AS u__id, u.type AS u__type, u2.id AS u2__id FROM test_table t LEFT JOIN test_table_translation t2 ON t.id = t2.id LEFT JOIN ull_entity u ON t.ull_user_id = u.id AND u.type = 'user' LEFT JOIN ull_employment_type u2 ON u.ull_employment_type_id = u2.id LEFT JOIN ull_employment_type_translation u3 ON u2.id = u3.id WHERE (t2.lang = ? AND u3.lang = ? AND (t.my_email LIKE ? OR t2.my_string LIKE ? OR u.username LIKE ? OR u3.name LIKE ?))", 
     177    'Returns the correct query' 
     178  ); 
     179   
    160180   
    161181  $t->isa_ok($q->execute(), 'Doctrine_Collection', 'Successfully executes the query'); 
     
    167187//var_dump($q->execute()->toArray(false)); 
    168188 
     189$t->diag('count()'); 
     190  $q = new ullQuery('TestTable'); 
     191  $t->is($q->count(), 2, 'Returns the correct number of results'); 
     192 
     193   
     194$t->diag('addGroupBy()'); 
     195  $q = new ullQuery('UllUserStatus'); 
     196  $q->addSelect('COUNT(*)'); 
     197  $q->addGroupBy('is_absent'); 
     198   
     199  $t->is( 
     200    $q->getSqlQuery(), 
     201    "SELECT COUNT(*) AS u__0 FROM ull_user_status u GROUP BY u.is_absent", 
     202    'Returns the correct query' 
     203  ); 
     204   
    169205 
    170206$t->diag('Doctrine 1.0 bug resolved with UllUser and SELECT on >= 2 relations');   
     
    179215  $q->execute(); 
    180216   
    181 $t->diag('count()'); 
    182   $q = new ullQuery('TestTable'); 
    183   $t->is($q->count(), 2, 'Returns the correct number of results'); 
     217 
    184218 
    185219