/**
  * Delete the object
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Exception
  */
 public function delete(\Magento\Framework\Model\AbstractModel $object)
 {
     $connection = $this->transactionManager->start($this->getConnection());
     try {
         $object->beforeDelete();
         $this->_beforeDelete($object);
         $this->objectRelationProcessor->delete($this->transactionManager, $connection, $this->getMainTable(), $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $object->getId()), $object->getData());
         $this->_afterDelete($object);
         $object->isDeleted(true);
         $object->afterDelete();
         $this->transactionManager->commit();
         $object->afterDeleteCommit();
     } catch (\Exception $e) {
         $this->transactionManager->rollBack();
         throw $e;
     }
     return $this;
 }
Beispiel #2
0
 /**
  * Delete products.
  *
  * @return $this
  * @throws \Exception
  */
 protected function _deleteProducts()
 {
     $productEntityTable = $this->_resourceFactory->create()->getEntityTable();
     while ($bunch = $this->_dataSourceModel->getNextBunch()) {
         $idToDelete = [];
         foreach ($bunch as $rowNum => $rowData) {
             if ($this->validateRow($rowData, $rowNum) && self::SCOPE_DEFAULT == $this->getRowScope($rowData)) {
                 $idToDelete[] = $this->_oldSku[$rowData[self::COL_SKU]]['entity_id'];
             }
         }
         if ($idToDelete) {
             $this->countItemsDeleted += count($idToDelete);
             $this->transactionManager->start($this->_connection);
             try {
                 $this->objectRelationProcessor->delete($this->transactionManager, $this->_connection, $productEntityTable, $this->_connection->quoteInto('entity_id IN (?)', $idToDelete), ['entity_id' => $idToDelete]);
                 $this->transactionManager->commit();
             } catch (\Exception $e) {
                 $this->transactionManager->rollBack();
                 throw $e;
             }
             $this->_eventManager->dispatch('catalog_product_import_bunch_delete_after', ['adapter' => $this, 'bunch' => $bunch]);
         }
     }
     return $this;
 }
 /**
  * Delete entity using current object's data
  *
  * @param \Magento\Framework\DataObject|int|string $object
  * @return $this
  * @throws \Exception
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function delete($object)
 {
     try {
         $connection = $this->transactionManager->start($this->getConnection());
         if (is_numeric($object)) {
             $id = (int) $object;
         } elseif ($object instanceof \Magento\Framework\Model\AbstractModel) {
             $object->beforeDelete();
             $id = (int) $object->getId();
         }
         $this->_beforeDelete($object);
         try {
             $where = [$this->getEntityIdField() . '=?' => $id];
             $this->objectRelationProcessor->delete($this->transactionManager, $connection, $this->getEntityTable(), $this->getConnection()->quoteInto($this->getEntityIdField() . '=?', $id), [$this->getEntityIdField() => $id]);
             $this->loadAllAttributes($object);
             foreach ($this->getAttributesByTable() as $table => $attributes) {
                 $this->getConnection()->delete($table, $where);
             }
         } catch (\Exception $e) {
             throw $e;
         }
         $this->_afterDelete($object);
         if ($object instanceof \Magento\Framework\Model\AbstractModel) {
             $object->isDeleted(true);
             $object->afterDelete();
         }
         $this->transactionManager->commit();
         if ($object instanceof \Magento\Framework\Model\AbstractModel) {
             $object->afterDeleteCommit();
         }
     } catch (\Exception $e) {
         $this->transactionManager->rollBack();
         throw $e;
     }
     return $this;
 }