context->getObject(); /** @var StructuralDBElement $value */ // Check if the targeted parent is the object itself: $entity_id = $entity->getID(); if ($entity_id !== null && $entity_id === $value->getID()) { //Set the entity to a valid state $entity->setParent(null); $this->context->buildViolation($constraint->self_message)->addViolation(); //The other things can not happen. return; } // Check if the targeted parent is a child object if ($value->isChildOf($entity)) { //Set the entity to a valid state $entity->setParent(null); $this->context->buildViolation($constraint->children_message)->addViolation(); return; } } }