Smarty db
From PeacockWiki
(diff) ←Older revision | Current revision | Newer revision→ (diff)
Contents |
Introduction
Smarty_DB is a php object model written by Trevor peacock for use with the TimeShare project. Smarty_DB generates an object model of a database (currently only MySQL is supported). Smarty_DB uses the Smarty Templating Engine for rendering SQL commands, and also provides plugins to smarty for use in normal HTML templates, allowing many database queries to be done directly from smarty templates with no PHP code required.
Principles
Smarty makes a few assumptions about your database
- Every table that is to be accessable by Smarty_DB has a autonumbered primary key field 'id'.
- Wherever one table references another table, the foreign key field will have the same name as the foreign table, and the foreign key field will have the value of the key in the foreign table, where the key is the field 'id'.
+-----------+ | author | +-----------+ | id | | name | | dob | | | +-----------+ +-----------+ | book | +-----------+ | id | | name | | pub_date | | author | | | +-----------+
This example follows rule #1 and #2. Each table has an 'id' field, and the author field in the book table is a pointer to the id of the author table. This allows Smarty_DB to recognise the link, and automatically allow the programmer to simply reference the foreign table.
//$book is a Smarty_DB object containing a single book object. $author = $book->author;
In this example Smarty_DB recognises the link between book and author, and automatically runs a SQL query to fetch the data related to the author, and returns it as a Smarty_DB object. This can be quite a powerful mechanism for performing lookups
print "Book Title: ".$book->name print "Author: ".$book->author->name;
TODO
- caching
- smarty html plugins
- smarty sql plugins
- php coding
- generator queries
- manual table queries
- manual field queries
Classes
DatabaseModel
- public function __construct(ObjectDatabase $db = null) {
- public static function get_db() {
Smarty_Database extends Smarty
- function Smarty_Database() {
- function display($tpl_path) {
- function fetch($tpl_path = null) {
- function template($tpl_path) {
DBDataObject extends DatabaseModel
- function __construct(ObjectDatabase $db = null, $params = array (), $table = null) {
- function __get($nm) {
- function clear_cache($nm = null) {
- function get($nm) {
- function has_data($nm) {
- function set($nm, $val) {
- function __set($nm, $val) {
- function get_item($nm) {
- function run_query($query, $table = null) {
- function __call($n, $p) {
- static function new_object($table) {
- static function action($query, $params = array ()) {
- static function generate($query, $params = array ()) {
- static function do_generate($query, $params = array ()) {
- function table_name() {
- function changed() {
- function update($add = false) {
- function add() {
- function delete()
ObjectDatabase extends Database
- function __construct() {
- public static function register_class($class_name, $table_name) {
- function smarty_query_exists($script, $table = null) {
- function smarty_query_exists_action($script, $table = null) {
- function smarty_query_exists_generator($script, $table = null) {
- function run_template($filename, $params = array ()) {
- public function data_object($result, $params = array ()) {
- function new_object($table, $result=null, $params=null) {
- function smarty_query_filename_action($script) {
- function smarty_query_filename_generator($script) {
- function smarty_query_filename($script, $table = null) {