Skip to content

Instantly share code, notes, and snippets.

@alcaeus
Created February 28, 2017 10:16
Show Gist options
  • Save alcaeus/af289aabfe38635bc5ee6154c58302f6 to your computer and use it in GitHub Desktop.
Save alcaeus/af289aabfe38635bc5ee6154c58302f6 to your computer and use it in GitHub Desktop.
Relationship from child document to parent
<?php
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
/**
* @ODM\EmbeddedDocument
* @ODM\HasLifecycleCallbacks
*/
class EmbeddedDocument
{
private $parent;
public function __construct(Parent $parent)
{
$this->parent = $parent;
}
/** @ODM\PostLoad */
public function postLoad(LifecycleEventArgs $args)
{
$uow = $args->getDocumentManager()->getUnitOfWork();
[, $this->parent] = $args->getDocumentManager()->getUnitOfWork()->getParentAssociation($this);
}
}
<?php
/** @ODM\Document **/
class Parent
{
/** @ODM\EmbedOne(...) */
private $embedded;
public function __construct()
{
$this->embedded = new Embedded($this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment