I have a OneToMany Relationship between a ChartPage and a BaseChart:
1 ChartPage holds 1 BaseChart
and 1 BaseChart holds many ChartPages
Charts are managed in a different bundle of my application, so they can be removed individually. What I like to have is, that Doctrine automatically removes the ChartPage.Chart reference when the Chart is deleted, but nothing else (not remove the ChartPage).
The other way around should leave everything as is: When I remove the ChartPage with a referenced BaseChart - nothing should happen (not delete the BaseChart)
I tried every combination with one of these: cascade="{detach,merge,refresh,remove,persist}"
that I could think of, but I can't figure it out..
This is my mapping:
<?php
/**
* Class ChartPage
* @package VBCMS\Bundle\AdminBundle\Document\Page
* @Serializer\AccessType("public_method")
* @MongoDB\Document()
*/
class ChartPage extends BasePage {
/**
* @var BaseChart
* @Serializer\Type("VBCMS\Bundle\StatisticBundle\Document\BaseChart")
* @Serializer\Accessor(setter="setChartDeserialize")
* @MongoDB\ReferenceOne(
* targetDocument="VBCMS\Bundle\StatisticBundle\Document\BaseChart",
* mappedBy="pages",
* cascade={"persist,detach,merge"}
* )
*/
protected $chart;
}
/
/**
* Class BaseChart
* @package VBCMS\Bundle\StatisticBundle\Document
* @Serializer\AccessType("public_method")
* @MongoDB\Document(
* collection="Chart",
* repositoryClass="VBCMS\Bundle\StatisticBundle\Repository\ChartRepository"
* )
*/
class BaseChart {
/**
* @var BasePage[]|Collection
* @Serializer\Exclude()
* @MongoDB\ReferenceMany(
* targetDocument="VBCMS\Bundle\AdminBundle\Document\Page\ChartPage",
* inversedBy="chart",
* cascade={"persist,detach,merge"}
* )
*/
protected $pages;
}
The only idea I have left is to build a custom preRemove EventListener that sets the references back to NULL
before a BasePage ist removed, but I hoped I could avoid this manual mess.