Example #1
0
    public function delete()
    {
        if ($this->id == 1) {
            return false;
        }
        /* Get childs categories */
        $toDelete = array((int) $this->id);
        $this->recursiveDelete($toDelete, (int) $this->id);
        $toDelete = array_unique($toDelete);
        /* Delete CMS Category and its child from database */
        $list = sizeof($toDelete) > 1 ? implode(',', $toDelete) : (int) $this->id;
        Db::getInstance()->exec('DELETE FROM `' . DB_PREFIX . 'category` WHERE `id_category` IN (' . $list . ')');
        /*取消排序可大大加快删除速度*/
        //self::cleanPositions($this->id_parent);
        /* Delete pages which are in categories to delete */
        $result = Db::getInstance()->getAll('
		SELECT `id_rule`
		FROM `' . DB_PREFIX . 'rule`
		WHERE `id_entity` IN (' . $list . ') AND entity="' . get_class($this) . '"');
        foreach ($result as $p) {
            $rule = new Rule((int) $p['id_rule']);
            if (Validate::isLoadedObject($rule)) {
                $rule->delete();
            }
        }
        /* Delete pages which are in categories to delete */
        $result = Db::getInstance()->getAll('
		SELECT `id_product`
		FROM `' . DB_PREFIX . 'product`
		WHERE `id_category_default` IN (' . $list . ')');
        $products = array();
        foreach ($result as $p) {
            $products[] = $p['id_product'];
        }
        /*采用批量删除产品方法*/
        Product::batchDeleteProduct($products);
        Db::getInstance()->exec('DELETE FROM `' . DB_PREFIX . 'product_to_category` WHERE `id_product` IN(' . pSQL($list) . ')');
        return true;
    }