/** * Unit test for eZProductCollectionItem::cleanupList() * * Outline: * 1) Create 40 eZProductCollectionItem objects with a product_collection_id * from 1 to 4 * 2) Call cleanupList with (1, 2) as a parameter * 4) Check that the 20 matching items have been removed * 5) Check that the 20 other, non-matching items haven't been removed **/ public function testCleanupList() { // Create a few collections $row = array('product_collection_id' => 0, 'contentobject_id' => 1, 'item_count' => 1, 'price' => 50, 'is_vat_inc' => 1, 'vat_value' => 19.6, 'discount' => 0, 'name' => __FUNCTION__); $deleteIDArray = $keepIDArray = array(); for ($i = 1; $i < 40; $i++) { $row['productcollection_id'] = ceil($i / 10); $row['name'] = __FUNCTION__ . ":" . $row['productcollection_id'] . "-{$i}"; $item = new eZProductCollectionItem($row); $item->store(); if ($row['productcollection_id'] <= 2) { $deleteIDArray[] = $item->attribute('id'); } else { $keepIDArray[] = $item->attribute('id'); } } eZProductCollectionItem::cleanupList(array(1, 2)); // Check that each item of deleteIDArray has been removed foreach ($deleteIDArray as $id) { $this->assertNull(eZProductCollectionItem::fetch($id)); } // And check that each item of remainingIDArray hasn't been deleted foreach ($keepIDArray as $id) { $this->assertType('eZProductCollectionItem', eZProductCollectionItem::fetch($id)); } }
/** * Removes all product collections based on a product collection ID list * Will also remove the product collection items. * * @param array $productCollectionIDList array of eZProductCollection IDs * * @return void */ static function cleanupList($productCollectionIDList) { $db = eZDB::instance(); $db->begin(); // Purge shipping information associated with product collections being removed. foreach ($productCollectionIDList as $productCollectionID) { eZShippingManager::purgeShippingInfo($productCollectionID); } eZProductCollectionItem::cleanupList($productCollectionIDList); $where = $db->generateSQLINStatement($productCollectionIDList, 'id', false, false, 'int'); $db->query("DELETE FROM ezproductcollection WHERE {$where}"); $db->commit(); }
function collectProductCollections($maxTime = false, $sleepTime = false, $limit = false) { $db = eZDB::instance(); // Create a temporary table for filling in collection ids // that are in use if ($db->databaseName() == 'mysql') { $db->query("DROP TABLE IF EXISTS ezproductcollection_used"); } $db->query("CREATE TABLE ezproductcollection_used ( id int )"); // Fill in collections used by orders $db->query("INSERT INTO ezproductcollection_used (id) SELECT productcollection_id FROM ezorder"); // Fill in collections used by wish lists $db->query("INSERT INTO ezproductcollection_used (id) SELECT productcollection_id FROM ezwishlist"); // Fill in collections used by baskets $db->query("INSERT INTO ezproductcollection_used (id) SELECT productcollection_id FROM ezbasket"); // Create the index for faster selects $db->query("CREATE INDEX ezproductcollection_used_id on ezproductcollection_used( id )"); if ($maxTime === false and $db->hasRequiredServerVersion('4.0', 'mysql')) { // Delete entries which are not in ezproductcollection_used $db->query("DELETE FROM ezproductcollection, ezproductcollection_item, ezproductcollection_item_opt\nUSING ezproductcollection_used\n RIGHT JOIN ezproductcollection\n ON ezproductcollection_used.id = ezproductcollection.id\n LEFT JOIN ezproductcollection_item\n ON ezproductcollection.id = ezproductcollection_item.productcollection_id\n LEFT JOIN ezproductcollection_item_opt\n ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id\nWHERE ezproductcollection_used.id IS NULL"); $db->query($sql); // Remove the temporary table $db->query("DROP TABLE ezproductcollection_used"); return; } // Find all product collections which are not in use if ($db->hasRequiredServerVersion('8', 'oracle')) { $sql = "SELECT ezproductcollection.id\nFROM ezproductcollection_used, ezproductcollection\nWHERE ezproductcollection_used.id = ezproductcollection.id (+) AND\n ezproductcollection_used.id IS NULL"; } else { $sql = "SELECT ezproductcollection.id\nFROM ezproductcollection_used\n RIGHT JOIN ezproductcollection\n ON ezproductcollection_used.id = ezproductcollection.id\nWHERE ezproductcollection_used.id IS NULL"; } if ($limit === false) { $limit = eZDBGarbageCollector::ITEM_LIMIT; } $end = false; if (is_numeric($maxTime)) { $end = time() + $maxTime; } do { $rows = $db->arrayQuery($sql, array('offset' => 0, 'limit' => $limit)); if (count($rows) == 0) { break; } $idList = array(); foreach ($rows as $row) { $idList[] = (int) $row['id']; } eZProductCollectionItem::cleanupList($idList); $ids = implode(', ', $idList); $db->query("DELETE FROM ezproductcollection WHERE id IN ( {$ids} )"); $db->query("DELETE FROM ezproductcollection_used WHERE id IN ( {$ids} )"); // Stop when we used up our time if ($end !== false and time() > $end) { break; } // Sleep a little if required, reduces load on server if ($sleepTime !== false) { sleep($sleepTime); } } while (true); // Remove the temporary table $db->query("DROP TABLE ezproductcollection_used"); }