Changeset 1616
- Timestamp:
- 06/02/08 18:36:36 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php
r1615 r1616 108 108 * @param stubCriterion $criterion 109 109 * @param string $entityClass entity class to find instances of 110 * @param string $orderBy optional overrule default order of entity 110 111 * @return stubDatabaseFinderResult list of instances of $entityClass found with $criterion 111 112 * @throws stubPersistenceException 112 113 */ 113 public function findByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass )114 public function findByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass, $orderBy = null) 114 115 { 115 116 if ($entityClass->hasAnnotation('Entity') === false) { … … 118 119 119 120 $setterMethodHelper = new stubSetterMethodHelper($entityClass); 120 $data = $this->fetchData($entityClass, $setterMethodHelper, $criterion );121 $data = $this->fetchData($entityClass, $setterMethodHelper, $criterion, null, $orderBy); 121 122 $finderResult = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper); 122 123 return $finderResult; … … 127 128 * 128 129 * @param string $entityClass entity class to find instances of 130 * @param string $orderBy optional overrule default order of entity 129 131 * @return stubDatabaseFinderResult list of instances of $entityClass found 130 132 * @throws stubPersistenceException 131 133 */ 132 public function findAll(stubBaseReflectionClass $entityClass )134 public function findAll(stubBaseReflectionClass $entityClass, $orderBy = null) 133 135 { 134 136 if ($entityClass->hasAnnotation('Entity') === false) { … … 137 139 138 140 $setterMethodHelper = new stubSetterMethodHelper($entityClass); 139 $data = $this->fetchData($entityClass, $setterMethodHelper );141 $data = $this->fetchData($entityClass, $setterMethodHelper, null, null, $orderBy); 140 142 $finderResult = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper); 141 143 return $finderResult; … … 149 151 * @param stubCriterion $criterion optional 150 152 * @param array $primaryKeys optional 153 * @param string $orderBy optional overrule default order of entity 151 154 * @return array 152 155 * @throws stubDatabaseFinderException 153 156 */ 154 protected function fetchData(stubBaseReflectionClass $entityClass, stubSetterMethodHelper $setterMethodHelper, stubCriterion $criterion = null, array $primaryKeys = null )157 protected function fetchData(stubBaseReflectionClass $entityClass, stubSetterMethodHelper $setterMethodHelper, stubCriterion $criterion = null, array $primaryKeys = null, $orderBy = null) 155 158 { 156 159 $select = $this->createSelect($entityClass, $setterMethodHelper, ((null === $primaryKeys) ? (array()) : ($primaryKeys))); … … 159 162 } 160 163 161 $entityAnnotation = $entityClass->getAnnotation('Entity'); 162 if ($entityAnnotation->hasDefaultOrder() === true) { 163 $select->orderBy($entityAnnotation->getDefaultOrder()); 164 if (null !== $orderBy) { 165 $select->orderBy($orderBy); 166 } else { 167 $entityAnnotation = $entityClass->getAnnotation('Entity'); 168 if ($entityAnnotation->hasDefaultOrder() === true) { 169 $select->orderBy($entityAnnotation->getDefaultOrder()); 170 } 164 171 } 165 172 trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php
r1615 r1616 197 197 198 198 /** 199 * test that finding data of an object with a criterion works as expected 200 * 201 * @test 202 */ 203 public function byCriterionOverruleOrderBy() 204 { 205 $mockCriterion = $this->getMock('stubCriterion'); 206 $mockCriterion->expects($this->any())->method('toSQL')->will($this->returnValue('example')); 207 $mockResult = $this->getMock('stubDatabaseResult'); 208 $this->mockConnection->expects($this->any())->method('query')->will($this->returnValue($mockResult)); 209 $mockResult->expects($this->once()) 210 ->method('fetchAll') 211 ->will($this->returnValue(array(array('bar' => 'Here is bar.', 'default' => 'And this is default.')))); 212 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity'), 'blub DESC'); 213 $this->assertEquals(1, $finderResult->count()); 214 $data = $finderResult->current(); 215 $this->assertEquals('Here is bar.', $data->withAnnotation()); 216 $this->assertEquals('And this is default.', $data->withDefaultValue()); 217 $select = $this->mockQueryBuilder->getSelect(); 218 $this->assertEquals('foo', $select->getBaseTableName()); 219 $this->assertEquals('blub DESC', $select->getOrderedBy()); 220 $this->assertTrue($select->hasCriterion()); 221 } 222 223 /** 199 224 * test that finding data for all instances of an object works as expected 200 225 * … … 220 245 $this->assertFalse($select->hasCriterion()); 221 246 } 247 248 /** 249 * test that finding data for all instances of an object works as expected 250 * 251 * @test 252 */ 253 public function findAllOverruleOrderBy() 254 { 255 $mockResult = $this->getMock('stubDatabaseResult'); 256 $this->mockConnection->expects($this->any())->method('query')->will($this->returnValue($mockResult)); 257 $mockResult->expects($this->once()) 258 ->method('fetchAll') 259 ->will($this->returnValue(array(array('bar' => 'Here is bar.', 'default' => 'And this is default.')))); 260 $finderResult = $this->dbFinder->findAll(new stubReflectionClass('MockSinglePrimaryKeyEntity'), 'blub DESC'); 261 $this->assertEquals(1, $finderResult->count()); 262 $data = $finderResult->current(); 263 $this->assertEquals('Here is bar.', $data->withAnnotation()); 264 $this->assertEquals('And this is default.', $data->withDefaultValue()); 265 $select = $this->mockQueryBuilder->getSelect(); 266 $this->assertEquals('foo', $select->getBaseTableName()); 267 $this->assertEquals('blub DESC', $select->getOrderedBy()); 268 $this->assertFalse($select->hasCriterion()); 269 } 222 270 } 223 271 ?>
