isNew() && $entity->has('layoutList')) { $listBefore = $entity->getFetched('layoutList') ?? []; $listNow = $entity->get('layoutList') ?? []; foreach ($listBefore as $name) { if (!in_array($name, $listNow)) { $layout = $this->getEntityManager()->getRepository('LayoutRecord')->where([ 'layoutSetId' => $entity->id, 'name' => $name, ])->findOne(); if ($layout) { $this->getEntityManager()->removeEntity($layout); } } } } } protected function afterRemove(Entity $entity, array $options = []) { $layoutList = $this->getEntityManager()->getRepository('LayoutRecord')->where([ 'layoutSetId' => $entity->id, ])->find(); foreach ($layoutList as $layout) { $this->getEntityManager()->removeEntity($layout); } } }