Jacek Kowalski
2019-02-20 64994ed50bf27f21ec4a17c241f72bc333ed1afe
Use transaction to add mapping to the DB
1 files modified
14 ■■■■■ changed files
lib/database.php 14 ●●●●● patch | view | raw | blame | history
lib/database.php
@@ -20,6 +20,18 @@
        $this->addStatement = $this->pdo->prepare('INSERT OR REPLACE INTO vehicles (id, num, weight) VALUES (?, ?, ?)');
    }
    
    public function beginTransaction() {
        $this->pdo->beginTransaction();
    }
    public function commit() {
        $this->pdo->commit();
    }
    public function rollback() {
        $this->pdo->rollback();
    }
    public function getById($id) {
        $this->getByIdStatement->execute([$id]);
        return $this->getByIdStatement->fetch();
@@ -39,9 +51,11 @@
    }
    
    public function addMapping($mapping) {
        $this->beginTransaction();
        $weight = count($mapping);
        foreach($mapping as $id => $vehicle) {
            $this->add($id, (int)substr($vehicle['num'], 2), $weight);
        }
        $this->commit();
    }
}