/** * Update many rows in table {delivery_method_options} * @param array $data * @return bool */ public function updateMany(array $data) { # get hardcoded store id $this->store_id = $this->getStoreID(); $statement = $this->_db->prepare("UPDATE " . self::TABLE_NAME . " SET `url` = ? , `name` = ?, `weight_from` = ?, `weight_to` = ?, `notes` = ? WHERE id = ? AND store_id = ?"); if ($statement === false) { $response = new Response('Prepare statement has error ' . htmlspecialchars($this->_db->error), 200, true); $response->toJSON(); } foreach ($data as $key => $range) { $this->prepareVars($data); $bind = $statement->bind_param('iiissdd', $this->id, $this->delivery_method_id, $this->store_id, $this->url, $this->name, $this->weight_from, $this->weight_to); if ($bind === false) { $response = new Response('Bind has errors: ' . htmlspecialchars($statement->error), 200, true); $response->toJSON(); } $execute = $statement->execute(); if ($execute === false) { $response = new Response('Query was unable to execute: ' . htmlspecialchars($statement->error), 200, true); $response->toJSON(); } } $statement->close(); return true; }
/** * Update more than one row * @param array $data * @return bool */ public function updateMany(array $data) { # get hardcoded store id $this->store_id = $this->getStoreID(); $statement = $this->_db->prepare("UPDATE " . self::TABLE_NAME . " SET `range_from` = ? , `range_to` = ?, `price` = ?, `active` = ? WHERE id = ? AND store_id = ?"); if ($statement === false) { $response = new Response('Prepare statement has error ' . htmlspecialchars($this->_db->error), 200, true); $response->toJSON(); } foreach ($data as $key => $range) { $this->prepareVars($range); $bind = $statement->bind_param('dddiii', $this->range_from, $this->range_to, $this->price, $this->active, $this->id, $this->store_id); if ($bind === false) { $response = new Response('Bind has errors: ' . htmlspecialchars($statement->error), 200, true); $response->toJSON(); } $execute = $statement->execute(); if ($execute === false) { $response = new Response('Query was unable to execute: ' . htmlspecialchars($statement->error), 200, true); $response->toJSON(); } } $statement->close(); return true; }
/** * Used to save/update/delete all data at once * @param $data */ private function saveAll($data) { # get hardcoded store id $this->store_id = $this->getStoreID(); $methods = $data['methods']; $rangesToDelete = $data['toDelete']; $optionsModel = new DeliveryMethodOptions(); $rangesModel = new DeliveryMethodRanges(); $statementUpdateMethod = $this->_db->prepare("UPDATE " . self::TABLE_NAME . " SET `store_id`= ?, `status` = ?, `fixed_price` = ? WHERE id = ?"); if ($statementUpdateMethod === false) { $response = new Response('Prepare statement has error ' . htmlspecialchars($this->_db->error), 200, true); $response->toJSON(); } # used to save ranges after $rangesUpdate = array(); $rangesSave = array(); # used to save options $optionsUpdate = array(); $optionsSave = array(); foreach ($methods as $key => $method) { if (isset($method['options'])) { # if ID is not set, that means we are creating new row if (!isset($method['options']['id']) || $method['options']['id'] == 0) { $optionsSave[] = $method['options']; } else { $optionsUpdate[] = $method['options']; } } if (isset($method['ranges'])) { $ranges = $method['ranges']; # prepare ranges for bulk update/add/delete foreach ($ranges as $rKey => $range) { if (!isset($range['id']) || $range['id'] == 0) { $rangesSave[] = $range; } else { $rangesUpdate[] = $range; } } } # Validate results $bind = $statementUpdateMethod->bind_param('iidi', $this->store_id, $method['status'], $method['fixed_price'], $method['id']); if ($bind === false) { $response = new Response('Bind has errors: ' . htmlspecialchars($statementUpdateMethod->error), 200, true); $response->toJSON(); } $execute = $statementUpdateMethod->execute(); if ($execute === false) { $response = new Response('Query was unable to execute: ' . htmlspecialchars($statementUpdateMethod->error), 200, true); $response->toJSON(); } } $statementUpdateMethod->close(); # it is cheaper for server to have few more loops trough this small number of data # than to have more openings of mysqli preparation statements and closing of them # save all new ranges if (!empty($rangesSave)) { $rangesModel->saveMany($rangesSave); } # update existing ranges if (!empty($rangesUpdate)) { $rangesModel->updateMany($rangesUpdate); } # save new options if (!empty($optionsSave)) { $optionsModel->saveMany($optionsSave); } # update existing options if (!empty($optionsUpdate)) { $optionsModel->updateMany($optionsUpdate); } # Ranges to delete if (!empty($rangesToDelete)) { $rangesModel->deleteMany($rangesToDelete); } $response = new Response(array('execution' => true)); $response->toJSON(); }