diff --git a/lib/Doctrine/Common/DataFixtures/AbstractFixture.php b/lib/Doctrine/Common/DataFixtures/AbstractFixture.php index ac5976e2..1e7fb674 100644 --- a/lib/Doctrine/Common/DataFixtures/AbstractFixture.php +++ b/lib/Doctrine/Common/DataFixtures/AbstractFixture.php @@ -63,11 +63,14 @@ public function setReference($name, $object) /** * Set the reference entry identified by $name * and referenced to managed $object. If $name - * already is set, it overrides it + * already is set, it throws a + * BadMethodCallException exception * * @param string $name * @param object $object - managed object * @see Doctrine\Common\DataFixtures\ReferenceRepository::addReference + * @throws BadMethodCallException - if repository already has + * a reference by $name * @return void */ public function addReference($name, $object) diff --git a/lib/Doctrine/Common/DataFixtures/FixtureInterface.php b/lib/Doctrine/Common/DataFixtures/FixtureInterface.php index 9b0a8b4c..3d66bf46 100644 --- a/lib/Doctrine/Common/DataFixtures/FixtureInterface.php +++ b/lib/Doctrine/Common/DataFixtures/FixtureInterface.php @@ -31,7 +31,7 @@ interface FixtureInterface /** * Load data fixtures with the passed EntityManager * - * @param Doctrine\Common\Persistence\ObjectManager $manager + * @param ObjectManager $manager */ - function load(ObjectManager $manager); + public function load(ObjectManager $manager); } diff --git a/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php b/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php index 9bc5dd4b..baf0bed7 100644 --- a/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php +++ b/lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php @@ -28,6 +28,7 @@ * Allow data fixture references and identities to be persisted when cached data fixtures * are pre-loaded, for example, by LiipFunctionalTestBundle\Test\WebTestCase loadFixtures(). * + * @author Guilherme Blanco * @author Anthon Pang */ class ProxyReferenceRepository extends ReferenceRepository @@ -59,12 +60,13 @@ protected function getRealClass($className) */ public function serialize() { + $unitOfWork = $this->getManager()->getUnitOfWork(); $simpleReferences = array(); foreach ($this->getReferences() as $name => $reference) { $className = $this->getRealClass(get_class($reference)); - $simpleReferences[$name] = array($className, $reference->getId()); + $simpleReferences[$name] = array($className, $this->getIdentifier($reference, $unitOfWork)); } $serializedData = json_encode(array( @@ -90,7 +92,7 @@ public function unserialize($serializedData) $name, $this->getManager()->getReference( $proxyReference[0], // entity class name - $proxyReference[1] // id + $proxyReference[1] // identifiers ) ); } diff --git a/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php b/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php index 658b3d49..305f2616 100644 --- a/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php +++ b/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php @@ -19,6 +19,7 @@ namespace Doctrine\Common\DataFixtures\Purger; +use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Internal\CommitOrderCalculator; use Doctrine\ORM\Mapping\ClassMetadata; @@ -127,6 +128,10 @@ public function purge() $orderedTables[] = $class->getQuotedTableName($platform); } + if ($platform instanceof MySqlPlatform) { + $this->setForeignKeyChecks(false); + } + foreach($orderedTables as $tbl) { if ($this->purgeMode === self::PURGE_MODE_DELETE) { $this->em->getConnection()->executeUpdate("DELETE FROM " . $tbl); @@ -134,6 +139,10 @@ public function purge() $this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true)); } } + + if ($platform instanceof MySqlPlatform) { + $this->setForeignKeyChecks(true); + } } private function getCommitOrder(EntityManager $em, array $classes) @@ -196,4 +205,18 @@ private function getAssociationTables(array $classes) return $associationTables; } + + /** + * Enable/disable foreign key checks on the MySQL platform + * + * @param bool $bool + */ + private function setForeignKeyChecks($bool) + { + if ($this->purgeMode !== self::PURGE_MODE_TRUNCATE) { + return; + } + + $this->em->getConnection()->query(sprintf('SET FOREIGN_KEY_CHECKS=%s', (int) $bool)); + } } diff --git a/lib/Doctrine/Common/DataFixtures/ReferenceRepository.php b/lib/Doctrine/Common/DataFixtures/ReferenceRepository.php index 1cac6e38..290f2212 100644 --- a/lib/Doctrine/Common/DataFixtures/ReferenceRepository.php +++ b/lib/Doctrine/Common/DataFixtures/ReferenceRepository.php @@ -70,14 +70,23 @@ public function __construct(ObjectManager $manager) * @param object $reference Reference object * @param object $uow Unit of work * - * @return mixed + * @return array */ protected function getIdentifier($reference, $uow) { + // In case Reference is not yet managed in UnitOfWork + if ( ! $uow->isInIdentityMap($reference)) { + $class = $this->manager->getClassMetadata(get_class($reference)); + + return $class->getIdentifierValues($reference); + } + + // Dealing with ORM UnitOfWork if (method_exists($uow, 'getEntityIdentifier')) { return $uow->getEntityIdentifier($reference); } + // ODM UnitOfWork return $uow->getDocumentIdentifier($reference); }