Browse Source

Make sure author information is not duplicated in the DB

Jordi Boggiano 14 years ago
parent
commit
607e176ac7

+ 12 - 6
src/Packagist/WebBundle/Command/UpdatePackagesCommand.php

@@ -244,24 +244,30 @@ EOF
 
 
                 if (!empty($authorData['email'])) {
                 if (!empty($authorData['email'])) {
                     $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByEmail($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) {
                 if (!$author) {
                     $author = new Author();
                     $author = new Author();
                     $em->persist($author);
                     $em->persist($author);
                 }
                 }
+
                 foreach (array('email', 'name', 'homepage') as $field) {
                 foreach (array('email', 'name', 'homepage') as $field) {
                     if (isset($authorData[$field])) {
                     if (isset($authorData[$field])) {
                         $author->{'set'.$field}($authorData[$field]);
                         $author->{'set'.$field}($authorData[$field]);
                     }
                     }
                 }
                 }
+
                 $author->setUpdatedAt(new \DateTime);
                 $author->setUpdatedAt(new \DateTime);
                 if (!$version->getAuthors()->contains($author)) {
                 if (!$version->getAuthors()->contains($author)) {
                     $version->addAuthor($author);
                     $version->addAuthor($author);

+ 33 - 0
src/Packagist/WebBundle/Entity/AuthorRepository.php

@@ -0,0 +1,33 @@
+<?php
+
+/*
+ * This file is part of Packagist.
+ *
+ * (c) Jordi Boggiano <j.boggiano@seld.be>
+ *     Nils Adermann <naderman@naderman.de>
+ *
+ * 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 <j.boggiano@seld.be>
+ */
+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();
+    }
+}