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

Source for file DatabaseEntity.class.php

Documentation is available at DatabaseEntity.class.php

  1. <?php
  2. /**
  3.  * @package data_structures
  4.  */
  5.  
  6. /**
  7.  *
  8.  */
  9. require_once CLASSPATH."data_structures/ABSTObject.class.php";
  10.  
  11. /**
  12.  * Class to generate objects from database tables. It makes most sense to use
  13.  * this class as an abstract type.
  14.  * For example:
  15.  *
  16.  * <code>
  17.  * class Article extends DatabaseEntity
  18.  * {
  19.  *      function Article( $int_id = null )
  20.  *      {
  21.  *            parent::DatabaseEntity( $GLOBALS["dbt_article"] );
  22.  *
  23.  *            if ( $int_id != null && is_numeric( $int_id ) )
  24.  *            {   parent::initiateObjectFromTuple("id_article = '{$int_id}'"); }
  25.  *      }
  26.  * } // end of class Article
  27.  * </code>
  28.  *
  29.  * @package   data_structures
  30.  * @version   0.1.42
  31.  * @since     0.9.1.5
  32.  *
  33.  * @author    Daniel Plücken <daniel@debakel.net>
  34.  * @license   http://www.gnu.org/copyleft/lesser.html
  35.  *             GNU Lesser General Public License
  36.  * @copyright Copyright (C) 2006 Daniel Plücken <daniel@debakel.net>
  37.  *
  38.  *  This library is free software; you can redistribute it and/or
  39.  *  modify it under the terms of the GNU Lesser General Public
  40.  *  License as published by the Free Software Foundation; either
  41.  *  version 2.1 of the License.
  42.  *
  43.  *  This library is distributed in the hope that it will be useful,
  44.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  45.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46.  *  GNU Lesser General Public License for more details.
  47.  *
  48.  *  You should have received a copy of the GNU Lesser General
  49.  *  Public License along with this library; if not, write to the
  50.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  51.  *  Boston, MA 02111-1307 USA
  52.  */
  53. class DatabaseEntity extends ABSTObject
  54. {
  55.   /**
  56.     * Stores the reference to an object of a database entity.
  57.     *
  58.     * @access public
  59.     * @var    DatabaseEntity $____parent_obj 
  60.     */
  61.     var $____parent_obj = null;
  62.     /**
  63.     * Stores the reference to an object of a database table.
  64.     *
  65.     * @access public
  66.     * @var    ABSTDatabaseTable $____dbt 
  67.     */
  68.     var $____dbt = null;
  69.     /**
  70.     * Stores the where-clause to generate the data of this entity.
  71.     *
  72.     * @access public
  73.     * @var    array  $____str_where 
  74.     */
  75.     var $____str_where = "";
  76.     /**
  77.     * Stores the key fieldnames.
  78.     *
  79.     * @access public
  80.     * @var    array  $____key_arr 
  81.     */
  82.     var $____key_arr = array();
  83.     /**
  84.     * Stores the references to the objects of the key database tables.
  85.     *
  86.     * @access public
  87.     * @var    array  $____key_table_arr 
  88.     */
  89.     var $____key_table_arr = array();
  90.     /**
  91.     * Stores the references to the objects of the key entities.
  92.     *
  93.     * @access public
  94.     * @var    array  $____key_object_arr 
  95.     */
  96.     var $____key_object_arr = array();
  97.     /**
  98.     * Stores the references to other entities.
  99.     *
  100.     * @access public
  101.     * @var    array  $____entity_arr 
  102.     */
  103.     var $____entity_arr = array();
  104.     /**
  105.     * Stores the references to other entities. But with a relation name as key
  106.     * in the first array dimension.
  107.     *
  108.     * @access public
  109.     * @var    array  $____relation_arr 
  110.     */
  111.     var $____relation_arr = array();
  112.  
  113.     /**
  114.      * Constructor
  115.      * Put in a reference to an object which is a subtype of the type
  116.      * ABSTDatabaseTable or a reference to an object which is a subtype of the
  117.      * type ABSTView.
  118.      *
  119.      * @access  public
  120.      * @version 1.0
  121.      * @since   0.1.0
  122.      *
  123.      * @param   ABSTDatabaseTable $dbt_obj 
  124.      *
  125.      * @return  DatabaseEntity 
  126.     */
  127.     function DatabaseEntity&$dbt_obj )
  128.     {    $this->____dbt =$dbt_obj}
  129.  
  130.  
  131.  
  132.     /**
  133.      * Initiates this object by using the data of a tuple from a database result.
  134.      * The database table is managed by an object of the GilliGan-Framework. See
  135.      * in package databases for further information. The database tables object
  136.      * will be stored in the Field $____dbt of this object. The original
  137.      * fieldnames from the database table will be used for the fieldnames of this
  138.      * entity object.
  139.      * Beware, only the first found tuple will used for the initiation,
  140.      * therefore you better should use the primary key to grab the data from
  141.      * the underlying database table.
  142.      *
  143.      * @access  public
  144.      * @version 1.0
  145.      * @since   0.1.0
  146.      *
  147.      * @param   string  $str_where 
  148.      *
  149.      * @return  void 
  150.      */
  151.     function initiateObjectFromTuple$str_where )
  152.     {
  153.         $res_arr $this->____dbt->getSingleDataset$str_where );
  154.  
  155.         if is_array$res_arr ) )
  156.         foreach $res_arr as $fieldname => $value )
  157.         if !is_int$fieldname && !empty$fieldname ) )
  158.         {    $this->$fieldname $value}
  159.     }
  160.  
  161.  
  162.  
  163.     /**
  164.      * Adds a keytable to this object.
  165.      *
  166.      * @version 1.1
  167.      * @since   0.1.1
  168.      * @author  Daniel Plücken <daniel@debakel.net>
  169.      * @access  public
  170.      * @param   string            $key     Fieldname that refers to the key table.
  171.      * @param   ABSTDatabaseTable $dbt_obj Reference to the key table.
  172.      * @return  void 
  173.      */
  174.     function addKeyTable$key&$dbt_obj )
  175.     {
  176.         $pkey_arr $dbt_obj->getPrimaryKey();
  177.         $this->____key_table_arr[$key=$dbt_obj;
  178.  
  179.         $tmp_obj =new DatabaseEntity$dbt_obj );
  180.         $tmp_obj->initiateObjectFromTuple$pkey_arr[0]." = '".$this->$key."'" );
  181.  
  182.         $this->____key_object_arr[$key$tmp_obj;
  183.         $this->____key_arr[$key;
  184.     }
  185.  
  186.  
  187.  
  188.     /**
  189.      * Adds a weak entity.
  190.      *
  191.      * @access  public
  192.      * @version 1.1
  193.      * @since   0.1.1
  194.      *
  195.      * @param   DatabaseEntity $dbe 
  196.      * @param   string         $named_relation For example is_in_category ...
  197.      *
  198.      * @return  void 
  199.      */
  200.     function addEntity&$dbe$named_relation "" )
  201.     {
  202.         $dbe->____parent_obj =$this;
  203.  
  204.         if empty$named_relation ) )
  205.         {   $this->____entity_arr[=$dbe;}
  206.         else
  207.         {   $this->____relation_arr[$named_relation][=$dbe}
  208.     }
  209.  
  210.  
  211.  
  212.     /**
  213.      * Describes an "n to m"-Relation to this object.
  214.      *
  215.      * @access  public
  216.      * @version 1.14
  217.      * @since   0.1.1
  218.      *
  219.      * @param   ABSTDatabaseTable $dbt Reference to the key table.
  220.      * @param   string            $str_key Fieldname that refers to the key
  221.      *                                      table.
  222.      * @param   string            $str_additional_where 
  223.      * @param   string            $named_relation For example is_in_category ...
  224.      *
  225.      * @return  void 
  226.      */
  227.     function addMultipleEntities(
  228.                                   &$dbt$str_key$str_additional_where "",
  229.                                   $named_relation ""
  230.                                 )
  231.     {
  232.         $pkey_arr $this->____dbt->getPrimaryKey();
  233.         $str_where_entity $str_key." = '".$this->$pkey_arr[0]."'";
  234.  
  235.         if !empty$str_additional_where ) )
  236.         {   $str_where_entity .= " AND ".$str_additional_where}
  237.  
  238.         $res_arr $dbt->getDatasets(
  239.                                         $str_where_entity"",
  240.                                         MYSQL_OUTPUT_ASSOC
  241.                                     );
  242.  
  243.         if is_array$res_arr ) )
  244.         for $i 0$i count$res_arr )$i++ )
  245.         {
  246.             $tmp_obj =new DatabaseEntity$dbt );
  247.  
  248.             foreach $res_arr[$ias $fieldname => $value )
  249.             {   $tmp_obj->$fieldname $value}
  250.  
  251.             $this->addEntity$tmp_obj$named_relation );
  252.         }
  253.     }
  254.  
  255.  
  256.  
  257.     /**
  258.     * Returns an array of this object. The objects are generated from a hurd of
  259.     * tuples collected from a result of a database query.
  260.     *
  261.     * @static
  262.     *
  263.     * @access  public
  264.     * @version 1.22
  265.     * @since   0.1.1
  266.     *
  267.     * @author  Daniel Plücken <daniel@debakel.net>
  268.     * @author  Daniel Dornhardt <daniel@dornhardt.com>
  269.     *
  270.     * @param   ABSTDatabaseTable $dbt_obj   Reference to the table from which
  271.     *                                        the Tuple should read.
  272.     * @param   string            $str_where Where-clause to fetch the tuple.
  273.     * @param   string            $classname if used for subclasses of
  274.     *                                         DatabaseEntity, class $classname is
  275.     *                                         used for the construction objects so
  276.     *                                         members / methods of subclass can be
  277.     *                                         used
  278.     * @param   boolean $primary_key_as_array_keys If this is set to true, the
  279.     *                                               keys of the output array will
  280.     *                                               be the same like the primary
  281.     *                                               key value for the matching
  282.     *                                               tuple. THIS DOESN'T MAKE SENSE
  283.     *                                               FOR ABSTView-OBJECTS!
  284.     * @return  array 
  285.     */
  286.     function createEntitiesFromMultiTuple(
  287.                          &$dbt_obj,
  288.                          $str_where,
  289.                          $classname = __CLASS__,
  290.                          $primary_key_as_array_keys false
  291.                                        )
  292.     {
  293.         $res_arr $dbt_obj->getDatasets$str_where""MYSQL_OUTPUT_ASSOC );
  294.  
  295.         if (
  296.              is_a$dbt_obj"ABSTView" )
  297.           || is_subclass_of$dbt_obj"ABSTView" )
  298.            )
  299.         {
  300.             $tmp_arr array();
  301.             if is_array$res_arr ) )
  302.             for $i 0$i count$res_arr )$i++ )
  303.             {
  304.                $tmp_arr[=new $classname$dbt_obj );
  305.  
  306.                foreach $res_arr[$ias $fieldname => $value )
  307.                {
  308.                     $fieldname substrstrstr$fieldname"_" ));
  309.                     if empty$tmp_arr[count($tmp_arr)-1]->$fieldname ) )
  310.                     {   $tmp_arr[count($tmp_arr)-1]->$fieldname $value}
  311.                }
  312.             }
  313.         }
  314.         else
  315.         {
  316.             $tmp_arr array();
  317.             if is_array$res_arr ) )
  318.             {
  319.                 if (
  320.                      !$primary_key_as_array_keys
  321.                   || !($pk_arr $dbt_obj->getPrimaryKey())
  322.                    )
  323.                 {
  324.                     for $i 0$i count$res_arr )$i++ )
  325.                     {
  326.                         $tmp_arr[=new $classname$dbt_obj );
  327.  
  328.                         foreach $res_arr[$ias $fieldname => $value )
  329.                         {    $tmp_arr[count($tmp_arr)-1]->$fieldname $value}
  330.                     }
  331.                 }
  332.                 else
  333.                 {
  334.                     for $i 0$i count$res_arr )$i++ )
  335.                     {
  336.                        $str_pk_impl "";
  337.                        foreach $pk_arr as $key_name )
  338.                        {    $str_pk_impl .= $res_arr[$i][$key_name]}
  339.  
  340.                        $tmp_arr[$str_pk_impl=new $classname$dbt_obj );
  341.  
  342.                        foreach $res_arr[$ias $fieldname => $value )
  343.                        {    $tmp_arr[$str_pk_impl]->$fieldname $value}
  344.                     }
  345.                 }
  346.             }
  347.         }
  348.  
  349.         return $tmp_arr;
  350.     }
  351.  
  352.  
  353.  
  354.     /**
  355.      * Trys to delete a dataset in the database table with values from the
  356.      * primary key.
  357.      *
  358.      * @access  public
  359.      * @version 1.0
  360.      * @since   0.1.42
  361.      *
  362.      * @return  boolean 
  363.      */
  364.     function delete()
  365.     {
  366.         $p_arr $this->____dbt->getPrimaryKey();
  367.  
  368.         $str_where "";
  369.         if !$p_arr )
  370.         {   return false}
  371.         else
  372.         {
  373.             $str_name array_pop$p_arr );
  374.             $str_where $str_name." = '{$this->$str_name}'";
  375.  
  376.             foreach $p_arr as $str_name )
  377.             {   $str_where .= " AND {$str_name} = '{$this->$str_name}'"}
  378.  
  379.             return $this->____dbt->delete$str_where );
  380.         }
  381.     }
  382.  
  383.  
  384.  
  385.     /**
  386.      * Replaces this object in the database table.
  387.      *
  388.      * @access  public
  389.      * @version 0.12
  390.      * @since   0.1.21
  391.      *
  392.      * @author  Daniel Dornhardt <daniel@dornhardt.com>
  393.      * @author  Daniel Plücken <daniel@debakel.net> (auto_increment-handle)
  394.      *
  395.      * @return  boolean 
  396.      */
  397.     function replace()
  398.     {
  399.         $keys $this->____dbt->getFieldnames();
  400.         $values array();
  401.  
  402.         foreach $keys as $fieldname )
  403.         {    $values[$this->$fieldname}
  404.  
  405.         $auto_increment_field $this->____dbt->getAutoIncrementField();
  406.  
  407.         // It is an update or there is no auto increment field!
  408.         if (
  409.              empty$auto_increment_field )
  410.           || !empty$this->$auto_increment_field )
  411.            )
  412.         {   return $this->____dbt->replace$keys$values )}
  413.         // It is an insert and an auto increment field exists!
  414.         else
  415.         {
  416.             $bool_ret $this->____dbt->replace$keys$values );
  417.             if $bool_ret )
  418.             {   $this->$auto_increment_field $GLOBALS["insert_id"]}
  419.         }
  420.  
  421.         return $bool_ret;
  422.     }
  423.  
  424.  
  425.  
  426.     /**
  427.      * Puts the values of the passed kind of request variables into the fields
  428.      * of this object. This is useful to update a dataset with entered form data
  429.      * before writing it back to the database table were it come from. Use the
  430.      * method "replace" to to so.
  431.      *
  432.      * @access  public
  433.      * @version 1.0
  434.      * @since   0.1.4
  435.      *
  436.      * @param array $request_type_arr 
  437.      *
  438.      * @return boolean 
  439.      */
  440.                       $request_type_arr array"GET""POST""REQUEST" )
  441.                                              )
  442.     {
  443.         if is_string$request_type_arr ) )
  444.         {  $request_type_arr array$request_type_arr )}
  445.  
  446.         $field_arr $this->____dbt->getRealFieldnames();
  447.  
  448.         if !is_array$field_arr ) )
  449.         {  return false}
  450.  
  451.         if get_magic_quotes_gpc() )
  452.         {
  453.             if in_array"GET"$request_type_arr ) )
  454.             foreach $field_arr as $field_name )
  455.             if isset$_GET$field_name ) )
  456.             {  $this->$field_name stripslashes$_GET$field_name )}
  457.  
  458.             if in_array"POST"$request_type_arr ) )
  459.             foreach $field_arr as $field_name )
  460.             if isset$_POST$field_name ) )
  461.             {  $this->$field_name stripslashes$_POST$field_name )}
  462.  
  463.             if in_array"REQUEST"$request_type_arr ) )
  464.             foreach $field_arr as $field_name )
  465.             if isset$_REQUEST$field_name ) )
  466.             {  $this->$field_name stripslashes$_REQUEST$field_name )}
  467.         }
  468.         else
  469.         {
  470.             if in_array"GET"$request_type_arr ) )
  471.             foreach $field_arr as $field_name )
  472.             if isset$_GET$field_name ) )
  473.             {  $this->$field_name $_GET$field_name ]}
  474.  
  475.             if in_array"POST"$request_type_arr ) )
  476.             foreach $field_arr as $field_name )
  477.             if isset$_POST$field_name ) )
  478.             {  $this->$field_name $_POST$field_name ]}
  479.  
  480.             if in_array"REQUEST"$request_type_arr ) )
  481.             foreach $field_arr as $field_name )
  482.             if isset$_REQUEST$field_name ) )
  483.             {  $this->$field_name $_REQUEST$field_name ]}
  484.         }
  485.  
  486.         return true;
  487.     }
  488.  
  489.  
  490.  
  491.     /**
  492.      * Puts the fields of this Object, that correspond with the fields of
  493.      * database table, into the $_POST-Array.
  494.      *
  495.      * @access  public
  496.      * @version 1.0
  497.      *
  498.      * @return  void 
  499.      */
  500.     function putDatabaseFieldsIntoPostVars()
  501.     {
  502.         if method_exists $this->____dbt"getFieldnames" ) )
  503.         {
  504.             $tmp_arr $this->____dbt->getFieldnames();
  505.             foreach $tmp_arr as $value )
  506.             {
  507.                 $_POST[$value$this->$value;
  508.                 $_REQUEST[$value$this->$value;
  509.             }
  510.         }
  511.     }
  512.  
  513.  
  514.  
  515.     /**
  516.      * Puts the fields of this Object, that correspond with the fields of
  517.      * database table, into the $_GET-Array.
  518.      *
  519.      * @access  public
  520.      * @version 1.0
  521.      *
  522.      * @return  void 
  523.      */
  524.     function putDatabaseFieldsIntoGetVars()
  525.     {
  526.         if method_exists $this->____dbt"getFieldnames" ) )
  527.         {
  528.             $tmp_arr $this->____dbt->getFieldnames();
  529.             foreach $tmp_arr as $value )
  530.             {
  531.                 $_GET[$value$this->$value;
  532.                 $_REQUEST[$value$this->$value;
  533.             }
  534.         }
  535.     }
  536.  
  537.  
  538.  
  539.     /**
  540.      * Prints out the Structure of this Object without the management data.
  541.      *
  542.      * @require PHP 4
  543.      * @version 1.0
  544.      * @since   0.1.37
  545.      * @access  public
  546.      *
  547.      * @return  void 
  548.      *
  549.      * @author  Daniel Plücken <daniel.pluecken@wfp2.com>
  550.      */
  551.     function printStructure()
  552.     {    parent::printStructure"!^\_\_\_\_!" )}
  553. // End of class DatabaseEntity
  554.  
  555. ?>

Documentation generated on Thu, 05 Jun 2008 23:42:41 +0200 by phpDocumentor 1.4.1