opti attribute group deletion

This commit is contained in:
Marion Muszynski 2016-06-03 13:06:03 +02:00
parent b1a0a1b29e
commit 5f932e9bcd

View File

@ -0,0 +1,47 @@
<?php
class AttributeGroup extends AttributeGroupCore
{
/**
* @Override
* On vient directement recup que les attributeCombinaisons avec l'id_attribute = 0
*/
public static function cleanDeadCombinations()
{
$attributeCombinations = Db::getInstance()->ExecuteS('
SELECT pac.`id_attribute`, pa.`id_product_attribute`
FROM `'._DB_PREFIX_.'product_attribute` pa LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pa.`id_product_attribute` = pac.`id_product_attribute`)
WHERE pac.`id_attribute`=0
');
$toRemove = array();
foreach ($attributeCombinations AS $attributeCombination)
if ((int)($attributeCombination['id_attribute']) == 0)
$toRemove[] = (int)($attributeCombination['id_product_attribute']);
if (!empty($toRemove) AND Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'product_attribute` WHERE `id_product_attribute` IN ('.implode(', ', $toRemove).')') === false)
return false;
return true;
}
public function delete()
{
/* Select children in order to find linked combinations */
$attributeIds = Db::getInstance()->ExecuteS('SELECT `id_attribute` FROM `'._DB_PREFIX_.'attribute` WHERE `id_attribute_group` = '.(int)($this->id));
if ($attributeIds === false)
return false;
/* Removing attributes to the found combinations */
$toRemove = array();
foreach ($attributeIds AS $attribute)
$toRemove[] = (int)($attribute['id_attribute']);
if (!empty($toRemove) AND Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'product_attribute_combination` WHERE `id_attribute` IN ('.implode(', ', $toRemove).')') === false)
return false;
/* Remove combinations if they do not possess attributes anymore */
if (!self::cleanDeadCombinations())
return false;
/* Also delete related attributes */
if (Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'attribute_lang` WHERE `id_attribute` IN (SELECT id_attribute FROM `'._DB_PREFIX_.'attribute` WHERE `id_attribute_group` = '.(int)($this->id).')') === false OR Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'attribute` WHERE `id_attribute_group` = '.(int)($this->id)) === false)
return false;
$return = parent::delete();
if($return)
Module::hookExec('afterDeleteAttributeGroup', array('id_attribute_group' => $this->id));
return $return;
}
}