cvs: presentations /slides/mvc/example/model db.inc items.inc

From: Date: Sat, 04 Mar 2006 16:45:58 +0000
Subject: cvs: presentations /slides/mvc/example/model db.inc items.inc
Groups: php.pres 
Request: Send a blank email to pres+get-1267@lists.php.net to get a copy of this message
rasmus Sat Mar 4 16:45:58 2006 UTC Modified files: /presentations/slides/mvc/example/model db.inc items.inc Log: Switch to using a static property here to avoid multiple db connections when using this as a base for a larger system http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/db.inc?r1=1.2&r2=1.3&diff_format=u Index: presentations/slides/mvc/example/model/db.inc diff -u presentations/slides/mvc/example/model/db.inc:1.2 presentations/slides/mvc/example/model/db.inc:1.3 --- presentations/slides/mvc/example/model/db.inc:1.2 Thu Mar 2 04:11:06 2006 +++ presentations/slides/mvc/example/model/db.inc Sat Mar 4 16:45:58 2006 @@ -1,11 +1,11 @@ <?php class db { - public $dbh = false; + protected static $dbh = false; function connect() { - $this->dbh = new PDO('sqlite:./model/example.db'); - $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + self::$dbh = new PDO('sqlite:./model/example.db'); + self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } protected function fatal_error($msg) { http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/items.inc?r1=1.4&r2=1.5&diff_format=u Index: presentations/slides/mvc/example/model/items.inc diff -u presentations/slides/mvc/example/model/items.inc:1.4 presentations/slides/mvc/example/model/items.inc:1.5 --- presentations/slides/mvc/example/model/items.inc:1.4 Thu Mar 2 04:11:06 2006 +++ presentations/slides/mvc/example/model/items.inc Sat Mar 4 16:45:58 2006 @@ -11,8 +11,8 @@ function insert($item) { $t = $_SERVER["REQUEST_TIME"]; try { - if(!$this->dbh) $this->connect(); - $stmt = $this->dbh->prepare("INSERT INTO items + if(!self::$dbh) $this->connect(); + $stmt = self::$dbh->prepare("INSERT INTO items (id,cat,sdesc,ldesc,price,ctime) VALUES (NULL,:cat,:sdesc,:ldesc,:price,$t)"); @@ -29,8 +29,8 @@ function modify($item) { try { - if(!$this->dbh) $this->connect(); - $stmt = $this->dbh->prepare("UPDATE items SET + if(!self::$dbh) $this->connect(); + $stmt = self::$dbh->prepare("UPDATE items SET cat=:cat, sdesc=:sdesc, ldesc=:ldesc, price=:price WHERE id=:id"); @@ -51,8 +51,8 @@ if($id!=-1) $where = "where id=".(int)$id; try { - if(!$this->dbh) $this->connect(); - $result = $this->dbh->query("SELECT * FROM items + if(!self::$dbh) $this->connect(); + $result = self::$dbh->query("SELECT * FROM items $where order by ctime desc"); $rows = $result->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) {

« previous php.pres (#1267) next »