Source for file record.php
Documentation is available at record.php
* dbscript for PHP 4 & 5 - restful crud framework
* @version 0.1.2 -- 19-Feb-2007
* @author Brian Hendrickson <brian@dbscript.net>
* @link http://dbscript.net/
* @copyright Copyright 2007 Brian Hendrickson
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* an item in a database table.
* $User = $db->get_record( 'people' );
* $User = $db->get_record( 'people', $req->userid );
* $User->set_value( 'first_name', $req->first_name );
* $User = $db->get_record( 'people', $req->userid );
* $db->delete_record( $User );
* $User = $db->get_record( 'people', $req->userid );
* print "Hello, $User->first_name!";
* $people = $db->get_recordset( "SELECT * FROM people WHERE disk_usage > 1000" );
* {@link http://dbscript.net/record}
* @author Brian Hendrickson <brian@dbscript.net>
* attribute names and data from the database field
* list of changed fields since last save_changes
* arguments from Record object instantiation
* fields to SELECT if not *
* list of related records returned by query
* sql query used to fetch this record
* true if this is an unsaved skeleton record
* true if this record is an unsaved skeleton record
* true if the record exists in the database
* initialize a new record data object
* @author Brian Hendrickson <brian@dbscript.net>
function Record( $table, &$db ) {
// check to see if field names were passed via the awesome "dot notation"
// otherwise we select all fields, using all system memory until crash.
if (count($select) > 1) {
for ($p= 1;$p< count($select);$p++ ) {
$this->table = $select[0];
// the record does not exist in the database
// check for a primary key as the fourth argument
if (isset ($func_args[3])) {
} elseif (isset ($db->models[$table]->primary_key)) {
// if a record ID was passed, fetch the record
if (isset ($func_args[2])) {
$db->fetch_record($this,$func_args[2]);
if ( isset ( $db->models[$table]->primary_key ) ) {
// otherwise a new blank record is created
if ( isset ( $db->models[$table] ) ) {
foreach ($db->models[$table]->field_array as $field=> $data_type) {
$db->set_attributes( $this, $f );
* get a list of Record attributes
* @author Brian Hendrickson <brian@dbscript.net>
* change a Record attribute value, and
* register the change in the database
* @author Brian Hendrickson <brian@dbscript.net>
* @param string field_name
if ($db->models[$this->table]->is_blob($field) && is_array($value))
$value = $value['tmp_name'];
if ( !( isset ( $this->$field ) ) )
trigger_error("the new value for $field is invalid", E_USER_ERROR );
* Save attributes changed via ->set_value( field, new_value )
* @author Brian Hendrickson <brian@dbscript.net>
$result = $db->save_record($this);
* Save all attributes into the database
* @author Brian Hendrickson <brian@dbscript.net>
$result = $db->save_record($this);
* get the first Record returned from a related table
* @author Brian Hendrickson <brian@dbscript.net>
if (!(isset ($db->models[$table]))) return false;
$rs = & $db->recordsets[$this->table];
return $db->get_record( $table );
* Iterate to the next Record returned from a related table
* @author Brian Hendrickson <brian@dbscript.net>
$rs = & $db->recordsets[$this->table];
* Count the number of related records from a specific table
* @author Brian Hendrickson <brian@dbscript.net>
* Has and Belongs To Many
* set a 'child-many' relationship
* @author Brian Hendrickson <brian@dbscript.net>
* @param string relstring
* set a 'child-one' relationship
* @author Brian Hendrickson <brian@dbscript.net>
* @param string relstring
* set a 'parent-many' relationship
* @author Brian Hendrickson <brian@dbscript.net>
* @param string relstring
* set a 'parent-one' relationship
* @author Brian Hendrickson <brian@dbscript.net>
* @param string relstring
* Set the skeleton flag on a record
* @author Brian Hendrickson <brian@dbscript.net>
* Look for a callback function for this field
* @author Brian Hendrickson <brian@dbscript.net>
* @param string field_name
$function_name = "validate_" . $this->table . "_" . $field;
return $function_name($value);
|