There is no easy way to get out of this situation. I finally went my own way and created a validator :
Entity
/**
* @ORM\Entity(repositoryClass="Ibiz\DoctrineExtensionsBundle\Entity\Repository\NestedTreeRepository")
* @Gedmo\Tree(type="nested")
* @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="uniq_url", columns={"parent_id", "name"})})
* @IbizAssert\UniquePath("getName")
*/
class Folder
{
/**
* @ORM\Column(type="string", nullable=false)
*/
protected $name;
public function getName()
{
return $this->name;
}
}
Validator/Constraints/UniquePath.php
namespace Ibiz\DoctrineExtensionsBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class UniquePath extends Constraint
{
public $em = null;
public $errorMethod = null;
public $message = 'The name "%name%" already exists.';
public $service = 'ibiz.validator.unique_path';
public function validatedBy()
{
return $this->service;
}
public function getRequiredOptions()
{
return array('errorMethod');
}
public function getDefaultOption()
{
return 'errorMethod';
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
Validator/Constraints/UniquePathValidator.php
namespace Ibiz\DoctrineExtensionsBundle\Validator\Constraints;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class UniquePathValidator extends ConstraintValidator
{
private $registry;
public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}
public function validate($entity, Constraint $constraint)
{
if ($constraint->errorMethod === null)
{
throw new ConstraintDefinitionException('ErrorMethod should be set');
} else if (!is_string($constraint->errorMethod)) {
throw new UnexpectedTypeException($constraint->errorMethod, 'string');
}
if ($constraint->em) {
$em = $this->registry->getManager($constraint->em);
} else {
$em = $this->registry->getManagerForClass(get_class($entity));
}
$className = $this->context->getClassName();
$repo = $em->getRepository($className);
$count = $repo->getSameNameSiblingsCount($entity);
if ($count != 0) {
$this->context->addViolation($constraint->message, array('%name%' => $entity->{$constraint->errorMethod}()));
}
}
}
Entity/Repository/NestedTreeRepository.php
namespace Ibiz\DoctrineExtensionsBundle\Entity\Repository;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository as BaseRepository;
class NestedTreeRepository extends BaseRepository
{
public function getSameNameSiblingsCountQueryBuilder($node)
{
$meta = $this->getClassMetadata();
if (!$node instanceof $meta->name) {
throw new InvalidArgumentException("Node is not related to this repository");
}
$config = $this->listener->getConfiguration($this->_em, $meta->name);
$qb = $this->_em->createQueryBuilder();
$qb->select($qb->expr()->count('n.id'))
->from($config['useObjectClass'], 'n');
if ($node->getParent() === null) {
$qb->where($qb->expr()->andx(
$qb->expr()->eq('n.name', ':name'),
$qb->expr()->isNull('n.parent')
))
->setParameters(array(
'name' => $node->getName(),
));
} else {
$qb->leftJoin('n.parent', 'p')
->where($qb->expr()->andx(
$qb->expr()->eq('n.name', ':name'),
$qb->expr()->eq('p.name', ':parent')
))
->setParameters(array(
'name' => $node->getName(),
'parent' => $node->getParent()->getName(),
));
}
return $qb;
}
public function getSameNameSiblingsCountQuery($node)
{
return $this->getSameNameSiblingsCountQueryBuilder($node)->getQuery();
}
public function getSameNameSiblingsCount($node)
{
return $this->getSameNameSiblingsCountQuery($node)->getSingleScalarResult();
}
}