Location: PHPKode > scripts > AnonORM > anonorm/AnonORM.class.php
<?php
//$Id: AnonORM.class.php,v 1.1 2005/09/21 19:01:39 scampbell Exp $
require_once("adodb/adodb.inc.php");
require_once("../simpletest/unit_tester.php");
require_once("../simpletest/reporter.php");
/* ***** BEGIN LICENSE BLOCK *****
This software may only be used by you under license from Canterris
Inc. ('Canterris').  A copy of Canterris' Source Code Agreement 
('the License') is available by mailing Canterris:
		Canterris Inc.
		Suite 104
		214 - 11 Ave SE
		Calgary, AB T2G 0X8
		Canada
If you received this software without first entering into a license
with Canterris, you have an infringing copy of this software and cannot
use it without violating Canterris' intellectual property rights.
Software distributed under the License is distributed on an 'AS IS' basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is: AnonORM -- A Simple PHP ORM Interface
The Initial Developer of the Original Code is Sean Campbell
The following code is Copyright (C) 2005 Canterris Inc.
All Rights Reserved.
***** END LICENSE BLOCK ******/

/**
 * The Database connection string used to generate an ADODB connection.
 */
define('SQL_DB_URI',"mysql://root:@localhost/test?persist");

/**
 * Generic Anonymous Object which can be dynamically created and
 * loaded with data from a database query.  All creation and loading
 * should be done by an AnonORMPeer object.
 * @created 20-Sep-2005
 * @author Sean Campbell
 * @version $Revision: 1.1 $
 */
  class AnonORM {

        /**
         * Default constructor for the object.  Can take a comma 
         * seperated list of parameters indicating property names 
         * and values which are then loaded into the object.
         * 
         * For a better way of creating and assigning properties 
         * use __set()
         */
       public function __construct( ) {
           $n = func_num_args( ) ;
           for ( $i = 0 ; $i < $n ; $i += 2 ) {
               $this->{func_get_arg($i)} = func_get_arg($i + 1) ;
           }
       }
       
       /**
        * Generic mutator function.  Dynamically creates a property
        * if one does not already exist, otherwise changes the value
        * of the names property.
        * @since 1.0
        * @param nm The name of the property to mutate
        * @param val The value for the named property
        * @return void
        */
       public function __set($nm, $val){
           $this->$nm = $val;
       }
       
       /**
        * Generic accessor function.
        * @since 1.0
        * @param nm The name of the property to access
        * @return mixed The value of the names property
        */
       public function __get($nm){
           return $this->$nm;
       }

       /**
        * Tests to see if the names property has a value.
        * @since 1.0
        * @param nm The name of the property to test
        * @return boolean True if there is a value stored in the property, False otherwise
        */
       public function __isset($nm){
           return isset($this->$nm);
       }
       
       /**
        * Generic function to unset a property.
        * @since 1.0
        * @param nm The name of the property to unset
        * @return void
        */
       public function __unset($nm){
           unset($this->$nm);
       }
       
       /**
        * Generates an associative array of property names and their associated values.
        * @since 1.0
        * @return array List of the object properties
        */
       public function getProperties(){
          return get_object_vars($this);
       }

       /**
        * Generates string representation of the object in the same manner as a var_dump.
        * @since 1.0
        * @return string String representation of the object
        */
       public function __toString(){
          return "<pre>".var_export($this,true)."</pre>";
       }

    }

/**
 * The Peer class used to access the database and generate the dynamic anonymous objects.
 * To work properly ADODB must be configured and the SQL_DB_URI define must be set to a 
 * valid URI.
 * @created 20-Sep-2005
 * @author Sean Campbell
 * @version $Revision: 1.1 $
 */
    class AnonORMPeer {
    
          /** The ADODB database connection */
          private $DB;

          /**
           * Constructor.  Connects to the database using the provided connection string.
           * @since 1.0
           * @param uri The connection string for ADODB to connect to the DB
           */
          public function __construct($uri) {
                 $this->DB = NewADOConnection($uri);
          }

          /**
           * Creates and loads an anonymous ORM object dynamically bases on a SQL Query.
           * @since 1.0
           * @param sql the SQL query used to pull the object data from the database
           * @return array AN array containing the generated AnonORM objects
           */
          public function loadObj($sql){
                 $rs = $this->DB->Execute($sql);
                 $res = $rs->GetRows();
                 $objects = array();
                 foreach($res as $r){
                     $o = new AnonORM();
                     foreach($r as $s=>$v){
                         if(is_string($s)){
                             $o->{$s} = $v;
                         }
                     }
                     array_push($objects,(object)$o);
                 }
                 return $objects;    
          }

          /**
           * Reload an anonymous ORM object dynamically bases on a SQL Query.
           * @since 1.0
           * @param obj A reference to the object to update
           * @param sql the SQL query used to pull the object data from the database
           * @return mixed The updated object
           */
          public function &updateObj(&$obj,$sql){
                 $res = $this->DB->GetRow($sql);
                 foreach($res as $s=>$v){
                     if(is_string($s)){
                         unset($obj->{$s});
                         $obj->{$s} = $v;
                     }
                 }
                 return (object)$obj;
          }
          
          /**
           * Unwinds the properties from the provided object and stores them in the 
           * named database table.
           * @since 1.0
           * @param obj A reference to the object to save
           * @param table the database table to save the object data to
           * @param keys Primary key(s) of the table provided
           * @return void
           */
          public function saveObj(&$obj, $table, $keys){
                 $values = $obj->getProperties();
                 $ret = $this->DB->Replace($table,$values,$keys,$autoquote = true);
          }

          /**
           * Runs the Unit Test Suite
           * @since 1.0
           * @return void
           */
          public static function test(){
                 $test = &new AnonORMTest();
                 $test->run(new HtmlReporter());
          }

    }

/**
 * The AnonORM Unit Testing class.
 * @created 20-Sep-2005
 * @author Sean Campbell
 * @version $Revision: 1.1 $
 */
    class AnonORMTest extends UnitTestCase {

    private $t;
    private $temp;

    function __construct(){

    }

    function testCreatingNewAnonORMPeer() {

        $this->t = new AnonORMPeer(SQL_DB_URI);
        $this->assertIsA($this->t,"AnonORMPeer","Is Not An AnonORMPeer");
    }
    
    function testLoadingObject(){
        $this->temp = $this->t->loadObj("Select * FROM Test");
        if(count($this->temp)>1){
             $this->assertIsA($this->temp[0],"AnonORM","Is Not An AnonORM");
        } else {
            $this->assertIsA($this->temp,"AnonORM","Is Not An AnonORM");
        }
    }
    
    function testSavingObject(){
        $this->temp[1]->name = "Joe Blow";
        $this->t->saveObj($this->temp[1], "Test", "id");
        $this->assertNoErrors ("New Value 'Joe Blow' Not Saved");
    }
    
    function testUpdatingObject(){
        $this->temp[1] = $this->t->updateObj($this->temp[1],"Select * FROM Test WHERE id='".$this->temp->id."'");
        $this->assertTrue($this->temp[1]->name = "Joe Blow","New Value 'Joe Blow' Not Returned");
    }
    
    function testPrintingObject(){
        echo $this->temp[1];
        $this->assertNoErrors ("Object Not Printed.");
    }
}

?>
Return current item: AnonORM