Created
August 22, 2016 11:32
-
-
Save Schweriner/3aa345c636308cf3c72d1a43cca5b4ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Dispatches magic methods (findBy[Property]()) | |
* http://etobi.de/blog/2011/10/multi-property-magic-finder/ | |
* @param string $methodName The name of the magic method | |
* @param string $arguments The arguments of the magic method | |
* @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException | |
* @return void | |
* @api | |
*/ | |
public function __call($methodName, $arguments) { | |
if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) { | |
$query = $this->createQueryFromConstraintString(substr($methodName, 6), $arguments); | |
return $query->execute(); | |
} elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) { | |
$query = $this->createQueryFromConstraintString(substr($methodName, 9), $arguments); | |
$object = $query->setLimit(1)->execute()->getFirst(); | |
return $object; | |
} elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) { | |
$query = $this->createQueryFromConstraintString(substr($methodName, 7), $arguments); | |
$result = $query->execute()->count(); | |
return $result; | |
} | |
throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480); | |
} | |
/** | |
* http://etobi.de/blog/2011/10/multi-property-magic-finder/ | |
* @param string $constrainString | |
* @param array $arguments | |
* @return \TYPO3\CMS\Extbase\Persistence\QueryInterface | |
*/ | |
protected function createQueryFromConstraintString($constraintString, $arguments) { | |
$query = $this->createQuery(); | |
$constraints = array(); | |
$argumentPointer = 0; | |
if (strstr($constraintString, 'And')) { | |
foreach (explode('And', $constraintString) as $propertyName) { | |
$propertyName = strtolower(substr($propertyName, 0, 1)) . substr($propertyName, 1); | |
$constraints[] = $query->equals($propertyName, $arguments[$argumentPointer]); | |
$argumentPointer++; | |
} | |
$query->matching($query->logicalAnd($constraints)); | |
} elseif (strstr($constraintString, 'Or')) { | |
foreach (explode('Or', $constraintString) as $propertyName) { | |
$propertyName = strtolower(substr($propertyName, 0, 1)) . substr($propertyName, 1); | |
$constraints[] = $query->equals($propertyName, $arguments[$argumentPointer]); | |
$argumentPointer++; | |
} | |
$query->matching($query->logicalOr($constraints)); | |
} else { | |
$propertyName = strtolower(substr($constraintString, 0, 1)) . substr($constraintString, 1); | |
$query->matching($query->equals($propertyName, $arguments[0])); | |
} | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment