listing12.08.php
Codice PHP:
<?php
require_once("woo/domain/Venue.php");
try {
$venues = \woo\domain\Venue::findAll();
print "<br><br>";
print_r($venues);
print "<br><br>";
} catch ( Exception $e ) {
include( 'error.php' );
exit(0);
}
// default page follows
?>
<html>
<head>
<title>Venues</title>
</head>
<body>
<h1>Venues</h1>
<?php foreach( $venues as $venue ) { ?>
<?php print $venue->getName(); ?><br />
<?php } ?>
</body>
</html>
Venue.php
Codice PHP:
<?php
namespace woo\domain;
require_once( "woo/domain/DomainObject.php" );
class Venue extends DomainObject {
private $name;
private $spaces;
function __construct( $id=null, $name=null ) {
$this->name = $name;
parent::__construct( $id );
}
function setSpaces( SpaceCollection $spaces ) {
$this->spaces = $spaces;
}
function getSpaces() {
if ( ! isset( $this->spaces ) ) {
$finder = self::getFinder( 'woo\\domain\\Space' );
$this->spaces = $finder->findByVenue( $this->getId() );
//$this->spaces = self::getCollection("woo\\domain\\Space");
}
return $this->spaces;
}
function addSpace( Space $space ) {
$this->getSpaces()->add( $space );
$space->setVenue( $this );
}
function setName( $name_s ) {
$this->name = $name_s;
$this->markDirty();
}
function getName( ) {
return $this->name;
}
static function findAll() {
$finder = self::getFinder( __CLASS__ );
return $finder->findAll();
}
static function find( $id ) {
$finder = self::getFinder( __CLASS__ );
return $finder->find( $id );
}
}
?>
DomainObject.php
Codice PHP:
<?php
namespace woo\domain;
require_once( "woo/domain/Collections.php" );
require_once( "woo/domain/ObjectWatcher.php" );
require_once( "woo/domain/HelperFactory.php" );
abstract class DomainObject {
private $id = -1;
function __construct( $id=null ) {
if ( is_null( $id ) ) {
$this->markNew();
} else {
$this->id = $id;
}
}
function markNew() {
ObjectWatcher::addNew( $this );
}
function markDeleted() {
ObjectWatcher::addDelete( $this );
}
function markDirty() {
ObjectWatcher::addDirty( $this );
}
function markClean() {
ObjectWatcher::addClean( $this );
}
function getId( ) {
return $this->id;
}
static function getCollection( $type ) {
return HelperFactory::getCollection( $type );
}
function collection() {
return self::getCollection( get_class( $this ) );
}
function finder() {
return self::getFinder( get_class( $this ) );
}
static function getFinder( $type ) {
return HelperFactory::getFinder( $type );
}
function setId( $id ) {
$this->id = $id;
}
function __clone() {
$this->id = -1;
}
}
?>
HelpFactory.php
Codice PHP:
<?php
namespace woo\domain;
if ( ! isset( $EG_DISABLE_INCLUDES ) ) {
require_once( "woo/mapper/VenueMapper.php" );
require_once( "woo/mapper/SpaceMapper.php" );
require_once( "woo/mapper/EventMapper.php" );
require_once( "woo/mapper/Collections.php" );
}
class HelperFactory {
static function getFinder( $type ) {
print $type."<br>";
$type = preg_replace( '|^.*\\\|', "", $type );
print $type."<br>";
$mapper = "\\woo\\mapper\\{$type}Mapper";
if ( class_exists( $mapper ) ) {
return new $mapper();
}
throw new \woo\base\AppException( "Unknown: $mapper" );
}
static function getCollection( $type ) {
$type = preg_replace( '|^.*\\\|', "", $type );
$collection = "\\woo\\mapper\\{$type}Collection";
if ( class_exists( $collection ) ) {
return new $collection();
}
throw new \woo\base\AppException( "Unknown: $collection" );
}
}
?>
VenueMapper.php
Codice PHP:
<?php
namespace woo\mapper;
require_once( "woo/mapper/Mapper.php" );
require_once( "woo/base/Exceptions.php" );
require_once( "woo/mapper/Collections.php" );
require_once( "woo/domain.php" );
class VenueMapper extends Mapper
implements \woo\domain\VenueFinder {
function __construct() {
parent::__construct();
$this->selectAllStmt = self::$PDO->prepare(
"SELECT * FROM venue");
$this->selectStmt = self::$PDO->prepare(
"SELECT * FROM venue WHERE id=?");
$this->updateStmt = self::$PDO->prepare(
"UPDATE venue SET name=?, id=? WHERE id=?");
$this->insertStmt = self::$PDO->prepare(
"INSERT into venue ( name )
values( ? )");
}
function getCollection( array $raw ) {
return new SpaceCollection( $raw, $this );
}
protected function doCreateObject( array $array ) {
$obj = new \woo\domain\Venue( $array['id'] );
$obj->setname( $array['name'] );
//$space_mapper = new SpaceMapper();
//$space_collection = $space_mapper->findByVenue( $array['id'] );
//$obj->setSpaces( $space_collection );
return $obj;
}
protected function targetClass() {
return "woo\\domain\\Venue";
}
protected function doInsert( \woo\domain\DomainObject $object ) {
$values = array( $object->getname() );
$this->insertStmt->execute( $values );
$id = self::$PDO->lastInsertId();
$object->setId( $id );
}
function update( \woo\domain\DomainObject $object ) {
$values = array( $object->getname(), $object->getid(), $object->getId() );
$this->updateStmt->execute( $values );
}
function selectStmt() {
return $this->selectStmt;
}
function selectAllStmt() {
return $this->selectAllStmt;
}
}
?>
Mapper.php
Codice PHP:
<?php
namespace woo\mapper;
require_once("woo/base/Registry.php");
require_once("woo/base/Exceptions.php");
require_once("woo/domain/Finders.php");
abstract class Mapper implements \woo\domain\Finder {
protected static $PDO;
function __construct() {
if ( ! isset(self::$PDO) ) {
$dsn = \woo\base\ApplicationRegistry::getDSN( );
print_r( $dsn );
print "<br>".$dsn;
if ( is_null( $dsn ) ) {
throw new \woo\base\AppException( "No DSN" );
}
self::$PDO = new \PDO( $dsn );
self::$PDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
}
private function getFromMap( $id ) {
return \woo\domain\ObjectWatcher::exists
( $this->targetClass(), $id );
}
private function addToMap( \woo\domain\DomainObject $obj ) {
return \woo\domain\ObjectWatcher::add( $obj );
}
function find( $id ) {
$old = $this->getFromMap( $id );
if ( $old ) { return $old; }
$this->selectstmt()->execute( array( $id ) );
$array = $this->selectstmt()->fetch( );
$this->selectstmt()->closeCursor( );
if ( ! is_array( $array ) ) { return null; }
if ( ! isset( $array['id'] ) ) { return null; }
$object = $this->createObject( $array );
$object->markClean();
return $object;
}
function findAll( ) {
$this->selectAllStmt()->execute( array() );
return $this->getCollection( $this->selectAllStmt()->fetchAll( \PDO::FETCH_ASSOC ) );
}
function createObject( $array ) {
$old = $this->getFromMap( $array['id']);
if ( $old ) { return $old; }
$obj = $this->doCreateObject( $array );
$this->addToMap( $obj );
//$obj->markClean();
return $obj;
}
function insert( \woo\domain\DomainObject $obj ) {
$this->doInsert( $obj );
$this->addToMap( $obj );
$obj->markClean();
}
// abstract function update( \woo\domain\DomainObject $object );
protected abstract function getCollection( array $raw );
protected abstract function doCreateObject( array $array );
protected abstract function doInsert( \woo\domain\DomainObject $object );
protected abstract function targetClass();
protected abstract function selectStmt( );
protected abstract function selectAllStmt( );
}
?>