Код: Выделить всё
class MainEntity
{
/**
* @var SecondEntity[]
*
* @ORM\OneToMany(targetEntity="SecondEntity", mappedBy="mainEntity", cascade={"persist"})
*/
private $secondEntities;
/**
* @var integer
*
* @ORM\Column(type="integer", nullable=false, name="second_entities_count")
*/
private $secondEntitiesCount;
...
}
class SecondEntity
{
/**
* @var MainEntity
*
* @ORM\ManyToOne(targetEntity="MainEntity", inversedBy="secondEntities")
* @ORM\JoinColumn(name="main_entity_id", referencedColumnName="id", nullable=false)
*/
private $mainEntity;
...
}
Для этого я создал подписчика onFlush, который собирает все запланированные удаления и вставки объектов SecondEntity
Код: Выделить всё
$delsertions = array_merge(
$unitOfWork->getScheduledEntityInsertions(),
$unitOfWork->getScheduledEntityDeletions()
);
foreach ($delsertions as $entity) {
if ($entity instanceof SecondEntity) {
$mainEntity = $entity->getMainEntity();
$mainEntityMeta = $em->getClassMetadata(MainEntity::class);
$unitOfWork->recomputeSingleEntityChangeSet($mainEntityMeta, $mainEntity);
dump($mainEntity->getSecondEntities); // The creation/deletion of the current entity is not reflected here!
}
}
И если я удалю единственный SecondEntity< /code>, коллекция $ SecondEntities по-прежнему будет содержать этот объект внутри.
Вызов recomputeSingleEntityChangeSet(), похоже, в этом случае ничего не делает.
Как заставить коллекцию правильно обновляться?
Подробнее здесь: https://stackoverflow.com/questions/382 ... object-onf