From b2cf3c6e6ebd43d9efbf093b613b691a27ae281b Mon Sep 17 00:00:00 2001 From: AD7six Date: Tue, 28 Jan 2014 17:05:27 +0000 Subject: [PATCH 1/2] add the name to the package-edit form Right now the only way to "rename" a package is to delete it and recreate with the correct name - make it easier. --- src/Packagist/WebBundle/Controller/PackageController.php | 3 +++ src/Packagist/WebBundle/Entity/Package.php | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Packagist/WebBundle/Controller/PackageController.php b/src/Packagist/WebBundle/Controller/PackageController.php index b31aed51d..28fc60ca6 100644 --- a/src/Packagist/WebBundle/Controller/PackageController.php +++ b/src/Packagist/WebBundle/Controller/PackageController.php @@ -32,6 +32,8 @@ public function editAction(Request $req, $name) $packageRepo = $this->getDoctrine()->getRepository('PackagistWebBundle:Package'); /** @var $package Package */ $package = $packageRepo->findOneByName($name); + $package->setEntityRepository($this->getDoctrine()->getRepository('PackagistWebBundle:Package')); + $package->setRouter($this->get('router')); if (!$package) { throw $this->createNotFoundException("The requested package, $name, could not be found."); @@ -42,6 +44,7 @@ public function editAction(Request $req, $name) } $form = $this->createFormBuilder($package, array("validation_groups" => array("Update"))) + ->add("name", "text") ->add("repository", "text") ->getForm(); diff --git a/src/Packagist/WebBundle/Entity/Package.php b/src/Packagist/WebBundle/Entity/Package.php index fd96be1b5..e5711df54 100644 --- a/src/Packagist/WebBundle/Entity/Package.php +++ b/src/Packagist/WebBundle/Entity/Package.php @@ -32,7 +32,7 @@ * @ORM\Index(name="dumped_idx",columns={"dumpedAt"}) * } * ) - * @Assert\Callback(methods={"isPackageUnique"}) + * @Assert\Callback(methods={"isPackageUnique"}, groups={"Update", "Default"}) * @Assert\Callback(methods={"isRepositoryValid"}, groups={"Update", "Default"}) * @author Jordi Boggiano */ @@ -221,7 +221,12 @@ public function isPackageUnique(ExecutionContext $context) { try { if ($this->entityRepository->findOneByName($this->name)) { - $context->addViolationAt('repository', 'A package with the name '.$this->name.' already exists.', array(), null); + if ($context->getGroup() === 'Update') { + $message = sprintf('A package named %s already exists', $this->name); + } else { + $message = 'A package with the name '.$this->name.' already exists.'; + } + $context->addViolationAt('repository', $message, array(), null); } } catch (\Doctrine\ORM\NoResultException $e) {} } From 594c93a71bf7bb8452163d96df3e85290c63da92 Mon Sep 17 00:00:00 2001 From: AD7six Date: Tue, 28 Jan 2014 18:15:24 +0000 Subject: [PATCH 2/2] allow changing just the repository --- src/Packagist/WebBundle/Entity/Package.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Packagist/WebBundle/Entity/Package.php b/src/Packagist/WebBundle/Entity/Package.php index e5711df54..d75fed572 100644 --- a/src/Packagist/WebBundle/Entity/Package.php +++ b/src/Packagist/WebBundle/Entity/Package.php @@ -219,9 +219,23 @@ public function setRouter($router) public function isPackageUnique(ExecutionContext $context) { + $group = $context->getGroup(); + + $qb = $this->entityRepository->getBaseQueryBuilder(); + + if ($group === 'Update') { + $qb->where('p.name = :name AND p.id != :id') + ->setParameters(array('name' => $this->name, 'id' => $this->id)); + } else { + $qb->where('p.name = :name') + ->setParameter('name', $this->name); + } + + $query = $qb->getQuery(); + try { - if ($this->entityRepository->findOneByName($this->name)) { - if ($context->getGroup() === 'Update') { + if ($query->getSingleResult()) { + if ($group === 'Update') { $message = sprintf('A package named %s already exists', $this->name); } else { $message = 'A package with the name '.$this->name.' already exists.';