dbscript
[ class tree: dbscript ] [ index: dbscript ] [ all elements ]

Source for file record.php

Documentation is available at record.php

  1. <?php
  2.  
  3.   /** 
  4.    * dbscript for PHP 4 & 5 - restful crud framework
  5.    * @version 0.1.2 -- 19-Feb-2007
  6.    * @author Brian Hendrickson <brian@dbscript.net>
  7.    * @link http://dbscript.net/
  8.    * @copyright Copyright 2007 Brian Hendrickson
  9.    * @package dbscript
  10.    * @license http://www.opensource.org/licenses/mit-license.php MIT License
  11.    */
  12.  
  13.   /**
  14.    * Record
  15.    * 
  16.    * an item in a database table.
  17.    * 
  18.    * Usage:
  19.    * <code>
  20.    * // INSERT
  21.    * $User = $db->get_record( 'people' );
  22.    * $User->save();
  23.    * // UPDATE
  24.    * $User = $db->get_record( 'people', $req->userid );
  25.    * $User->set_value( 'first_name', $req->first_name );
  26.    * // DELETE
  27.    * $User = $db->get_record( 'people', $req->userid );
  28.    * $db->delete_record( $User );
  29.    * // SELECT ONE
  30.    * $User = $db->get_record( 'people', $req->userid );
  31.    * print "Hello, $User->first_name!";
  32.    * // SELECT MULTIPLE
  33.    * $people = $db->get_recordset( "SELECT * FROM people WHERE disk_usage > 1000" );
  34.    * </code>
  35.    * 
  36.    * More info...
  37.    * {@link http://dbscript.net/record}
  38.    * 
  39.    * @package dbscript
  40.    * @author Brian Hendrickson <brian@dbscript.net>
  41.    * @access public
  42.    * @version 0.1.2
  43.    */
  44.  
  45. class Record {
  46.  
  47.   /**
  48.    * attribute names and data from the database field
  49.    * @var string[] 
  50.    */
  51.   var $attributes;
  52.  
  53.   /**
  54.    * list of changed fields since last save_changes
  55.    * @var string[] 
  56.    */
  57.   
  58.   /**
  59.    * primary key field
  60.    * @var string 
  61.    */
  62.   var $primary_key;
  63.  
  64.   /**
  65.    * arguments from Record object instantiation
  66.    * @var string[] 
  67.    */
  68.   var $func_args;
  69.  
  70.   /**
  71.    * fields to SELECT if not *
  72.    * @var string[] 
  73.    */
  74.   var $select;
  75.  
  76.   /**
  77.    * list of related records returned by query
  78.    * @var string[] 
  79.    */
  80.   var $children;
  81.  
  82.   /**
  83.    * sql query used to fetch this record
  84.    * @var string 
  85.    */
  86.   var $selecttext;
  87.   
  88.   /**
  89.    * true if this is an unsaved skeleton record
  90.    * @var boolean 
  91.    */
  92.   var $skeleton;
  93.  
  94.   /**
  95.    * true if this record is an unsaved skeleton record
  96.    * @var string[] 
  97.    */
  98.   var $relationships;
  99.  
  100.   /**
  101.    * name of database table
  102.    * @var string 
  103.    */
  104.   var $table;
  105.  
  106.   /**
  107.    * true if the record exists in the database
  108.    * @var string 
  109.    */
  110.   var $exists;
  111.   
  112.   /**
  113.    * Record
  114.    * 
  115.    * initialize a new record data object
  116.    * 
  117.    * @author Brian Hendrickson <brian@dbscript.net>
  118.    * @access public
  119.    * @param string table
  120.    */
  121.   function Record$table&$db {
  122.     trigger_before'Record'$this$db );
  123.     $func_args func_get_args();
  124.     // check to see if field names were passed via the awesome "dot notation"
  125.     // otherwise we select all fields, using all system memory until crash.
  126.     $select explode(".",$table);
  127.     if (count($select1{
  128.       for ($p=1;$p<count($select);$p++{
  129.         if ($p == 1{
  130.           $this->selecttext = $select[$p];
  131.         else {
  132.           $this->selecttext .= "," $select[$p];
  133.         }
  134.       }
  135.       $this->table = $select[0];
  136.       $table $select[0];
  137.     else {
  138.       $this->table = $table;
  139.       $this->selecttext = "*";
  140.     }
  141.     
  142.     // the record does not exist in the database
  143.     $this->exists = false;
  144.     // check for a primary key as the fourth argument
  145.     if (isset($func_args[3])) {
  146.       $this->primary_key = $func_args[3];
  147.     elseif (isset($db->models[$table]->primary_key)) {
  148.       $this->primary_key = $db->models[$table]->primary_key;
  149.     else {
  150.       $this->primary_key = "id";
  151.     }
  152.     // if a record ID was passed, fetch the record
  153.     if (isset($func_args[2])) {
  154.       $db->fetch_record($this,$func_args[2]);
  155.       if isset$db->models[$table]->primary_key ) ) {
  156.         $this->primary_key = $db->models[$table]->primary_key;
  157.       }
  158.     else {
  159.       // otherwise a new blank record is created
  160.       $this->attributes = array();
  161.       $this->modified_fields = array();
  162.       $this->relationships = array();
  163.       $this->children = array();
  164.       $this->skeleton = false;
  165.       $this->set_value($this->primary_key,"");
  166.       if isset$db->models[$table) ) {
  167.         $f array();
  168.         foreach ($db->models[$table]->field_array as $field=>$data_type{
  169.           $f[$field"";
  170.         }
  171.         $db->set_attributes$this$f );
  172.       }
  173.     }
  174.     trigger_after'Record'$this$db );
  175.   }
  176.  
  177.   /**
  178.    * Field Names
  179.    * 
  180.    * get a list of Record attributes
  181.    * 
  182.    * @author Brian Hendrickson <brian@dbscript.net>
  183.    * @access public
  184.    * @return string[] 
  185.    */
  186.   function field_names({
  187.     $arr array();
  188.     foreach array_keys$this->attributes as $field {
  189.       if ($field != $this->primary_key{
  190.         $arr[$field;
  191.       }
  192.     }
  193.     return $arr;
  194.   }
  195.  
  196.   /**
  197.    * Set Value
  198.    * 
  199.    * change a Record attribute value, and
  200.    * register the change in the database
  201.    * 
  202.    * @author Brian Hendrickson <brian@dbscript.net>
  203.    * @access public
  204.    * @param string field_name
  205.    * @param string value
  206.    */
  207.   function set_value($field,$value{
  208.     $db =db_object();
  209.     trigger_before'set_value'$this$db );
  210.     if (!(isset($this->attributes[$this->primary_key]))) {
  211.       $pkfield $this->primary_key;
  212.       $this->attributes[$pkfield"";
  213.       $this->$pkfield =$this->attributes[$pkfield];
  214.     }
  215.     if ($this->validate_field($field,$value)) {
  216.       if ($db->models[$this->table]->is_blob($field&& is_array($value))
  217.         $value $value['tmp_name'];
  218.       $this->attributes[$field$value;
  219.       if !isset$this->$field ) ) )
  220.         $this->$field =$this->attributes[$field];
  221.       $this->modified_fields[$field;
  222.     else {
  223.       trigger_error("the new value for $field is invalid"E_USER_ERROR );
  224.     }
  225.     trigger_after'set_value'$this$db );
  226.   }
  227.  
  228.   /**
  229.    * Save Changes
  230.    * 
  231.    * Save attributes changed via ->set_value( field, new_value )
  232.    * 
  233.    * @author Brian Hendrickson <brian@dbscript.net>
  234.    * @access public
  235.    */
  236.   function save_changes({
  237.     $db =db_object();
  238.     $result $db->save_record($this);
  239.     if ($result)
  240.       $this->exists = true;
  241.     return $result;
  242.   }
  243.   
  244.   /**
  245.    * Save
  246.    * 
  247.    * Save all attributes into the database
  248.    * 
  249.    * @author Brian Hendrickson <brian@dbscript.net>
  250.    * @access public
  251.    */
  252.   function save({
  253.     $db =db_object();
  254.     foreach array_keys$this->attributes as $field {
  255.       $this->modified_fields[$field;
  256.     }
  257.     $result $db->save_record($this);
  258.     if ($result)
  259.       $this->exists = true;
  260.     return $result;
  261.   }
  262.   
  263.   /**
  264.    * First Child
  265.    * 
  266.    * get the first Record returned from a related table
  267.    * 
  268.    * @author Brian Hendrickson <brian@dbscript.net>
  269.    * @access public
  270.    * @param string table
  271.    * @return Record 
  272.    */  
  273.   function FirstChild$table=NULL {
  274.     $db =db_object();
  275.     if (!(isset($db->models[$table]))) return false;
  276.     $rs =$db->recordsets[$this->table];
  277.     if (!$rsreturn false;
  278.     if (!($this->ChildCount$table 0)) {
  279.       return $db->get_record$table );
  280.     }
  281.     if isset$rs->relations[$this->attributes[$this->primary_key]][$table) )
  282.       return $rs->FirstChild$this->attributes[$this->primary_key]$table );
  283.     return false;
  284.   }
  285.  
  286.   /**
  287.    * Next Child
  288.    * 
  289.    * Iterate to the next Record returned from a related table
  290.    * 
  291.    * @author Brian Hendrickson <brian@dbscript.net>
  292.    * @access public
  293.    * @param string table
  294.    * @return Record 
  295.    */
  296.   function NextChild$table=NULL {
  297.     $db =db_object();
  298.     $rs =$db->recordsets[$this->table];
  299.     if (!$rsreturn false;
  300.     if isset$rs->relations[$this->attributes[$this->primary_key]][$table) )
  301.       return $rs->NextChild$this->attributes[$this->primary_key]$table );
  302.     return false;
  303.   }
  304.  
  305.   /**
  306.    * Child Count
  307.    * 
  308.    * Count the number of related records from a specific table
  309.    * 
  310.    * @author Brian Hendrickson <brian@dbscript.net>
  311.    * @access public
  312.    * @param string table
  313.    * @return Record 
  314.    */    
  315.   function ChildCount$table {
  316.     if isset$this->children[$table) )
  317.       return count$this->children[$table);
  318.     return 0;
  319.   }
  320.  
  321.   /**
  322.    * Has and Belongs To Many
  323.    * 
  324.    * set a 'child-many' relationship
  325.    * 
  326.    * @author Brian Hendrickson <brian@dbscript.net>
  327.    * @access public
  328.    * @param string relstring
  329.    */
  330.   function has_and_belongs_to_many$relstring {
  331.     $this->relationships[$relstring'child-many';
  332.   }
  333.  
  334.   /**
  335.    * Belongs To
  336.    * 
  337.    * set a 'child-one' relationship
  338.    * 
  339.    * @author Brian Hendrickson <brian@dbscript.net>
  340.    * @access public
  341.    * @param string relstring
  342.    */
  343.   function belongs_to$relstring {
  344.     $this->relationships[$relstring'child-one';
  345.   }
  346.  
  347.   /**
  348.    * Has Many
  349.    * 
  350.    * set a 'parent-many' relationship
  351.    * 
  352.    * @author Brian Hendrickson <brian@dbscript.net>
  353.    * @access public
  354.    * @param string relstring
  355.    */
  356.   function has_many$relstring {
  357.     $this->relationships[$relstring'parent-many';
  358.   }
  359.  
  360.   /**
  361.    * Has One
  362.    * 
  363.    * set a 'parent-one' relationship
  364.    * 
  365.    * @author Brian Hendrickson <brian@dbscript.net>
  366.    * @access public
  367.    * @param string relstring
  368.    */
  369.   function has_one$relstring {
  370.     $this->relationships[$relstring'parent-one';
  371.   }
  372.  
  373.   /**
  374.    * Is Skeleton
  375.    * 
  376.    * Set the skeleton flag on a record
  377.    * 
  378.    * @author Brian Hendrickson <brian@dbscript.net>
  379.    * @access public
  380.    */
  381.   function is_skeleton({
  382.     $this->skeleton = true;
  383.   }
  384.  
  385.   /**
  386.    * Validate Field
  387.    * 
  388.    * Look for a callback function for this field
  389.    * 
  390.    * @author Brian Hendrickson <brian@dbscript.net>
  391.    * @access public
  392.    * @param string field_name
  393.    * @param string value
  394.    */
  395.   function validate_field($field,&$value{
  396.     $function_name "validate_" $this->table . "_" $field;
  397.     if (function_exists($function_name)) {
  398.       return $function_name($value);
  399.     else {
  400.       return true;
  401.     }
  402.   }
  403.  
  404. }
  405.  
  406. ?>

Documentation generated on Mon, 19 Feb 2007 10:25:01 -0800 by phpDocumentor 1.3.1