3 Stimmen

Ich verwende das Unit Of Work Design Pattern mit PHP für mein Projekt.

Ich arbeite an einem Projekt, das das Unit-of-Work-Designmuster verwendet. Es enthält Storage, EntityCollection,..... Der Code des Unit of Work enthält die PDO Adapter-Code-Datei. Aber der Adapter ist nicht vollständig, um Abfragen auszuführen. Ich habe die Dateien irgendwo gefunden. Ich kann nicht die Abfragen erstellen, die ich möchte. Jedes Mal, wenn ich Code schreibe, stoße ich auf viele Probleme im Zusammenhang mit Abfragen. Können Sie mir bitte helfen, es flexibler zu gestalten... Zum Beispiel möchte ich folgendes ausführen: $db->select()->where('id>:id')->andWhere('')->orWHere()->orderBy()->groupBy()->having()->fetch()/fetchOne(); oder ähnliches.

config = compact("dsn", "username", "password", "driverOptions");
        $this->database = $driverOptions['db_name'];
    }

    public function getStatement() {
        if ($this->statement === null) {
            throw new \PDOException(
            "Es gibt kein PDOStatement-Objekt zur Verwendung.");
        }
        return $this->statement;
    }

    public function connect() {
        // Wenn bereits ein PDO-Objekt vorhanden ist, frühzeitig zurückgeben
        if ($this->connection) {
            return;
        }

        try {
            $this->connection = new \PDO(
                    $this->config["dsn"], $this->config["username"], $this->config["password"], $this->config["driverOptions"]);
            $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            $this->connection->setAttribute(
                    \PDO::ATTR_EMULATE_PREPARES, false);
        } catch (\PDOException $e) {
            throw new \RunTimeException($e->getMessage());
        }
    }

    public function disconnect() {
        $this->connection = null;
    }

    public function prepare($sql, array $options = array()) {
        $this->connect();

        try {
            $this->statement = $this->connection->prepare($sql, $options);

            return $this;
        } catch (\PDOException $e) {
            throw new \RunTimeException($e->getMessage());
        }
    }

    public function execute(array $parameters = array()) {

        try {
            $this->getStatement()->execute($parameters);
            return $this;
        } catch (\PDOException $e) {
            throw new \RunTimeException($e->getMessage());
        }
    }

    public function countAffectedRows() {
        try {
            return $this->getStatement()->rowCount();
        } catch (\PDOException $e) {
            throw new \RunTimeException($e->getMessage());
        }
    }

    /**
     * countAffectedRows iin Alias
     */
    public function count() {
        try {
            return $this->getStatement()->rowCount();
        } catch (\PDOException $e) {
            throw new \RunTimeException($e->getMessage());
        }
    }

    public function getLastInsertId($name = null) {
        $this->connect();
        return $this->connection->lastInsertId($name);
    }

    public function fetch($fetchStyle = null, $cursorOrientation = null, $cursorOffset = null) {
        if ($fetchStyle === null) {
            $fetchStyle = $this->fetchMode;
        }

        try {
            return $this->getStatement()->fetch($fetchStyle, $cursorOrientation, $cursorOffset);
        } catch (\PDOException $e) {
            throw new \RunTimeException($e->getMessage());
        }
    }

    public function fetchAll($fetchStyle = null, $column = 0) {
        if ($fetchStyle === null) {
            $fetchStyle = $this->fetchMode;
        }

        try {
            return $fetchStyle === \PDO::FETCH_COLUMN ? $this->getStatement()->fetchAll($fetchStyle, $column) : $this->getStatement()->fetchAll($fetchStyle);
        } catch (\PDOException $e) {
            throw new \RunTimeException($e->getMessage());
        }
    }

    public function select($table, $bind = array(), $where = "", $options = array()) {
        if (count($bind) > 0) {
            foreach ($bind as $col => $value) {
                unset($bind[$col]);
                $bind[":" . $col] = $value;
            }
        }
        if (isset($options['fields'])) {
            $fields = $options['fields'];
        } else {
            $fields = '*';
        }
        $sql = "SELECT " . $fields . " FROM " . $table . " ";

        if (strlen($where) > 2) {
            $sql .= "WHERE " . $where;
        }

//        set_flash($sql);
        $this->prepare($sql)
                ->execute($bind);

        return $this;
    }

    public function query($sql, array $bind = array()) {
        if (is_array($bind)) {
            foreach ($bind as $col => $value) {
                unset($bind[$col]);
                $bind[":" . $col] = $value;
            }
        }

        $this->prepare($sql)
                ->execute($bind);
        return $this;
    }

    public function insert($table, array $bind) {
        $cols = implode(", ", array_keys($bind));
        $values = implode(", :", array_keys($bind));
        foreach ($bind as $col => $value) {
            unset($bind[$col]);
            $bind[":" . $col] = $value;
        }

        $sql = "INSERT INTO " . $table
                . " (" . $cols . ")  VALUES (:" . $values . ")";
        return (int) $this->prepare($sql)
                        ->execute($bind)
                        ->getLastInsertId();
    }

    public function update($table, array $bind, $where = "") {
        $set = array();
        foreach ($bind as $col => $value) {
            unset($bind[$col]);
            $bind[":" . $col] = $value;
            $set[] = $col . " = :" . $col;
        }

        $sql = "UPDATE " . $table . " SET " . implode(", ", $set)
                . (($where) ? " WHERE " . $where : " ");
        return $this->prepare($sql)
                        ->execute($bind)
                        ->countAffectedRows();
    }

    public function delete($table, $where = "") {
        $sql = "DELETE FROM " . $table . (($where) ? " WHERE " . $where : " ");
        return $this->prepare($sql)
                        ->execute()
                        ->countAffectedRows();
    }

    public function fetchAllTables() {
        $sql = "SHOW TABLES FROM " . $this->database;
        $this->prepare($sql)
                ->execute();

        return $this;
    }

    public function fetchAllFields($table) {
        $sql = "SHOW FIELDS FROM " . $table;
        $this->prepare($sql)
                ->execute();

        return $this;
    }

}

---Der vollständige Code ist hier--- https://github.com/batmunkhcom/mbm/tree/master/src/D

4voto

Mamuz Punkte 1720

Zunächst einmal besteht das Anliegen eines UnitOfWork-Musters darin, alles zu verfolgen, was während einer Geschäftstransaktion geschieht, das sich auf die Datenbank auswirken kann. Nach Transaktionen wird festgestellt, was alles getan werden muss, um die Datenbank aufgrund Ihrer Arbeit zu ändern. Ihre Klasse hat andere Anliegen.

Es sieht aus wie eine God-Class (Antimuster). Es hat mehr als eine Verantwortung: Verbindung, PeparedStatement, Ausführung und andere Helfer. Es ist ziemlich schwer zu skalieren und zu pflegen. Versuchen Sie, alle Verantwortlichkeiten in unterschiedliche Klassen aufzuteilen auf SOLID-Weise mit entsprechendem Designmuster.

Ihre Idee in Ihrem Code ist bereits von anderen Frameworks umgesetzt worden. Es ist sehr schwer, es noch einmal zu implementieren. Zum Beispiel können Sie das Doctrine ORM/DBAL Framework ausprobieren.

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X