Skip to main content
added 164 characters in body
Source Link

The trouble I'm foreseeing with this is that every instance of every model will open and maintain its own MySQL connection, and this all happens on a per-request basis. So if 10 people visit a page that calls 10 objects you've just occupied 100/250 connections, 250 being the default max on a fresh MySQL install.

You want things like this to scale in a linear way, not geometrically.

Instead, instantiate a single DB connection and pass that to your models. [Dependency Injection]

Secondly, I would offload calls to things like prepare() and execute() to the Connection object, as that is where you would want to handle related tasks like error handling. eg:

class Connection {
    function query($sql, $params=NULL) {
        if( ! $sth = $this->dbh->prepare($sql) ) {
            $err_arr = $this->dbh->errorInfo();
            $err_msg = sprintf("SQLSTATE ERR: %s<br />\nmySQL ERR: %s<br />\nMessage: %s<br />\n", $err_arr[0], $err_arr[1], $err_arr[2]);
            throw new Exception($err_msg);
        }
        if( ! $sth->execute($params) ) {
            $err_arr = $sth->errorInfo();
            $err_msg = sprintf("SQLSTATE ERR: %s<br />\nmySQL ERR: %s<br />\nMessage: %s<br />\n", $err_arr[0], $err_arr[1], $err_arr[2]);
            throw new Exception($err_msg);
        }
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
}

$resultclass =Model {
    function __construct($conn) {
        $this->query(>conn = $conn;
    'SELECT}
 foo FROM bar WHEREfunction bazinsert() ={
 :param OR bla     $sql = "INSERT INTO foo (bar, baz) VALUES (:param'id, :value)";
        $this->conn->query($sql, array('param'
            'id' => 'shoop'$this->id,
            'value' => $this->value));
    }
}

The trouble I'm foreseeing with this is that every instance of every model will open and maintain its own MySQL connection, and this all happens on a per-request basis. So if 10 people visit a page that calls 10 objects you've just occupied 100/250 connections, 250 being the default max on a fresh MySQL install.

You want things like this to scale in a linear way, not geometrically.

Instead, instantiate a single DB connection and pass that to your models. [Dependency Injection]

Secondly, I would offload calls to things like prepare() and execute() to the Connection object, as that is where you would want to handle related tasks like error handling. eg:

class Connection {
    function query($sql, $params=NULL) {
        if( ! $sth = $this->dbh->prepare($sql) ) {
            $err_arr = $this->dbh->errorInfo();
            $err_msg = sprintf("SQLSTATE ERR: %s<br />\nmySQL ERR: %s<br />\nMessage: %s<br />\n", $err_arr[0], $err_arr[1], $err_arr[2]);
            throw new Exception($err_msg);
        }
        if( ! $sth->execute($params) ) {
            $err_arr = $sth->errorInfo();
            $err_msg = sprintf("SQLSTATE ERR: %s<br />\nmySQL ERR: %s<br />\nMessage: %s<br />\n", $err_arr[0], $err_arr[1], $err_arr[2]);
            throw new Exception($err_msg);
        }
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
}

$result = $conn->query(
    'SELECT foo FROM bar WHERE baz = :param OR bla = :param',
     array('param' => 'shoop'));

The trouble I'm foreseeing with this is that every instance of every model will open and maintain its own MySQL connection, and this all happens on a per-request basis. So if 10 people visit a page that calls 10 objects you've just occupied 100/250 connections, 250 being the default max on a fresh MySQL install.

You want things like this to scale in a linear way, not geometrically.

Instead, instantiate a single DB connection and pass that to your models. [Dependency Injection]

Secondly, I would offload calls to things like prepare() and execute() to the Connection object, as that is where you would want to handle related tasks like error handling. eg:

class Connection {
    function query($sql, $params=NULL) {
        if( ! $sth = $this->dbh->prepare($sql) ) {
            $err_arr = $this->dbh->errorInfo();
            $err_msg = sprintf("SQLSTATE ERR: %s<br />\nmySQL ERR: %s<br />\nMessage: %s<br />\n", $err_arr[0], $err_arr[1], $err_arr[2]);
            throw new Exception($err_msg);
        }
        if( ! $sth->execute($params) ) {
            $err_arr = $sth->errorInfo();
            $err_msg = sprintf("SQLSTATE ERR: %s<br />\nmySQL ERR: %s<br />\nMessage: %s<br />\n", $err_arr[0], $err_arr[1], $err_arr[2]);
            throw new Exception($err_msg);
        }
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
}

class Model {
    function __construct($conn) {
        $this->conn = $conn;
    }
    function insert() {
        $sql = "INSERT INTO foo (bar, baz) VALUES (:id, :value)";
        $this->conn->query($sql, array(
            'id' => $this->id,
            'value' => $this->value));
    }
}
Source Link

The trouble I'm foreseeing with this is that every instance of every model will open and maintain its own MySQL connection, and this all happens on a per-request basis. So if 10 people visit a page that calls 10 objects you've just occupied 100/250 connections, 250 being the default max on a fresh MySQL install.

You want things like this to scale in a linear way, not geometrically.

Instead, instantiate a single DB connection and pass that to your models. [Dependency Injection]

Secondly, I would offload calls to things like prepare() and execute() to the Connection object, as that is where you would want to handle related tasks like error handling. eg:

class Connection {
    function query($sql, $params=NULL) {
        if( ! $sth = $this->dbh->prepare($sql) ) {
            $err_arr = $this->dbh->errorInfo();
            $err_msg = sprintf("SQLSTATE ERR: %s<br />\nmySQL ERR: %s<br />\nMessage: %s<br />\n", $err_arr[0], $err_arr[1], $err_arr[2]);
            throw new Exception($err_msg);
        }
        if( ! $sth->execute($params) ) {
            $err_arr = $sth->errorInfo();
            $err_msg = sprintf("SQLSTATE ERR: %s<br />\nmySQL ERR: %s<br />\nMessage: %s<br />\n", $err_arr[0], $err_arr[1], $err_arr[2]);
            throw new Exception($err_msg);
        }
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
}

$result = $conn->query(
    'SELECT foo FROM bar WHERE baz = :param OR bla = :param',
     array('param' => 'shoop'));