[Phphtmllib-devel] SF.net SVN: phphtmllib: [2869] trunk/phphtmllib/src
Status: Beta
Brought to you by:
hemna
From: <bri...@us...> - 2007-08-23 20:52:16
|
Revision: 2869 http://phphtmllib.svn.sourceforge.net/phphtmllib/?rev=2869&view=rev Author: brianrue Date: 2007-08-23 13:52:18 -0700 (Thu, 23 Aug 2007) Log Message: ----------- added join() and join_all() to DBDataObject Modified Paths: -------------- trunk/phphtmllib/src/data/DBDataObject.inc trunk/phphtmllib/src/generator/DBDataObjectTemplateGenerator.inc Modified: trunk/phphtmllib/src/data/DBDataObject.inc =================================================================== --- trunk/phphtmllib/src/data/DBDataObject.inc 2007-08-23 18:24:42 UTC (rev 2868) +++ trunk/phphtmllib/src/data/DBDataObject.inc 2007-08-23 20:52:18 UTC (rev 2869) @@ -504,8 +504,134 @@ return $collection; } + + /** + * Find a single data object by doing a join query + * on this data object's table and one or more other + * tables (in the $tables array). + * The where clause may contain the special bind var + * :self, which will not be bound but instead replaced + * with this object's table name. + * + * $tables is a simple array of values; the values are + * the table names to join and the keys are meaningless. + * The table names may include aliases, i.e. + * array('member m','message ms') which will be utilized + * by the where clause. + * + * @param array $tables + * @param string $where + * @param array $bind_vars + */ + protected function _join($tables, $where, $bind_vars=array()) { + // generate a cache key + // this key should store the original main id cache key + // that stores the actual data + $key = 'join_' . $this->table_name . '|' . implode('|', $tables) . '_' . implode(':', array_keys($bind_vars)) . '_' . implode(':', $bind_vars); + if ($this->allow_find_cache && $this->data->{$this->primary_key} = $this->cache->get($key)) { + // found this id in the cache + // let's load by id + $this->load(); + } + else { + + $this->set_db_object(); + + $where = str_replace(':self', $this->table_name, $where); + + + $tables[] = $this->table_name; + + $tables = implode(', ', $tables); + + try { + $data = $this->db->queryBindOneRow('select ' . $this->table_name . '.* from ' . + $tables . + ' where ' . $where, + $bind_vars); + + } catch (PDOException $e) { + throw new DataObjectException('No results for ' . $tables . ' where ' . $where, + phphtmllibException::DBDATAOBJECT_FIND_NOT_FOUND); + } + + if (!$data) { + throw new DataObjectException('No results for ' . $tables . ' where ' . $where, + phphtmllibException::DBDATAOBJECT_FIND_NOT_FOUND); + } + + $this->set_data($data); + + if ($this->allow_find_cache) { + // cache the key + $this->cache->set($key, $this->data->{$this->primary_key}); + } + } + } + /** + * Find all data objects matching a join query + * on this data object's table and one or more other + * tables (in the $tables array). + * The where clause may contain the special bind var + * :self, which will not be bound but instead replaced + * with this object's table name. + * + * $tables is a simple array of values; the values are + * the table names to join and the keys are meaningless. + * The table names may include aliases, i.e. + * array('member m','message ms') which will be utilized + * by the where clause. + * + * @param array $tables + * @param string $where_clause + * @param array $bind_vars + * @param string $order_clause + * @param string $limit_clause + * @return DataObjectCollection + */ + protected function _join_all($tables, $where_clause, $bind_vars=array(), + $order_clause=NULL, $limit_clause=NULL) { + $this->set_db_object(); + + $where_clause = str_replace(':self', $this->table_name, $where_clause); + $order_clause = str_replace(':self', $this->table_name, $order_clause); + + + $tables[] = $this->table_name; + + $tables = implode(', ', $tables); + + try { + if ($where_clause) { + $where_clause = ' where '.$where_clause; + } + if ($order_clause) { + $order_clause = ' order by ' . $order_clause; + } + $stmt = $this->db->queryBind('select distinct ' . $this->table_name . '.' . $this->primary_key . ' as ' . $this->primary_key . '_distinct, ' . + $this->table_name . '.* from ' . + $tables . $where_clause . ' ' . + $order_clause . ' ' . $limit_clause, + $bind_vars); + } catch (PDOException $e) { + throw new DataObjectException('No results for ' . $tables . ' where ' . $where_clause, + phphtmllibException::DBDATAOBJECT_FIND_NOT_FOUND); + } + + $collection = new DataObjectCollection(); + while ($row = $stmt->fetch()) { + $class = get_class($this); + $class = new $class; + $class->set_data($row); + $collection->add($class); + } + + return $collection; + } + + /** * Returns a related data object * by object name * @@ -637,7 +763,59 @@ return $obj->_find_all($where_clause, $bind_vars, $order_clause, $limit_clause); } + + /** + * Find a single data object by doing a join query + * on this data object's table and one or more other + * tables (in the $tables array). + * The where clause may contain the special bind var + * :self, which will not be bound but instead replaced + * with this object's table name. + * + * $tables is a simple array of values; the values are + * the table names to join and the keys are meaningless. + * The table names may include aliases, i.e. + * array('member m','message ms') which will be utilized + * by the where clause. + * + * @param array $tables + * @param string $where + * @param array $bind_vars + */ + public static function join($class_name, $tables, $where_clause, $bind_vars=array()) { + $obj = new $class_name(); + + return $obj->_join($tables, $where_clause, $bind_vars); + } + + /** + * Find all data objects matching a join query + * on this data object's table and one or more other + * tables (in the $tables array). + * The where clause may contain the special bind var + * :self, which will not be bound but instead replaced + * with this object's table name. + * + * $tables is a simple array of values; the values are + * the table names to join and the keys are meaningless. + * The table names may include aliases, i.e. + * array('member m','message ms') which will be utilized + * by the where clause. + * + * @param array $tables + * @param string $where_clause + * @param array $bind_vars + * @param string $order_clause + * @param string $limit_clause + * @return DataObjectCollection + */ + public static function join_all($class_name, $tables, $where_clause, $bind_vars=array(), + $order_clause=NULL,$limit_clause=NULL) { + $obj = new $class_name(); + return $obj->_join_all($tables, $where_clause, $bind_vars, $order_clause, $limit_clause); + } + /* -- SERIALIZE FUNCTIONS -------------------------------------------------------- */ /** Modified: trunk/phphtmllib/src/generator/DBDataObjectTemplateGenerator.inc =================================================================== --- trunk/phphtmllib/src/generator/DBDataObjectTemplateGenerator.inc 2007-08-23 18:24:42 UTC (rev 2868) +++ trunk/phphtmllib/src/generator/DBDataObjectTemplateGenerator.inc 2007-08-23 20:52:18 UTC (rev 2869) @@ -411,6 +411,36 @@ public static function find_all($where_clause, $bind_vars=array(), $order_clause=NULL, $limit_clause=NULL) { return DBDataObject::find_all(\'{CHILD_NAME}\', $where_clause, $bind_vars, $order_clause, $limit_clause); } + + /** + * This method finds a single data object + * using a join + * + * @param array $tables + * @param string $where ie. \'foo=:foo\' + * @param array $bind_vars + * @return {CHILD_NAME} + */ + public static function join($tables, $where_clause, $bind_vars=array()) { + return DBDataObject::join(\'{CHILD_NAME}\', $tables, $where_clause, $bind_vars); + } + + /** + * This method finds all matching + * data objects using a join + * + * @param array $tables + * @param string $where ie. \'foo=:foo\' + * @param array $bind_vars + * @param string $order_clause \'order by foo\' + * @param string $limit clause \'limit 10,20\' + * @return DataObjectCollection + */ + public static function join_all($tables, $where_clause, $bind_vars=array(), $order_clause=NULL, $limit_clause=NULL) { + return DBDataObject::join_all(\'{CHILD_NAME}\', $tables, $where_clause, $bind_vars, $order_clause, $limit_clause); + } + + // ------------- DATA ACCESS FUNCTIONS --------------- {ACCESS_BLOCK} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |