Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Andrianov A.M.
- * @param $xml
- * @param $db
- * @return string
- * Продукты со значениями характеристик
- */
- static public function productsXML($xml, $db)
- {
- $productTable = Product::getTableSchema()->name;
- $productValueTable = ProductValue::getTableSchema()->name;
- ini_set('memory_limit', '1000M');
- // $db->createCommand("DELETE FROM $productTable")->execute();
- // $db->createCommand("DELETE FROM $productValueTable")->execute();
- foreach($xml->PriceItem as $product){
- if(!empty($product->Id)) {
- $n = $db->createCommand("INSERT INTO $productTable SET code=:code, `name`=:name, cost=:cost,
- cost_old=:cost_old,
- remainder_amount=:remainder_amount, category_id=:category_id,
- product_weight=:product_weight, parent_code=:parent_code, bonuses=:bonuses
- ON DUPLICATE KEY UPDATE `name`=:name , cost=:cost, product_weight=:product_weight,
- cost_old=:cost_old, remainder_amount=:remainder_amount, parent_code=:parent_code ", [
- ':code' => $product->Id,
- ':name' => htmlspecialchars($product->Nametovar),
- ':cost' => $product->Cost,
- ':cost_old' => $product->CostOld ? $product->CostOld : 0,
- ':remainder_amount' => $product->Quantity,
- ':category_id' => $product->Group_Id,
- ':product_weight' => null,
- ':bonuses' => $product->Cost / Product::BONUSES_COST,
- ':parent_code' => empty($product->Parent_Id) ? null : $product->Parent_Id,
- ])->execute();
- //значения характеристик продукта
- $db->createCommand("DELETE FROM $productValueTable WHERE product_code='{$product->Id}'")->execute();
- foreach ($product->Properties as $option){
- if(!empty($option->Value)) {
- $characteristics = CategoryCharacteristics::findOne(['option_id' => $option->Option_id]);
- $characteristicsName = $characteristics->name;
- $characteristicsMeasure = $characteristics->measure;
- $db->createCommand("INSERT INTO $productValueTable SET characteristics_id=:characteristics_id,
- value=:value, characteristics_name=:characteristics_name, product_code=:product_code,
- category_id=:category_id, characteristics_measure=:characteristics_measure
- ", [
- ':characteristics_id' => "$option->Option_id",
- ':value' => htmlspecialchars($option->Value),
- ':characteristics_name' => htmlspecialchars($characteristicsName),
- ':product_code' => "$product->Id",
- ':category_id' => "$product->Group_Id",
- ':characteristics_measure' => "$characteristicsMeasure",
- ])->execute();
- }
- }
- }
- }
- //связываем подчиненые продукты с главными
- $products = Product::find()
- ->innerJoinWith('parentProduct pr')
- ->where(['not',['product.parent_code'=>null]])
- ->all();
- foreach ($products as $product){
- $parentId = $product->parentProduct->id;
- $product->product_id = $parentId;
- $product->save(false);
- //$db->createCommand("UPDATE product SET product_id={$parentId} WHERE id='{$product->id}'")->execute();
- }
- ini_set('memory_limit', '250M');
- return 'Продукты и значения характеристик добавлены в базу данных';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement