public function validateOneMainCategory(ExecutionContextInterface $context) { if ($this->getCategories()->count() == 0) { return; } if (!$this->hasOneMainCategory()) { $context->addViolation('sonata.product.must_have_one_main_category'); } }