-
-
Save 1ed/2d9d639854e46100e1e33b9f560ca634 to your computer and use it in GitHub Desktop.
Doctrine cascade presist order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with (https://github.com/sensiolabs/melody):
melody run --no-cache --trust -vvv 2d9d639854e46100e1e33b9f560ca634