/** * @param CategoryMap $categoryMap * @throws \Exception */ public function insertWithRelatedCategory(CategoryMap $categoryMap) { $this->getPdo()->beginTransaction(); $this->getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { $productId = $this->insert(); $categoryMap->setVirtuemartProductId($productId); $filled = []; foreach ($categoryMap->toArray() as $column => $value) { if ($value !== null) { $filled[$column] = $value; } } $columnString = implode(',', array_keys($filled)); $valueString = implode(',', array_fill(0, count($filled), '?')); $prepareQuery = $this->getPdo()->prepare("INSERT INTO " . CategoryManager::TABLE_NAME . " ({$columnString}) VALUES ({$valueString})"); if (!$prepareQuery->execute(array_values($filled))) { throw new \Exception('Cannot execute query'); } $this->getPdo()->commit(); } catch (\Exception $ex) { $this->getPdo()->rollBack(); throw $ex; } }