Source for file model.php
Documentation is available at model.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
* Describes a database table: fields, rules and relationships.
* Automatically composes simple JOIN queries for relationships
* described in the models via has_many, has_one, etc.
* // define a new table named photos
* $model =& $db->model( 'photo' );
* // define the fields in the table
* $model->auto_field( 'id' );
* $model->file_field( 'image' );
* $model->char_field( 'title', 255 );
* $model->text_field( 'caption' );
* // create the table in the database
* // deny access to everybody, unless they are an administrator
* $model->let_access( 'all:never all:admin' );
* // function to test whether current user is an administrator
* {@link http://dbscript.net/model}
* @author Brian Hendrickson <brian@dbscript.net>
* name of the database table
* true if table exists in database
* list of field attributes
* list of security access rules
* name of primary key field
* name of collection key field
* list of relationships to other tables
* proper CamelCase name of data model object
* when querying with find(), offset by x records
* limit query to x records
* order query by this column
* order query ASC or DESC
* list of Collection/Feed params for layout
foreach ($fields as $table=> $fieldlist) {
// for each table in the submission do
$pkfield = $db->models[$table]->primary_key;
$rec = $db->models[$table]->base();
foreach ( $fieldlist as $field=> $type )
$result = $rec->save_changes();
$atomentry = $db->models['entries']->base();
$atomentry->set_value( 'etag', getEtag( $rec->$pkfield ) );
$atomentry->set_value( 'resource', $table );
$atomentry->set_value( 'record_id', $rec->$pkfield );
$atomentry->set_value( 'content_type', 'text/html' );
$aresult = $atomentry->save_changes();
trigger_error( "The record could not be saved into the database.", E_USER_ERROR );
$atomentry = $db->models['entries']->find_by( 'etag', $req->params['entry']['etag'] );
trigger_error( "The entry was not found in the database.", E_USER_ERROR );
$rec = $db->models[$atomentry->resource]->find( $atomentry->record_id );
trigger_error( "The record was not found in the database.", E_USER_ERROR );
foreach ($fields[$rec->table] as $field=> $type)
$result = $rec->save_changes();
foreach ($fields as $table=> $fieldlist) {
// for each table in the submission do
if (!(in_array( $table, array('entries',$rec->table), true ))) {
$rel = $rec->FirstChild( $table );
foreach ($fieldlist as $field=> $type)
$atomentry->set_value( 'last_modified', timestamp() );
$atomentry->save_changes();
trigger_error( "The record could not be updated in the database.", E_USER_ERROR );
foreach ($this->field_array as $fieldname=> $datatypename) {
if (isset ($req->params[$obj][$fieldname])) {
$fields[$this->table][$fieldname] = $datatypename;
foreach ($this->field_array as $fieldname=> $datatypename) {
if (isset ($_FILES[$obj]['name'][$fieldname])) {
$fields[$this->table][$fieldname] = $datatypename;
$req->params[$obj][$fieldname] = $_FILES[$obj]['tmp_name'][$fieldname];
foreach ($this->relations as $table=> $vals) {
if ( isset ( $db->models[$table] )) {
foreach ( $db->models[$table]->field_array as $fieldname=> $datatypename ) {
if (!($table == 'entries') && isset ($req->params[$obj][$fieldname]))
$fields[$table][$fieldname] = $datatypename;
foreach ( $db->models[$table]->field_array as $fieldname=> $datatypename ) {
if (!($table == 'entries') && isset ($_FILES[$obj]['name'][$fieldname])){
$fields[$table][$fieldname] = $datatypename;
$req->params[$obj][$fieldname] = $_FILES[$obj]['tmp_name'][$fieldname];
if (!(isset ($this->$alias) && isset ($this->data_array[$field])))
$this->$alias = & $this->data_array[$field];
function base( $custom_class = NULL ) {
if (count($func_args) == 1 && $custom_class == NULL )
$obj = new $custom_class( $this );
if (!$obj) trigger_error("error instantiating $custom_class", E_USER_ERROR );
$obj->Record( $this->table, $db );
trigger_error("error, class $custom_class not found", E_USER_ERROR );
$obj = new $custom_class( $this );
function set_field( $field, $data_type, $arr= NULL ) {
foreach ( $args as $str) {
$pair = split( ":", $str );
foreach ( $this->field_array as $field => $data_type ) {
foreach ( $args as $str) {
$pair = split( ":", $str );
foreach ( $this->field_array as $field => $data_type ) {
foreach ( $args as $str) {
$pair = split( ":", $str );
foreach ( $this->field_array as $field => $data_type ) {
$this->params[$param] = $value;
return in_array( $method, $obj->allowed_methods, true );
if (!(isset ($db->models[$table]))) {
$fields = $db->get_fields( $table );
if (isset ($fields[$table. "_primary_key"]))
$pk = $fields[$table. "_primary_key"];
$fields = & $db->models[$table]->field_array;
$pk = $db->models[$table]->primary_key;
$f = split(":",$relstring);
if ($type == 'child-many') {
foreach( $fields as $key=> $val ) {
foreach( $fields as $key=> $val ) {
foreach( $fields as $key=> $val ) {
if (!(isset ($this->access_list['read'][$resource]))) return false;
foreach ( $this->access_list['read'][$resource] as $callback ) {
if (!(isset ($this->access_list['write'][$resource]))) return false;
foreach ( $this->access_list['write'][$resource] as $callback ) {
if (!(isset ($this->access_list['read'][$resource]))) return false;
foreach ( $this->access_list['read'][$resource] as $callback ) {
if (!(isset ($req->activeroute)))
'requirements' => array ( '[a-z]+' ),
'requirements' => array ( '[a-z]+', '[0-9]+' ),
if (!(isset ($db->tables)))
$db->tables = $db->get_tables();
$this->auto_field( 'id' );
#$fields = $db->get_fields( $this->table );
#foreach ( $this->field_array as $field => $data_type ) {
# if ( !( array_key_exists( $field, $fields ) ) ) {
# $db->add_field( $this->table, $field, $data_type );
#if ( !( isset( $this->primary_key ))) {
# if (isset($fields[$this->table."_primary_key"]))
# $this->set_primary_key( $fields[$this->table."_primary_key"] );
#foreach ( $fields as $field => $type ) {
# if ( !( array_key_exists( $field, $this->field_array ) ) ) {
# if ( !( $this->table."_primary_key" == $field ) ) {
# $this->set_field( $field, $type );
return ( $db->get_mapped_datatype( $this->field_array[$field] ) === 'blob' );
function find( $id= NULL, $find_by= NULL ) {
$db->recordsets[$this->table] = $db->get_recordset($this->get_query($id, $find_by));
$rs = & $db->recordsets[$this->table];
if ( $id != NULL && $rs->rowcount > 0 )
return $rs->Load( $this->table, 0 );
return $rs->Load( $this->table, $rs->rowmap[$this->table][$id] );
return $this->find( $val, $col );
if (!(isset ($db->recordsets[$this->table])))
$rs = & $db->recordsets[$this->table];
return $rs->MoveFirst( $this->table );
if (!(isset ($db->recordsets[$this->table])))
$rs = & $db->recordsets[$this->table];
return $rs->MoveNext( $this->table );
if (isset ($_SESSION[$this->table. "_submission"]))
if ( $limit > 0 ) $this->limit = $limit;
if ( $offset > 0 ) $this->offset = $offset;
$rs = & $db->recordsets[$this->table];
return $rs->num_rows( $this->table );
|