1

こんにちは、次の関数と次のクラスがあります。問題は、配列のすべてではなく 1 つのキーのみをバインドしていることであり、その理由がわかりません。

public function addData(Array $data)
{
    $this->beginTransaction();
    $this->query('INSERT INTO test (name, date) VALUES (:invoiceName, NOW())');
    $this->bind(':invoiceName', uniqid());
    $this->execute();
    $this->query('INSERT INTO test_table (testId, username, type) VALUES (:invoiceId, :username, :type, )');
    $this->bind(':invoiceId', $this->lastInsertId());
    foreach($data as $key => $value)
    {
        $this->bind(':username', $value['name']);
        $this->bind(':type', $value['type']);
    }
    $this->execute();
    $this->endTransaction();
}

そして、データベース接続に使用しているクラス。

<?php
use PDO;
use PDOException;
class Database
{
    private
        $pdoOptions = array
            (
                PDO::ATTR_PERSISTENT => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_PERSISTENT => true,
            );
    private 
        $connectionConfig = array
            (
                'dsn' => 'mysql:dbname=Test;host=127.0.0.1',
                'username' => 'root',
                'password' => ''
            );
    private $dbh;
    private $stmt;

    public function setOptions($pdoOptions = array())
    {
        $this->pdoOptions = array_merge($this->pdoOptions, $pdoOptions);
    }

    public function getOptions()
    {
        return $this->pdoOptions;
    }

    public function setConnectionConfig($connectionArray = array())
    {
        $this->connectionConfig = array_merge($this->connectionConfig, $connectionArray);
    }

    public function getConnectionConfig()
    {
        return $this->connectionConfig;
    }

    public function createInstance()
    {
        if (!extension_loaded('PDO'))
            throw new PDOException(__CLASS__ . ': The PDO extension is required for this class');
        try {
            $this->dbh = new PDO($this->connectionConfig['dsn'], $this->connectionConfig['username'], $this->connectionConfig['password'], $this->pdoOptions);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    public function query($query)
    {
        $this->stmt = $this->dbh->prepare($query);
    }

    public function bind($param, $value, $type = null)
    {
        if(is_null($type))
        {
            switch(true)
            {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }

        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute()
    {
        $this->stmt->execute();
    }

    public function resultSet($type = PDO::FETCH_ASSOC)
    {
        $this->execute();
        return $this->stmt->fetchAll($type);
    }

    public function singleResult($type = PDO::FETCH_ASSOC)
    {
        $this->execute();
        return $this->stmt->fetch($type);
    }

    public function rowCount()
    {
        return $this->stmt->rowCount();
    }

    public function lastInsertId()
    {
        return $this->dbh->lastInsertId();
    }

    public function beginTransaction()
    {
        $this->dbh->beginTransaction();
    }

    public function endTransaction()
    {
        $this->dbh->commit();
    }

    public function cancelTransaction()
    {
        $this->dbh->rollBack();
    }

    public function debugDumpParams()
    {
        return $this->stmt->debugDumpParams();
    }
}
4

1 に答える 1