Location: PHPKode > scripts > SQLReactor > SQLReactor-0.6/example.php
<!--
    /*******************************************************************************
    * Copyright 2008 Rafael Marques Martins
    *
    * This file is part of SQLReactor.
    * 
    * SQLReactor is free software; you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation; either version 2 of the License, or
    * (at your option) any later version.
    * 
    * SQLReactor is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    * GNU General Public License for more details.
    * 
    * You should have received a copy of the GNU General Public License
    * along with SQLReactor; if not, write to the Free Software
    * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    * 
    *******************************************************************************/
-->
<h3>SQLReactor Example Page</h3>
<pre><?php

require( 'SQLReactor.php' );

/*
Connects to database. Uri pattern: dbms://user:hide@address.com/dabaseName
Examples:
    $conn = new SQLReactorConnection( "postgres://postgres:hide@address.com/sqlreactor" );
    $conn = new SQLReactorConnection( "mysql://root:hide@address.com/sqlreactor" );
    
    //No password example
    $conn = new SQLReactorConnection( "mysql://hide@address.com/sqlreactor" );
*/

$conn = new SQLReactorConnection( "postgres://postgres:hide@address.com/sqlreactor" );
//$conn = new SQLReactorConnection( "mysql://root:hide@address.com/sqlreactor" );
//$conn = new SQLReactorConnection( "sqlite://D:/Dev/SQLReactor/Source/trunk/teste.db" );
//$conn = new SQLReactorConnection( "oracle://sqlreactor:hide@address.com" );

//Classes needs to extend SQLReactor
class Ticket extends SQLReactor{

    //All the mapping must be in __map function
	public function __map(){
		global $conn;
        
        //you MUST set the connection for this class 
		$this->connection( $conn );
        
		$this->name 	        = SQLReactor::StringCol( array( 'length' => 255 ) );
        $this->statusHistory    = SQLReactor::Backref( array( 'target' => array( 'StatusHistory', 'ticket' ) ) );
	}
    
    //This magical getter will receive attribute value and manage it's return everytime it attribute is called
    function __getName( $value ){
        return strtoupper( $value );
    }
}

class StatusHistory extends SQLReactor{
	public function __map(){
		global $conn;
		
		$this->connection( $conn );
		
		$this->status 	        = $this->ForeignKey( array( 'target' => 'Status' ) );
		$this->ticket   	    = $this->ForeignKey( array( 'target' => 'Ticket' ) );
        
        $this->primaryKey( 'statusId', 'ticketId' );
	}
}

class Status extends SQLReactor{
	public function __map(){
		global $conn;
		
		$this->connection( $conn );
		
		$this->name 	        = $this->StringCol(array( 'length' => 255 ));
		$this->displayName   	= $this->StringCol(array( 'length' => 255 ));
	}
}

//Creates Tables in database. 
/** /
SQLReactor::createTable( Status );
SQLReactor::createTable( Ticket );
SQLReactor::createTable( StatusHistory );


/* Inserting examples * /
$ticket = new Ticket();
$ticket->name = 'Ticket 1';
$ticket->save();

$ticket = new Ticket();
$ticket->name = 'Ticket 2';
$ticket->save();

$ticket = new Ticket();
$ticket->name = 'Ticket 3';
$ticket->save();

$status = new Status();
$status->name = 'Status 1';
$status->save();

$status = new Status();
$status->name = 'Status 2';
$status->save();

$sh = new StatusHistory();
$sh->ticketId = 1;
$sh->statusId = 1;
$sh->save();

$sh = new StatusHistory();
$sh->ticketId = 1;
$sh->statusId = 2;
$sh->save();

$sh = new StatusHistory();
$sh->ticketId = 2;
$sh->statusId = 2;
$sh->save();

$sh = new StatusHistory();
$sh->ticketId = 3;
$sh->statusId = 1;
$sh->save();

/** /

/* Listing Examples: */
//Lists all Tickets in database
//$list = SQLReactor::getList( Ticket );

//Lists all Tickets that have status with id = 2 in statusHistory 
$list = SQLReactor::getList( Ticket,  array( 
    'filter' => array( 
        array( 'statusHistory->status->id', 2 )
    ),
    'eagerload' => array( 'statusHistory->status' ),
    'limit' => 50,
    'offset' => 0
) );

var_dump( SQLReactor::count( Ticket,  array( 
    'filter' => array( 
        array( 'statusHistory->status->id', 2 )
    ),
    'eagerload' => array( 'statusHistory->status' ),
    'limit' => 50,
    'offset' => 0
) ) );


//Example of reading items from list
foreach( $list as $ticket ){
    echo "Id: ",            $ticket->id,                "\n";
    echo "Name: ",          $ticket->name,              "\n";    
 
    echo "Status History: ", "\n";
    
    foreach( $ticket->statusHistory as $sh ){
        echo "\tStatus Id: ", $sh->status->id, "\n";
        echo "\tStatus Name: ", $sh->status->name, "\n\n";
    }
    
    echo "***********************************************************************************\n\n";
}

//Closes the database connection
$conn->close();

?></pre>
Return current item: SQLReactor