/** * Delete a product entry * * @param \Thelia\Core\Event\Product\ProductDeleteEvent $event */ public function delete(ProductDeleteEvent $event) { if (null !== ($product = ProductQuery::create()->findPk($event->getProductId()))) { $product->setDispatcher($event->getDispatcher())->delete(); $event->setProduct($product); } }
/** * @depends testSetProductTemplate */ public function testDelete(ProductModel $product) { $event = new ProductDeleteEvent($product->getId()); $event->setDispatcher($this->getDispatcher()); $action = new Product(); $action->delete($event); $deletedProduct = $event->getProduct(); $this->assertInstanceOf('Thelia\\Model\\Product', $deletedProduct, 'deleted product must be an instance of Thelia\\Model\\Product'); $this->assertTrue($deletedProduct->isDeleted()); }
/** * Delete a product entry * * @param \Thelia\Core\Event\Product\ProductDeleteEvent $event */ public function delete(ProductDeleteEvent $event) { if (null !== ($product = ProductQuery::create()->findPk($event->getProductId()))) { $con = Propel::getWriteConnection(ProductTableMap::DATABASE_NAME); $con->beginTransaction(); try { // Get product's files to delete after product deletion $fileList['images']['list'] = ProductImageQuery::create()->findByProductId($event->getProductId()); $fileList['images']['type'] = TheliaEvents::IMAGE_DELETE; $fileList['documentList']['list'] = ProductDocumentQuery::create()->findByProductId($event->getProductId()); $fileList['documentList']['type'] = TheliaEvents::DOCUMENT_DELETE; // Delete product $product->setDispatcher($event->getDispatcher())->delete($con); $event->setProduct($product); // Dispatch delete product's files event foreach ($fileList as $fileTypeList) { foreach ($fileTypeList['list'] as $fileToDelete) { $fileDeleteEvent = new FileDeleteEvent($fileToDelete); $event->getDispatcher()->dispatch($fileTypeList['type'], $fileDeleteEvent); } } $con->commit(); } catch (\Exception $e) { $con->rollback(); throw $e; } } }