From 607e176ac7713520f336cf8951ca30d85ec08c53 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 27 Oct 2011 22:21:56 +0200 Subject: [PATCH] Make sure author information is not duplicated in the DB --- .../Command/UpdatePackagesCommand.php | 18 ++++++---- .../WebBundle/Entity/AuthorRepository.php | 33 +++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/Packagist/WebBundle/Entity/AuthorRepository.php diff --git a/src/Packagist/WebBundle/Command/UpdatePackagesCommand.php b/src/Packagist/WebBundle/Command/UpdatePackagesCommand.php index edab4587b..840db0aa7 100644 --- a/src/Packagist/WebBundle/Command/UpdatePackagesCommand.php +++ b/src/Packagist/WebBundle/Command/UpdatePackagesCommand.php @@ -244,24 +244,30 @@ private function updateInformation(OutputInterface $output, RegistryInterface $d if (!empty($authorData['email'])) { $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByEmail($authorData['email']); + } - if (!$author && !empty($authorData['homepage'])) { - $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneBy(array( - 'name' => $authorData['name'], - 'homepage' => $authorData['homepage'] - )); - } + if (!$author && !empty($authorData['homepage'])) { + $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneBy(array( + 'name' => $authorData['name'], + 'homepage' => $authorData['homepage'] + )); + } + + if (!$author && !empty($authorData['name'])) { + $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByNameAndPackage($authorData['name'], $package); } if (!$author) { $author = new Author(); $em->persist($author); } + foreach (array('email', 'name', 'homepage') as $field) { if (isset($authorData[$field])) { $author->{'set'.$field}($authorData[$field]); } } + $author->setUpdatedAt(new \DateTime); if (!$version->getAuthors()->contains($author)) { $version->addAuthor($author); diff --git a/src/Packagist/WebBundle/Entity/AuthorRepository.php b/src/Packagist/WebBundle/Entity/AuthorRepository.php new file mode 100644 index 000000000..d18b2be20 --- /dev/null +++ b/src/Packagist/WebBundle/Entity/AuthorRepository.php @@ -0,0 +1,33 @@ + + * Nils Adermann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Packagist\WebBundle\Entity; + +use Doctrine\ORM\EntityRepository; + +/** + * @author Jordi Boggiano + */ +class AuthorRepository extends EntityRepository +{ + public function findOneByNameAndPackage($author, Package $package) + { + $qb = $this->createQueryBuilder('a'); + $qb->select('a') + ->leftJoin('a.versions', 'v') + ->leftJoin('v.package', 'p') + ->where('p.id = :packageId') + ->andWhere('a.name = :author') + ->setParameters(array('author' => $author, 'packageId' => $package->getId())); + return $qb->getQuery()->getOneOrNullResult(); + } +}