/** * Delete a product template entry * * @param \Thelia\Core\Event\Template\TemplateDeleteEvent $event */ public function delete(TemplateDeleteEvent $event) { if (null !== ($template = TemplateQuery::create()->findPk($event->getTemplateId()))) { // Check if template is used by a product $product_count = ProductQuery::create()->findByTemplateId($template->getId())->count(); if ($product_count <= 0) { $template->setDispatcher($event->getDispatcher())->delete(); } $event->setTemplate($template); $event->setProductCount($product_count); } }
/** * Delete a product template entry * * @param \Thelia\Core\Event\Template\TemplateDeleteEvent $event * @throws \Exception */ public function delete(TemplateDeleteEvent $event) { if (null !== ($template = TemplateQuery::create()->findPk($event->getTemplateId()))) { // Check if template is used by a product $product_count = ProductQuery::create()->findByTemplateId($template->getId())->count(); if ($product_count <= 0) { $con = Propel::getWriteConnection(TemplateTableMap::DATABASE_NAME); $con->beginTransaction(); try { $template->setDispatcher($event->getDispatcher())->delete($con); // We have to also delete any reference of this template in category tables // We can't use a FK here, as the DefaultTemplateId column may be NULL // so let's take care of this. CategoryQuery::create()->filterByDefaultTemplateId($event->getTemplateId())->update(['DefaultTemplateId' => null], $con); $con->commit(); } catch (\Exception $ex) { $con->rollback(); throw $ex; } } $event->setTemplate($template); $event->setProductCount($product_count); } }