/** * associate an array of id_attachment $array to the product $id_product * and remove eventual previous association * * @static * @param $id_product * @param $array * @return bool */ public static function attachToProduct($id_product, $array) { $result1 = Attachment::deleteProductAttachments($id_product); if (is_array($array)) { $ids = array(); foreach ($array as $id_attachment) { if ((int) $id_attachment > 0) { $ids[] = array('id_product' => (int) $id_product, 'id_attachment' => (int) $id_attachment); } } if (!empty($ids)) { $result2 = Db::getInstance()->insert('product_attachment', $ids); } } Product::updateCacheAttachment((int) $id_product); if (is_array($array)) { return $result1 && (!isset($result2) || $result2); } return $result1; }
public static function duplicateAttachments($id_product_old, $id_product_new) { // Get all ids attachments of the old product $sql = 'SELECT `id_attachment` FROM `' . _DB_PREFIX_ . 'product_attachment` WHERE `id_product` = ' . (int) $id_product_old; $results = Db::getInstance()->executeS($sql); if (!$results) { return true; } $data = array(); // Prepare data of table product_attachment foreach ($results as $row) { $data[] = array('id_product' => (int) $id_product_new, 'id_attachment' => (int) $row['id_attachment']); } // Duplicate product attachement $res = Db::getInstance()->insert('product_attachment', $data); Product::updateCacheAttachment((int) $id_product_new); return $res; }