Skip to main content
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            return$return = $f($this);
            $this->dbConn->commit();
            return $return;
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}
public function multiQueryTransavtionmultiQueryTransaction($queries)
{
    $this->transaction( function () use ($queries) {
        foreach ($queries as $row)
        {
            $this->query($row['query'], $row['params']);
        }
    });
}
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            return $f($this);
            $this->dbConn->commit();
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}
public function multiQueryTransavtion($queries)
{
    $this->transaction( function () use ($queries) {
        foreach ($queries as $row)
        {
            $this->query($row['query'], $row['params']);
        }
    });
}
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            $return = $f($this);
            $this->dbConn->commit();
            return $return;
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}
public function multiQueryTransaction($queries)
{
    $this->transaction( function () use ($queries) {
        foreach ($queries as $row)
        {
            $this->query($row['query'], $row['params']);
        }
    });
}
added 28 characters in body
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            return $f($this);
            $this->dbConn->commit();
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}

and then using this generic transaction() method we can create a helper function for the specific multiple insertqueries case

public function multiInsertmultiQueryTransavtion($queries)
{
    $this->transaction( function () use ($queries) {
        foreach ($queries as $row)
        {
            $this->query($row['query'], $row['params']);
        }
    });
}
$insert_query[] = [
    "query" => "INSERT INTO mytable (name, text) VALUES (:name, :text);",
    "params" => [":name" => "Name Test", ":text" => "Text Test"],
];
$insert_query[] = [
    "query" => "INSERT INTO mytable (name, text) VALUES (:name, :text);",
    "params" => [":name" => "Another Name", ":text" => "Another Text"],
];

$pdo = new PDO ...;
$myQuery = new MyQuery($pdo);
$myQuery->multiInsert>multiQueryTransaction($insert_query);
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            $f($this);
            $this->dbConn->commit();
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}

and then using this generic transaction() method we can create a helper function for the specific multiple insert case

public function multiInsert($queries)
{
    $this->transaction( function () use ($queries) {
        foreach ($queries as $row)
        {
            $this->query($row['query'], $row['params']);
        }
    });
}
$insert_query[] = [
    "query" => "INSERT INTO mytable (name, text) VALUES (:name, :text);",
    "params" => [":name" => "Name Test", ":text" => "Text Test"],
];
$insert_query[] = [
    "query" => "INSERT INTO mytable (name, text) VALUES (:name, :text);",
    "params" => [":name" => "Another Name", ":text" => "Another Text"],
];

$pdo = new PDO ...;
$myQuery = new MyQuery($pdo);
$myQuery->multiInsert($insert_query);
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            return $f($this);
            $this->dbConn->commit();
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}

and then using this generic transaction() method we can create a helper function for the specific multiple queries case

public function multiQueryTransavtion($queries)
{
    $this->transaction( function () use ($queries) {
        foreach ($queries as $row)
        {
            $this->query($row['query'], $row['params']);
        }
    });
}
$insert_query[] = [
    "query" => "INSERT INTO mytable (name, text) VALUES (:name, :text);",
    "params" => [":name" => "Name Test", ":text" => "Text Test"],
];
$insert_query[] = [
    "query" => "INSERT INTO mytable (name, text) VALUES (:name, :text);",
    "params" => [":name" => "Another Name", ":text" => "Another Text"],
];

$pdo = new PDO ...;
$myQuery = new MyQuery($pdo);
$myQuery->multiQueryTransaction($insert_query);
added 539 characters in body
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51
try {
    $pdo->beginTransaction();
    foreach ($data as $row)
    {
        $pdo->prepare($row['sql'])->execute($row['params']);
    }
    $pdo->commit();
}catch (Exception\Throwable $e){
    $pdo->rollback();
    throw $e;
}
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            $f($this);
            $this->dbConn->commit();
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}

So it can be used like this

$myQuery->transaction( function () use (/* variables you need*/) {
    // write any code you want to wrap into transaction
});

and then using this generic transaction()transaction() method we can create a helper function for the specific multiple insert case

Also, regarding the MyQuery() class in general, I would recommend to read this coding standard
and this article of mine, Your first database wrapper's childhood diseases that can explain some flaws and obsoleted parts in the existing code that I fixed in my version.

try {
    $pdo->beginTransaction();
    foreach ($data as $row)
    {
        $pdo->prepare($row['sql'])->execute($row['params']);
    }
    $pdo->commit();
}catch (Exception $e){
    $pdo->rollback();
    throw $e;
}
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            $f($this);
            $this->dbConn->commit();
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}

and then using this generic transaction() method we can create a helper function for the specific multiple insert case

try {
    $pdo->beginTransaction();
    foreach ($data as $row)
    {
        $pdo->prepare($row['sql'])->execute($row['params']);
    }
    $pdo->commit();
}catch (\Throwable $e){
    $pdo->rollback();
    throw $e;
}
class MyQuery
{
    public $dbConn;

    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
    }

    public function query($query, $params) {
        $stmt = $this->dbConn->prepare($query);
        $stmt->execute($params);
        return $stmt;
    }

    public function transaction(Callable $f)
    {
        try {
            $this->dbConn->beginTransaction();
            $f($this);
            $this->dbConn->commit();
        } catch (\Throwable $e) {
            $this->dbConn->rollBack();
            throw $e;
        }
    }
}

So it can be used like this

$myQuery->transaction( function () use (/* variables you need*/) {
    // write any code you want to wrap into transaction
});

and then using this generic transaction() method we can create a helper function for the specific multiple insert case

Also, regarding the MyQuery() class in general, I would recommend to read this coding standard
and this article of mine, Your first database wrapper's childhood diseases that can explain some flaws and obsoleted parts in the existing code that I fixed in my version.

deleted 4 characters in body
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51
Loading
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51
Loading