Created
June 16, 2015 12:22
-
-
Save benjaminrau/3d219c9adff1eaa29fa1 to your computer and use it in GitHub Desktop.
TYPO3 Flow IN-Contraint needs array with Persistence Identifiers as $operand due to an Doctrine bug. You are not able to pass a ArrayCollection or other collection containing objects right now. The following helper would replace an operand containing objects with an array containg the identifiers only!
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
Succeeds: | |
$portionDemandFrom = $q->matching( | |
$q->logicalAnd( | |
$q->lessThanOrEqual('startingFromPersonCount', $this->personCountRange['from']), | |
$q->in('productCategory', $this->generalUtility->prepareOperandForInExpression($this->getProduct()->getCategories())) | |
) | |
)->execute()->getFirst(); | |
Following example would fail: | |
$portionDemandFrom = $q->matching( | |
$q->logicalAnd( | |
$q->lessThanOrEqual('startingFromPersonCount', $this->personCountRange['from']), | |
$q->in('productCategory', $this->getProduct()->getCategories()) | |
) | |
)->execute()->getFirst(); | |
Following example would also fail (with different error message: | |
$portionDemandFrom = $q->matching( | |
$q->logicalAnd( | |
$q->lessThanOrEqual('startingFromPersonCount', $this->personCountRange['from']), | |
$q->in('productCategory', $this->getProduct()->getCategories()->toArray()) | |
) | |
)->execute()->getFirst(); |
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
<?php | |
namespace Cf\Shop\Utility; | |
/* * | |
* This script belongs to the TYPO3 Flow package "Cf.Shop". * | |
* */ | |
use Doctrine\Common\Collections\Collection; | |
use TYPO3\Flow\Annotations as Flow; | |
/** | |
*/ | |
class GeneralUtility { | |
/** | |
* @var \TYPO3\Flow\Object\ObjectManagerInterface | |
* @Flow\Inject | |
*/ | |
protected $objectManager; | |
/** | |
* @param mixed $operand | |
* @return array | |
*/ | |
public function prepareOperandForInExpression($operand) { | |
if ($operand instanceof Collection && 0 < $operand->count()) { | |
$collection = $operand; | |
$operand = array(); | |
$persistenceManager = $this->objectManager->get('TYPO3\Flow\Persistence\Generic\PersistenceManager'); | |
foreach ($collection AS $object) { | |
$operand[] = $persistenceManager->getIdentifierByObject($object); | |
} | |
} | |
return $operand; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment