table = $table; if(!$this->table) { $this->table = strtolower(substr(get_class($this), 0, -6) . "s"); } $this->conn =& Registry::get('db'); } /** * @abstract * Subclass must define this to instantiate correct class */ function createInstance() { } /** * Find that should work for simple 1 table active record objects */ function find($id) { $sql = $this->find_statement(); $row = $this->conn->selectOne($sql, $id, __FILE__, __LINE__); if(!$row) return $row; $obj = $this->createInstance(); $obj->fromArray($row); return $obj; } /** * Returns default SQL to find by ID * @return string SQL */ function find_statement() { return "select * from $this->table where id=?"; } /** * @deprecated */ function _find($sql, $id) { trigger_error("_find() is deprecated in " . get_class($this)); $row = $this->conn->selectOne($sql, $id, __FILE__, __LINE__); if( !$row ) { return $row; } //create one $obj = $this->createInstance(); $keys = array_keys($row); foreach( $keys as $key ) { $obj->set($key, $row[$key] ); } $obj -> id = $row['id']; return $obj; } function findAll($arr = '', $limit = 0, $offset = 0 ) { $args = ''; $sql = "select * from $this->table"; if( $arr ) { $where = ''; $args = array(); foreach ( $arr as $i => $v ) { $where .= "$i = '".$this->conn->escape($v)."' and "; $args[] = $v; } $where = rtrim($where, " and " ); $sql .= " where $where"; } return $this->_findAll($sql, $limit, $offset); } /** * @access private * generic find method */ function _findAll($sql, $limit=0, $offset=0) { if($limit) { $sql .= " limit $limit offset $offset"; } $rows = $this->conn->selectRows($sql, '', __FILE__, __LINE__); if( !$rows ) { return $rows; } // FALSE or null $ret = array(); foreach( $rows as $row ) { $obj = $this->createInstance(); $obj-> fromArray($row); $ret[] = $obj; } return $ret; } } ?>