Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/e1ed6136e9a69d725eb0 to your computer and use it in GitHub Desktop.
Save doctrinebot/e1ed6136e9a69d725eb0 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-1443 - https://github.com/doctrine/doctrine2/issues/2070
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\EventSubscriber;
require_once __DIR__ . '/../../../TestInit.php';
/**
* @group DDC-1443
*/
class DDC1443Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
parent::setUp();
$configuration = $this->_getTestEntityManager()->getConfiguration();
$configuration->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1443_TestEntity1'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1443_TestEntity2'),
));
} catch(\PDOException $e) {
}
}
public function testIssue()
{
$em = $this->_getTestEntityManager();
$em->getEventManager()->addEventSubscriber(new TestEntity1Subscriber());
$entity = new DDC1443_TestEntity2();
$em->persist($entity);
$em->flush();
$q = $em->createQuery('SELECT e FROM ' . __NAMESPACE__ . '\\DDC1443_TestEntity1 e WHERE e.testEntity2 = ?0');
$q->setParameter(0, $entity);
$this->assertEquals(1, count($q->getResult()));
}
}
class TestEntity1Subscriber implements EventSubscriber
{
/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array('prePersist');
}
/**
* Check if this entity is listened to
*/
protected function isListenedTo($entity)
{
return ($entity instanceof DDC1443_TestEntity2);
}
/**
* Create a new DDC1443_TestEntity1
*
* @param EventArgs $args
*/
public function prePersist($args)
{
$entity = $args->getEntity();
$em = $args->getEntityManager();
// Is it our desired Entity?
if ( ! $this->isListenedTo($entity)) return;
if ($entity->getId() === null) {
$entityWrapper = new DDC1443_TestEntity1();
$entityWrapper->setTestEntity2($entity);
$em->persist($entityWrapper);
}
}
}
/**
* @Entity
* @Table(name="te1")
*/
class DDC1443_TestEntity1
{
/**
* @Id
* @GeneratedValue(strategy="AUTO")
* @Column(type="integer")
*/
private $id;
/**
* @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC1443_TestEntity2", cascade={"persist"})
* @JoinColumn(name="test_entity2_id", referencedColumnName="id", nullable=false)
*/
private $testEntity2;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param DDC1443_TestEntity2 $testEntity2
*/
public function setTestEntity2(DDC1443_TestEntity2 $testEntity2)
{
$this->testEntity2 = $testEntity2;
}
/**
* @return DDC1443_TestEntity2
*/
public function getTestEntity2()
{
return $this->testEntity2;
}
}
/**
* @Entity
* @Table(name="te2")
*/
class DDC1443_TestEntity2
{
/**
* @Id
* @GeneratedValue(strategy="AUTO")
* @Column(type="integer")
*/
private $id;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
}
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\EventSubscriber,
Doctrine\ORM\Event\OnFlushEventArgs;
require_once __DIR__ . '/../../../TestInit.php';
/**
* @group DDC-1443
*/
class DDC1443Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
parent::setUp();
$configuration = $this->_getEntityManager()->getConfiguration();
$configuration->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1443_TestEntity1'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1443_TestEntity2'),
));
} catch(\PDOException $e) {
}
}
public function testIssue()
{
$em = $this->_getEntityManager();
$em->getEventManager()->addEventSubscriber(new TestEntity1Subscriber());
$entity = new DDC1443_TestEntity2();
$em->persist($entity);
$em->flush();
$q = $em->createQuery('SELECT e FROM ' . __NAMESPACE__ . '\\DDC1443_TestEntity1 e WHERE e.testEntity2 = ?0');
$q->setParameter(0, $entity);
$this->assertEquals(1, count($q->getResult()));
}
}
class TestEntity1Subscriber implements EventSubscriber
{
/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array('onFlush');
}
/**
* Check if this entity is listened to
*
* {@inheritdoc}
*/
protected function isListenedTo($entity)
{
return ($entity instanceof DDC1443_TestEntity2);
}
/**
* Create a new DDC1443_TestEntity1
*
* @param Doctrine\ORM\Event\OnFlushEventArgs $args
*/
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityInsertions() AS $entity) {
if ( ! $this->isListenedTo($entity)) continue;
$classMetadata = $em->getClassMetadata(__NAMESPACE__ . '\\DDC1443_TestEntity1');
$entityWrapper = new DDC1443_TestEntity1();
$entityWrapper->setTestEntity2($entity);
$em->persist($entityWrapper);
$uow->computeChangeSet($classMetadata, $entityWrapper);
}
}
}
/**
* @Entity
* @Table(name="te1")
*/
class DDC1443_TestEntity1
{
/**
* @Id
* @GeneratedValue(strategy="AUTO")
* @Column(type="integer")
*/
private $id;
/**
* @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC1443_TestEntity2", cascade={"persist"})
* @JoinColumn(name="test_entity2_id", referencedColumnName="id", nullable=false)
*/
private $testEntity2;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param DDC1443_TestEntity2 $testEntity2
*/
public function setTestEntity2(DDC1443_TestEntity2 $testEntity2)
{
$this->testEntity2 = $testEntity2;
}
/**
* @return DDC1443_TestEntity2
*/
public function getTestEntity2()
{
return $this->testEntity2;
}
}
/**
* @Entity
* @Table(name="te2")
*/
class DDC1443_TestEntity2
{
/**
* @Id
* @GeneratedValue(strategy="AUTO")
* @Column(type="integer")
*/
private $id;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment