Skip to content

Instantly share code, notes, and snippets.

@1ed
Last active May 24, 2017 10:19
Show Gist options
  • Save 1ed/2d9d639854e46100e1e33b9f560ca634 to your computer and use it in GitHub Desktop.
Save 1ed/2d9d639854e46100e1e33b9f560ca634 to your computer and use it in GitHub Desktop.
Doctrine cascade presist order
<?php
<<<CONFIG
packages:
- "doctrine/orm: ^2.5"
- "ramsey/uuid-doctrine: ^1.3"
- "symfony/var-dumper: ^3.2"
CONFIG;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Tools\Setup;
\Doctrine\DBAL\Types\Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');
$config = Setup::createConfiguration(true);
$annotation = $config->newDefaultAnnotationDriver([__DIR__], false);
$annotation->addExcludePaths([__DIR__.'/vendor']);
$config->setMetadataDriverImpl($annotation);
$entityManager = EntityManager::create($params = [
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/db.sqlite',
], $config);
$sm = $entityManager->getConnection()->getSchemaManager();
$sm->dropAndCreateDatabase($params['path']);
$st = new \Doctrine\ORM\Tools\SchemaTool($entityManager);
$st->createSchema($entityManager->getMetadataFactory()->getAllMetadata());
/**
* @ORM\Entity()
*/
class Offer
{
/**
* @ORM\Id()
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Request", inversedBy="offers", cascade={"persist"})
* @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
*/
private $request;
public function __construct(Request $request)
{
$this->id = \Ramsey\Uuid\Uuid::uuid4();
$this->request = $request;
$this->request->addOffer($this);
}
public function getId()
{
return $this->id;
}
public function getRequest()
{
return $this->request;
}
}
/**
* @ORM\Entity()
*/
class Request
{
/**
* @ORM\Id()
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* @var Offer[]|\Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany(targetEntity="Offer", mappedBy="request", cascade={"persist"})
*/
private $offers;
/**
* @ORM\OneToOne(targetEntity="Offer")
* @ORM\JoinColumn(onDelete="SET NULL", nullable=true)
*/
private $latestOffer;
public function __construct()
{
$this->id = \Ramsey\Uuid\Uuid::uuid4();
$this->offers = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function addOffer(Offer $offer)
{
if ($this->offers->contains($offer)) {
return;
}
if ($this !== $offer->getRequest()) {
throw new \LogicException('The offer must belong this request.');
}
$this->offers->add($offer);
$this->latestOffer = $offer;
}
public function getLatestOffer()
{
return $this->latestOffer;
}
}
$request = new Request();
$offer = new Offer($request);
// $entityManager->persist($request);
$entityManager->persist($offer);
$entityManager->flush();
$entityManager->clear();
$request = $entityManager->find(Request::class, $request->getId());
$offer = $entityManager->find(Offer::class, $offer->getId());
$offer->getRequest();
$request->getLatestOffer();
dump($request, $offer);
@1ed
Copy link
Author

1ed commented May 24, 2017

Run with (https://github.com/sensiolabs/melody): melody run --no-cache --trust -vvv 2d9d639854e46100e1e33b9f560ca634

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment