Created
January 23, 2013 15:56
-
-
Save RafaelKa/4608575 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Flows\IdentityValidation\Controller; | |
/* * | |
* This script belongs to the TYPO3 Flow package "Flows.IdentityValidation".* | |
* * | |
* */ | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Mvc\Controller\ActionController; | |
use \Flows\IdentityValidation\Domain\Model\ModelCompoundIdentity; | |
/** | |
* ModelCompoundIdentity controller for the Flows.IdentityValidation package | |
* | |
* @Flow\Scope("singleton") | |
*/ | |
class ModelCompoundIdentityController extends ActionController { | |
/** | |
* @Flow\Inject | |
* @var \Flows\IdentityValidation\Domain\Repository\ModelCompoundIdentityRepository | |
*/ | |
protected $modelCompoundIdentityRepository; | |
/** | |
* Shows a list of model compound identities | |
* | |
* @return void | |
*/ | |
public function indexAction() { | |
$this->view->assign('modelCompoundIdentities', $this->modelCompoundIdentityRepository->findAll()); | |
} | |
/** | |
* Shows a single model compound identity object | |
* | |
* @param \Flows\IdentityValidation\Domain\Model\ModelCompoundIdentity $modelCompoundIdentity The model compound identity to show | |
* @return void | |
*/ | |
public function showAction(ModelCompoundIdentity $modelCompoundIdentity) { | |
$this->view->assign('modelCompoundIdentity', $modelCompoundIdentity); | |
} | |
/** | |
* Shows a form for creating a new model compound identity object | |
* | |
* @return void | |
*/ | |
public function newAction() { | |
} | |
/** | |
* Adds the given new model compound identity object to the model compound identity repository | |
* | |
* @param \Flows\IdentityValidation\Domain\Model\ModelCompoundIdentity $newModelCompoundIdentity A new model compound identity to add | |
* @Flow\Validate(argumentName="newModelCompoundIdentity", type="UniqueEntity", options={ "properties"="propertyNameOne" }) | |
* @return void | |
*/ | |
public function createAction(ModelCompoundIdentity $newModelCompoundIdentity) { | |
$this->modelCompoundIdentityRepository->add($newModelCompoundIdentity); | |
$this->addFlashMessage('Created a new model compound identity.'); | |
//\TYPO3\Flow\var_dump($this->arguments->getValidationResults()); | |
//$this->redirect('index'); | |
} | |
/** | |
* Shows a form for editing an existing model compound identity object | |
* | |
* @param \Flows\IdentityValidation\Domain\Model\ModelCompoundIdentity $modelCompoundIdentity The model compound identity to edit | |
* @return void | |
*/ | |
public function editAction(ModelCompoundIdentity $modelCompoundIdentity) { | |
$this->view->assign('modelCompoundIdentity', $modelCompoundIdentity); | |
} | |
/** | |
* Shows a form for editing an existing model compound identity object | |
* | |
* @param \Flows\IdentityValidation\Domain\Model\ModelCompoundIdentity $modelCompoundIdentity The model compound identity to edit | |
* @return void | |
*/ | |
public function editForNotAdminAction(ModelCompoundIdentity $modelCompoundIdentity) { | |
$this->view->assign('modelCompoundIdentity', $modelCompoundIdentity); | |
} | |
/** | |
* Updates the given model compound identity object | |
* | |
* @param \Flows\IdentityValidation\Domain\Model\ModelCompoundIdentity $modelCompoundIdentity The model compound identity to update | |
* @Flow\Validate(argumentName="modelCompoundIdentity", type="UniqueEntity") | |
* @return void | |
*/ | |
public function updateAction(ModelCompoundIdentity $modelCompoundIdentity) { | |
$this->modelCompoundIdentityRepository->update($modelCompoundIdentity); | |
$this->addFlashMessage('Updated the model compound identity.'); | |
$this->redirect('index'); | |
} | |
/** | |
* Removes the given model compound identity object from the model compound identity repository | |
* | |
* @param \Flows\IdentityValidation\Domain\Model\ModelCompoundIdentity $modelCompoundIdentity The model compound identity to delete | |
* @return void | |
*/ | |
public function deleteAction(ModelCompoundIdentity $modelCompoundIdentity) { | |
$this->modelCompoundIdentityRepository->remove($modelCompoundIdentity); | |
$this->addFlashMessage('Deleted a model compound identity.'); | |
$this->redirect('index'); | |
} | |
} | |
?> |
This file contains hidden or 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 TYPO3\Flow\Validation\Validator; | |
/* * | |
* This script belongs to the TYPO3 Flow framework. * | |
* * | |
* It is free software; you can redistribute it and/or modify it under * | |
* the terms of the GNU Lesser General Public License, either version 3 * | |
* of the License, or (at your option) any later version. * | |
* * | |
* The TYPO3 project - inspiring people to share! * | |
* */ | |
use TYPO3\Flow\Annotations as Flow, | |
TYPO3\Flow\Reflection\ObjectAccess, | |
TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException; | |
/** | |
* Validator for uniqueness of entities. | |
* | |
* @api | |
* @Flow\Scope("singleton") | |
*/ | |
class UniqueEntityValidator extends AbstractValidator { | |
/** | |
* @Flow\Inject | |
* @var \TYPO3\Flow\Reflection\ReflectionService | |
*/ | |
protected $reflectionService; | |
/** | |
* @Flow\Inject | |
* @var \TYPO3\Flow\Persistence\PersistenceManagerInterface | |
*/ | |
protected $persistenceManager; | |
/** | |
* @var array | |
*/ | |
protected $supportedOptions = array( | |
'properties' => array(NULL, 'name of the unique property or comma separated list for all names of compound properties', 'string') | |
); | |
/** | |
* Checks if the given property ($object) is a unique entity depending on it's identity properties. | |
* | |
* @param object $object The object that should be validated | |
* @return void | |
*/ | |
protected function isValid($object) { | |
echo \TYPO3\Flow\var_dump($this->options, '$this->options : ', true ); | |
$classSchema = $this->reflectionService->getClassSchema($object); | |
if ($classSchema->getModelType() !== \TYPO3\Flow\Reflection\ClassSchema::MODELTYPE_ENTITY) { | |
throw new InvalidValidationOptionsException('The object supplied for the UniqueEntityValidator must be an entity.', 1358454270); | |
} | |
$identityProperties = $classSchema->getIdentityProperties(); | |
if (count($identityProperties) === 0) { | |
throw new InvalidValidationOptionsException('The object supplied for the UniqueEntityValidator must have at least one identity property.', 1358459831); | |
} | |
$identifierProperties = $this->reflectionService->getPropertyNamesByAnnotation($classSchema->getClassName(), 'Doctrine\ORM\Mapping\Id'); | |
if (count($identifierProperties) > 1) { | |
throw new InvalidValidationOptionsException('The object supplied for the UniqueEntityValidator must only have one identifier property @ORM\Id.', 1358501745); | |
} | |
$identifierPropertyName = count($identifierProperties) > 0 ? array_shift($identifierProperties) : 'Persistence_Object_Identifier'; | |
$query = $this->persistenceManager->createQueryForType($classSchema->getClassName()); | |
$constraints = array($query->logicalNot($query->equals($identifierPropertyName, $this->persistenceManager->getIdentifierByObject($object)))); | |
foreach ($identityProperties as $propertyName => $propertyType) { | |
$constraints[] = $query->equals($propertyName, ObjectAccess::getProperty($object, $propertyName)); | |
} | |
if ($query->matching($query->logicalAnd($constraints))->count() > 0) { | |
$this->addError('Another entity with the same unique identifiers already exists', 1355785874); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment