Skip to content

Commit

Permalink
Make sure author information is not duplicated in the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Oct 27, 2011
1 parent 8244e49 commit 607e176
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Packagist/WebBundle/Command/UpdatePackagesCommand.php
Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions 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();
}
}

0 comments on commit 607e176

Please sign in to comment.