Gumbo_Record
Variables
- $_columns
- $_columns_count
- $_columns_extra
- $_columns_mod
- $_loaded
- $_maps
- $_modified
- $_primary_value
- $_prototype
- $_read_only
Methods
- __construct
- add
- addMap
- asXML
- create
- decrement
- get
- getAllColumns
- getAllColumnsCount
- getAllColumnsExtra
- getAllColumnsMod
- getMap
- getPrimaryKey
- getPrimaryValue
- getPrototype
- getTable
- getXMLParent
- increment
- init
- isLoaded
- isMap
- isModified
- isReadOnly
- kill
- load
- loadXML
- map
- removeMap
- reset
- resetMaps
- save
- set
- setPrimaryValue
- setReadOnly
- sub
- unload
- _getCallback
- _getMap
- _isColumn
- _isColumnCount
- _isColumnExtra
- _isColumnMod
- _reload
- _setLoaded
- _setModified
- __get
- __isset
- __set
- __sleep
- __unset
- __wakeup
Generic Record Class
The Record Class represents a single database record from a table. The methods will provide access to the data of the record. If the class contains a database record, the values will be read only. In order to change records, the class must be extended.
Once the Record is loaded, the program will be able to access the internals of the class. This can be accomplished by calling the get method with the column name or using the overloaded __get method.
In order to manipulate database records, the Gumbo_Record class must be extended. The class will represent a database table. The child class must define the table structure to the Gumbo_Record parent. It should also define get and set methods for each column. A 'getColumnName' method will return the value. A 'setColumnName' method will set the value, forcing the data to be validated and filtered as necessary. The only way to modify Gumbo_Records is through a child class.
Implementation for a get method:
public function get[Column] () { return $this->get ("column_name");
Implementation for a set method:
public function setColumn ($val) { // perform validation/filtering, throw necessary Exceptions // do not set value if validation fails ... $this->set ("column_name", $val); }
The Gumbo_Record class contains some very advanced features for accessing database information. These features are not necessary, but are very useful. The first step is to pass the value to the parent Constructor.
public function __construct ($id=null) { parent::__construct ($id); ... }
The argument can be an integer or an array. If an integer is passed, it will be used as the primary key column value to run a single SELECT statement against the database. If an array is passed, it should be an associative array returned from a database query. The keys in the array are the column names. (ie. mysql_fetch_assoc ()). This feature is useful when returning multiple rows of data, passing the associative array into the Gumbo_Record object. If returning multiple records, this feature will avoid having to run a single query for each Gumbo_Record.
The child Record should instantiate a Prototype. This will pass important setup information about the child Class and the Database Table it represents. The Prototype will define the Table Name, the Primary Key, Table Columns, and more (see Gumbo_Abstract_Prototype). The best implementation of this feature is to create a single Prototype object during the programs lifetime. This can be accomplished by defining the Prototype as a Singleton, or setting a static prototype property inside the child Record. The child Record will then initialize the Prototype, passing the object to the parent 'init' method. (The implementation is if a Prototype is not a Singleton. Make the appropriate substitution to retrieve a Prototype object for Singletons)
// Singleton class User_Prototype extends Gumbo_Record_Prototype implements Gumbo_Interface_Singleton { ... } // static property in Record class User extends Gumbo_Record { private static $_prototype; public function __construct ($id=null) { ... if (User::$_prototype == null) { User::$_prototype = new User_Prototype (); } $this->init (User::$_prototype); $this->load (); // populates the Record } ... }
The following features will help make the Record more dynamic. This includes using maps to transmit data, or loading data through XML.
COUNT COLUMN (optional) This is a special feature, marking a registered column as a count column. This is a column whose value is an integer, used for counting. For example, if a table wants to track the number of times a record has been viewed 'view', the 'view' column could be registered as COUNT COLUMN and use the increment/decrement methods for easy changes. Count Columns are defined by the Prototype.
CALLBACKS (optional) The Gumbo_Record class performs four types of queries: "SELECT,INSERT,UPDATE,DELETE". The four methods (_reload,create,save,kill) each have defined queries to perform the single operation. In some cases, a more advanced or specific query is required. This is where the CALLBACKS are useful. The programmer can define more complex queries, placing them in a separate method. The Prototype would define the callback functions for each operation. If a callback is not defined, the Gumbo_Record class will use the pre-programmed defaults. (see Gumbo_Record_Prototype for more details)
MAPS (optional) The Gumbo_Record class contains four predefined Maps. This will associate the column names with particular elements. Maps are defined by the Prototype. In order to use a map, simple call the 'map' method, passing the appropriate data and which map to use. This is best used with Form Input.
JOIN QUERIES There will be instances where accessing information from one table requires information from another table by means of a join. This complex query should be placed inside a callback function. The associative array returned will contain the columns from the table including keys referencing the joined table data. The Gumbo_Record class takes the extra columns created by a join and places them in a special location called ColumnsExtra. Extra JOIN data will be read only. The child class should define 'get' methods to access the information. The parent get method just requires the column name and the value will be returned.
XML The child constructor should define the parent XML element name in the constructor. This will be used to format an XML string or parse a supplied string.
The XML feature will accept an XML string containing the data to apply to the Gumbo_Record. This will be applied to the column 'xml' Map. The method will require the name of the Parent XML element name. The parent element should contain an 'id' attribute with the value of the primary column key.
The next part of the feature will return the object as a formatted XML string. This will use the 'xml' Map. The formatted string will be surrounded by the parent XML value.
Author(s): |
Michael Luster <mluster79@yahoo.com> |
License: | New BSD License |
Copyright: | Copyright (c) 2007, iBayou, Michael Luster |
Link: | http://sourceforge.net/projects/phpgumbo |
Version: | 0.0.1 |
Implements interfaces:
Class Variables
Class Methods
Constructor
Parameter(s):
- (int|array) $val
[ Top ]
Adds (increments) to the current count columns value
Parameter(s):
- (string) $column :: count column name
- (int) $num :: increases by the supplied number
- precondition:
_isColumnCount ($column) - throws:
Gumbo_Exception
[ Top ]public void addMap ( $map, [string $key], [bool $replace] ) [line 1179]Adds a Map (not used)
Parameter(s):
- (Gumbo_Interface_Map) $map
- (string) $key :: reference string to the Map object (null is 'method')
- (bool) $replace :: replaces original if set
- postcondition:
remove all non-alphanumeric characters (including underscores) from the key - throws:
Gumbo_Exception
[ Top ]public string asXML ( [string $parent] ) [line 1005]Returns the object as an XML formatted string
Parameter(s):
- (string) $parent :: parent element name
- throws:
Gumbo_Exception
[ Top ]Gumbo_Exception - precondition:
!isReadOnly - precondition:
!isLoaded () - precondition:
isModified () - uses:
Gumbo_Query
[ Top ]public void decrement ( string $column, [int $num], [bool $allow_negative] ) [line 850]Wrapper to sub() method
Parameter(s):
- (string) $column :: count column name
- (int) $num :: decreases by the supplied number
- (bool) $allow_negative :: allows negative values
[ Top ]public mixed get ( string $column ) [line 978]Returns the value of a column
Parameter(s):
- (string) $column
- throws:
Gumbo_Exception
[ Top ]getPrototype()
[ Top ]getPrototype()
[ Top ]getPrototype()
[ Top ]public void increment ( string $column, [int $num] ) [line 790]Wrapper to add() method
Parameter(s):
- (string) $column :: count column name
- (int) $num :: increases by the supplied number
[ Top ]protected void init ( Gumbo_Record_Prototype $prototype ) [line 294]Initializes the Record by passing a Record_Prototype
Parameter(s):
- (Gumbo_Record_Prototype) $prototype
- throws:
Gumbo_Exception
[ Top ]public bool isMap ( [string $key] ) [line 1279]Returns if the Map exists
Parameter(s):
- (string) $key :: reference key string (null is 'method')
- throws:
Gumbo_Exception
[ Top ]!isReadOnly () - throws:
Gumbo_Exception - precondition:
isLoaded () - uses:
Gumbo_Query
[ Top ]public void loadXML ( string $xml, [string $parent] ) [line 354]Loads an XML string, but only the first level of the parent
Parameter(s):
- (string) $xml
- (string) $parent :: name of top level XML element
- throws:
Gumbo_Exception
[ Top ]public void map ( array $data, [string $key] ) [line 1208]Maps information into the Mapper
Parameter(s):
- (array) $data :: where key=>"form field name", value=>user data
- (string) $key :: Map reference key (null is 'method')
- throws:
Gumbo_Exception - precondition:
isMap("method") - precondition:
isMap(key)
[ Top ]public void removeMap ( [string $key] ) [line 1188]Deletes a Map (not used)
Parameter(s):
- (string) $key
- throws:
Gumbo_Exception
[ Top ]public void reset ( ) [line 628]Resets the object by removing the values from the modified array
[ Top ]no maps
[ Top ]Gumbo_Exception - precondition:
!isReadOnly () - precondition:
isLoaded () - precondition:
isModified () - uses:
Gumbo_Query
[ Top ]protected void set ( string $column, [mixed $val] ) [line 722]Sets the value of a table column
Parameter(s):
- (string) $column :: table column name
- (mixed) $val :: primitive type value
- precondition:
!isReadOnly () - throws:
Gumbo_Exception
[ Top ]public void setPrimaryValue ( mixed $val ) [line 641]Sets the primary key column value
Parameter(s):
- (mixed) $val
- throws:
Gumbo_Exception
[ Top ]public void setReadOnly ( bool $val ) [line 698]Sets the object as read only
Parameter(s):
- (bool) $val
- throws:
Gumbo_Exception
[ Top ]public void sub ( string $column, [int $num], [bool $allow_negative] ) [line 802]Subtracts (decrement) one from the count column value
Parameter(s):
- (string) $column :: count column name
- (int) $num :: decreases by the supplied number
- (bool) $allow_negative :: allows negative values
- precondition:
_isColumnCount ($column) - throws:
Gumbo_Exception
[ Top ]public void unload ( [bool $erase_all] ) [line 461]Clears the object values, leaves the columns structure
Parameter(s):
- (bool) $erase_all :: clears extra values from memory
[ Top ]private string|array _getCallback ( string $for ) [line 868]Returns the callback function/method
Parameter(s):
- (string) $for :: type of query "select|update|insert|delete"
- precondition:
getPrototype() - throws:
Gumbo_Exception
[ Top ]Gets the selected Map
Parameter(s):
- (string) $key :: (null is 'method')
- throws:
Gumbo_Exception
[ Top ]private bool _isColumn ( string $column ) [line 1054]Returns if the column exists
Parameter(s):
- (string) $column :: table column name
- throws:
Gumbo_Exception
[ Top ]private bool _isColumnCount ( string $column ) [line 1150]Returns if the supplied column was marked as a Count Column
Parameter(s):
- (string) $column
- throws:
Gumbo_Exception
[ Top ]private bool _isColumnExtra ( string $column ) [line 1118]Returns if the extra column key exists
Parameter(s):
- (string) $column :: extra column key
- throws:
Gumbo_Exception
[ Top ]private bool _isColumnMod ( string $column ) [line 1086]Returns if the supplied column was modified
Parameter(s):
- (string) $column :: modified key
- throws:
Gumbo_Exception
[ Top ]private void _reload ( ) [line 415]Loads new Record or refreshes the saved/created Item values
- uses:
Gumbo_Query
[ Top ]private void _setLoaded ( bool $val ) [line 660]Sets if the record was loaded
Parameter(s):
- (bool) $val
- throws:
Gumbo_Exception
[ Top ]private void _setModified ( bool $val ) [line 679]Sets if the record was modified
Parameter(s):
- (bool) $val
- throws:
Gumbo_Exception
[ Top ]public mixed __get ( string $column ) [line 252]Returns the raw value of the column name
Parameter(s):
- (string) $column
[ Top ]public bool __isset ( string $key ) [line 274]Returns if the column is set
Parameter(s):
- (string) $key
[ Top ]public void __set ( string $key, mixed $val ) [line 218]Sets the value of a table column using 'key' map
Parameter(s):
- (string) $key
- (mixed) $val
[ Top ]public void __unset ( string $key ) [line 238]Unsets the value of the supplied table column
Parameter(s):
- (string) $key
[ Top ]