I am writing a module which imports additional product information into a custom table with SKU as the PK.
If there is already a row in the custom table regarding the SKU, i have to delete the old row first, otherwise an Error "Unique Constraint violation found" is thrown.
However, when i call ResourceModel->delete(), nothing seems to happen on my custom table. I can see in the Model object regarding that SKU, that the isDeleted flag is now true and storedData is now length 0. Then i tried ResourceModel->save(), got a new empty Model object from the Factory, add the new Data for the SKU to it and save() using ResourceModel or transaction->save(). This only prevented the error,but the row in the custom table stayed the same.
How do I delete from my custom table in the right way?
This is the current code with a workaround, which is not using the ResourceModel to delete:
$stockImportModel = $this->stockImportFactory->create();
$this->stockImportResourceModel->load($stockImportModel, $sku);
if(!$stockImportModel->isEmpty()) {
// Delete with Model/ResourceModel not working. Delete condition in ObjectRelationProcessor empty.
// $this->stockImportResourceModel->delete($stockImportModel);
$connection = $this->resourceConnection->getConnection();
$tableName = $connection->getTableName(self::SAP_INVENTORY_TABLE);
$whereConditions = [
$connection->quoteInto('SKU = ?', $sku),
];
$connection->delete($tableName, $whereConditions);
}
$stockImportModel = $this->stockImportFactory->create();
$stockImportModel->addData([
ResourceModel:
protected function _construct() {
$this->_init('Vendor\Module\Model\ResourceModel\ResourceModelName');
}
Model:
protected function _construct() {
$this->_init('catalog_product_advanced_information', 'sku');
$this->_isPkAutoIncrement = false;
}