Commit 571cb467 authored by doctrine's avatar doctrine

--no commit message

--no commit message
parent dac05178
<?php
/**
* class Doctrine_Access
* Doctrine_Record and Doctrine_Collection classes extend this base class
* the purpose of DAOStrategy is to provice array access and property overload interface for these classes
*/
abstract class Doctrine_Access implements ArrayAccess {
/**
* setArray
* @param array $array an array of key => value pairs
*/
public function setArray(array $array) {
foreach($array as $k=>$v):
$this->set($k,$v);
endforeach;
}
/**
* __set -- an alias of set()
* @see set, offsetSet
* @param $name
* @param $value
*/
public function __set($name,$value) {
$this->set($name,$value);
}
/**
* __get -- an alias of get()
* @see get, offsetGet
* @param mixed $name
* @return mixed
*/
public function __get($name) {
return $this->get($name);
}
/**
* @return boolean -- whether or not the data has a field $offset
*/
public function offsetExists($offset) {
return (bool) isset($this->data[$offset]);
}
/**
* offsetGet -- an alias of get()
* @see get, __get
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset) {
return $this->get($offset);
}
/**
* sets $offset to $value
* @see set, __set
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value) {
if( ! isset($offset)) {
$this->add($value);
} else
$this->set($offset,$value);
}
/**
* unset a given offset
* @see set, offsetSet, __set
* @param mixed $offset
*/
public function offsetUnset($offset) {
if($this instanceof Doctrine_Collection) {
return $this->remove($offset);
} else {
$this->set($offset,null);
}
}
}
?>
<?php
require_once("Relation.class.php");
/**
* Doctrine_Association this class takes care of association mapping
* (= many-to-many relationships, where the relationship is handled through an additional relational table
* which holds 2 foreign keys)
*/
class Doctrine_Association extends Doctrine_Relation {
/**
* @var Doctrine_Table $associationTable
*/
private $associationTable;
/**
* the constructor
* @param Doctrine_Table $table foreign factory object
* @param Doctrine_Table $associationTable factory which handles the association
* @param string $local local field name
* @param string $foreign foreign field name
* @param integer $type type of relation
* @see Doctrine_Table constants
*/
public function __construct(Doctrine_Table $table, Doctrine_Table $associationTable, $local, $foreign, $type) {
parent::__construct($table, $local, $foreign, $type);
$this->associationTable = $associationTable;
}
/**
* @return Doctrine_Table
*/
public function getAssociationFactory() {
return $this->associationTable;
}
}
?>
<?php
/**
* Doctrine_BatchIterator
* iterates through Doctrine_Collection_Batch
*/
class Doctrine_BatchIterator implements Iterator {
/**
* @var Doctrine_Collection_Batch $collection
*/
private $collection;
/**
* @var array $keys
*/
private $keys;
/**
* @var mixed $key
*/
private $key;
/**
* @var integer $index
*/
private $index;
/**
* @var integer $count
*/
private $count;
/**
* constructor
* @var Doctrine_Collection_Batch $collection
*/
public function __construct(Doctrine_Collection_Batch $collection) {
$this->collection = $collection;
$this->keys = $this->collection->getKeys();
$this->count = $this->collection->count();
}
/**
* @return void
*/
public function rewind() {
$this->index = 0;
$i = $this->index;
if(isset($this->keys[$i]))
$this->key = $this->keys[$i];
}
/**
* @return boolean whether or not the iteration will continue
*/
public function valid() {
return $this->index < $this->count;
}
/**
* @return integer the current key
*/
public function key() {
return $this->key;
}
/**
* @return Doctrine_Record the current DAO
*/
public function current() {
return $this->collection->get($this->key);
}
/**
* @return void
*/
public function next() {
$this->index++;
$i = $this->index;
if(isset($this->keys[$i]))
$this->key = $this->keys[$i];
}
}
?>
<?php
interface iDoctrine_Cache {
public function store(Doctrine_Record $record);
public function clean();
public function delete($id);
public function fetch($id);
public function exists($id);
}
class Doctrine_Cache implements iDoctrine_Cache {
/**
* implemented by child classes
* @param Doctrine_Record $record
* @return boolean
*/
public function store(Doctrine_Record $record) {
return false;
}
/**
* implemented by child classes
* @return boolean
*/
public function clean() {
return false;
}
/**
* implemented by child classes
* @return boolean
*/
public function delete($id) {
return false;
}
/**
* implemented by child classes
* @throws InvalidKeyException
* @return Doctrine_Record found Data Access Object
*/
public function fetch($id) {
throw new InvalidKeyException();
}
/**
* implemented by child classes
* @param integer $id
* @return boolean
*/
public function exists($id) {
return false;
}
/**
* implemented by child classes
* @return integer
*/
public function deleteAll() {
return 0;
}
}
?>
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Cache.class.php");
/**
* Doctrine_CacheFile
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*/
class Doctrine_Cache_File extends Doctrine_Cache implements Countable {
const STATS_FILE = "stats.cache";
/**
* @var string $path path for the cache files
*/
private $path;
/**
* @var array $fetched an array of fetched primary keys
*/
private $fetched = array();
/**
* @var Doctrine_Table $objTable
*/
private $objTable;
/**
* constructor
* @param Doctrine_Table $objTable
*/
public function __construct(Doctrine_Table $objTable) {
$this->objTable = $objTable;
$name = $this->getTable()->getTableName();
$manager = Doctrine_Manager::getInstance();
$dir = $manager->getAttribute(Doctrine::ATTR_CACHE_DIR);
if( ! is_dir($dir))
mkdir($dir, 0777);
if( ! is_dir($dir.DIRECTORY_SEPARATOR.$name))
mkdir($dir.DIRECTORY_SEPARATOR.$name, 0777);
$this->path = $dir.DIRECTORY_SEPARATOR.$name.DIRECTORY_SEPARATOR;
/**
* create stats file
*/
if( ! file_exists($this->path.self::STATS_FILE))
touch($this->path.self::STATS_FILE);
}
/**
* @return Doctrine_Table
*/
public function getTable() {
return $this->objTable;
}
/**
* @return integer number of cache files
*/
public function count() {
$c = -1;
foreach(glob($this->path."*.cache") as $file) {
$c++;
}
return $c;
}
/**
* getStats
* @return array an array of fetch statistics, keys as primary keys
* and values as fetch times
*/
public function getStats() {
$f = file_get_contents($this->path.self::STATS_FILE);
// every cache file starts with a ":"
$f = substr(trim($f),1);
$e = explode(":",$f);
return array_count_values($e);
}
/**
* store store a Doctrine_Record into file cache
* @param Doctrine_Record $record data access object to be stored
* @return boolean whether or not storing was successful
*/
public function store(Doctrine_Record $record) {
if($record->getState() != Doctrine_Record::STATE_CLEAN)
return false;
$file = $this->path.$record->getID().".cache";
if(file_exists($file))
return false;
$clone = clone $record;
$id = $clone->getID();
$fp = fopen($file,"w+");
fwrite($fp,serialize($clone));
fclose($fp);
$this->fetched[] = $id;
return true;
}
/**
* clean
* @return void
*/
public function clean() {
$stats = $this->getStats();
arsort($stats);
$size = $this->objTable->getAttribute(Doctrine::ATTR_CACHE_SIZE);
$count = count($stats);
$i = 1;
$preserve = array();
foreach($stats as $id => $count) {
if($i > $size)
break;
$preserve[$id] = true;
$i++;
}
foreach(glob($this->path."*.cache") as $file) {
$e = explode(".",basename($file));
$c = count($e);
$id = $e[($c - 2)];
if( ! isset($preserve[$id]))
@unlink($this->path.$id.".cache");
}
$fp = fopen($this->path.self::STATS_FILE,"w+");
fwrite($fp,"");
fclose($fp);
}
/**
* @param integer $id primary key of the DAO
* @return string filename and path
*/
public function getFileName($id) {
return $this->path.$id.".cache";
}
/**
* @return array an array of fetched primary keys
*/
public function getFetched() {
return $this->fetched;
}
/**
* fetch fetch a Doctrine_Record from the file cache
* @param integer $id
*/
public function fetch($id) {
$name = $this->getTable()->getComponentName();
$file = $this->path.$id.".cache";
if( ! file_exists($file))
throw new InvalidKeyException();
$data = file_get_contents($file);
$record = unserialize($data);
if( ! ($record instanceof Doctrine_Record)) {
// broken file, delete silently
$this->delete($id);
throw new InvalidKeyException();
}
$this->fetched[] = $id;
return $record;
}
/**
* exists check the existence of a cache file
* @param integer $id primary key of the cached DAO
* @return boolean whether or not a cache file exists
*/
public function exists($id) {
$name = $this->getTable()->getComponentName();
$file = $this->path.$id.".cache";
return file_exists($file);
}
/**
* deleteAll
* @return void
*/
public function deleteAll() {
foreach(glob($this->path."*.cache") as $file) {
@unlink($file);
}
$fp = fopen($this->path.self::STATS_FILE,"w+");
fwrite($fp,"");
fclose($fp);
}
/**
* delete delete a cache file
* @param integer $id primary key of the cached DAO
*/
public function delete($id) {
$file = $this->path.$id.".cache";
if( ! file_exists($file))
return false;
@unlink($file);
return true;
}
/**
* deleteMultiple delete multiple cache files
* @param array $ids an array containing cache file ids
* @return integer the number of files deleted
*/
public function deleteMultiple(array $ids) {
$deleted = 0;
foreach($ids as $id) {
if($this->delete($id)) $deleted++;
}
return $deleted;
}
/**
* destructor
* the purpose of this destructor is to save all the fetched
* primary keys into the cache stats
*/
public function __destruct() {
if( ! empty($this->fetched)) {
$fp = fopen($this->path.self::STATS_FILE,"a");
fwrite($fp,":".implode(":",$this->fetched));
fclose($fp);
}
/**
*
* cache auto-cleaning algorithm
* $ttl is the number of page loads between each cache cleaning
* the default is 100 page loads
*
* this means that the average number of page loads between
* each cache clean is 100 page loads (= 100 constructed Doctrine_Managers)
*
*/
$ttl = $this->objTable->getAttribute(Doctrine::ATTR_CACHE_TTL);
$l1 = (mt_rand(1,$ttl) / $ttl);
$l2 = (1 - 1/$ttl);
if($l1 > $l2)
$this->clean();
}
}
?>
<?php
/**
* class Doctrine_Collection a collection of data access objects
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*/
class Doctrine_Collection extends Doctrine_Access implements Countable, IteratorAggregate {
/**
* @var array $data an array containing the data access objects of this collection
*/
protected $data = array();
/**
* @var Doctrine_Table $table each collection has only records of specified table
*/
protected $table;
/**
* @var Doctrine_Record $reference collection can belong to a record
*/
protected $reference;
/**
* @var string $reference_field the reference field of the collection
*/
protected $reference_field;
/**
* @var Doctrine_Relation
*/
protected $relation;
/**
* @var boolean $expanded whether or not this collection has been expanded
*/
protected $expanded = false;
/**
* constructor
*/
public function __construct(Doctrine_Table $table) {
$this->table = $table;
}
/**
* @return object Doctrine_Table
*/
public function getTable() {
return $this->table;
}
/**
* @param array $data
*/
public function addData(array $data) {
$this->data[] = $data;
}
/**
* @return mixed
*/
public function getLast() {
return end($this->data);
}
/**
* @return void
*/
public function setReference(Doctrine_Record $record,Doctrine_Relation $relation) {
$this->reference = $record;
$this->relation = $relation;
if($relation instanceof Doctrine_ForeignKey ||
$relation instanceof Doctrine_LocalKey) {
$this->reference_field = $relation->getForeign();
$value = $record->get($relation->getLocal());
foreach($this as $record) {
if($value !== null) {
$record->set($this->reference_field, $value);
} else {
$record->set($this->reference_field, $this->reference);
}
}
}
}
/**
* @return mixed
*/
public function getReference() {
return $this->reference;
}
/**
* @return boolean
*/
public function expand($i = null) {
if( ! isset($this->reference))
return false;
$id = $this->reference->getID();
if(empty($id))
return false;
foreach($this->data as $v) {
switch(gettype($v)):
case "array":
$ids[] = $v['id'];
break;
case "object":
$id = $v->getID();
if( ! empty($id))
$ids[] = $id;
break;
endswitch;
}
if($this instanceof Doctrine_Collection_Immediate) {
$fields = implode(", ",$this->table->getColumnNames());
} else {
$fields = implode(", ",$this->table->getPrimaryKeys());
}
if($this->relation instanceof Doctrine_ForeignKey) {
$str = "";
$params = array($this->reference->getID());
if( ! empty($ids)) {
$str = " && id NOT IN (".substr(str_repeat("?, ",count($ids)),0,-2).")";
$params = array_merge($params,$ids);
}
$str = " WHERE ".$this->reference_field." = ?".$str;
$query = "SELECT ".$fields." FROM ".$this->table->getTableName().$str;
$coll = $this->table->execute($query,$params);
} elseif($this->relation instanceof Doctrine_Association) {
$asf = $fk->getAssociationFactory();
$query = "SELECT ".$foreign." FROM ".$asf->getTableName()." WHERE ".$local."=".$this->getID();
$table = $fk->getTable();
$graph = new Doctrine_DQL_Parser($table->getSession());
$q = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".id IN ($query)";
}
foreach($coll as $record) {
if(isset($this->reference_field))
$record->rawSet($this->reference_field, $this->reference);
$this->reference->addReference($record);
}
return true;
}
/**
* @return boolean
*/
public function remove($key) {
if( ! isset($this->data[$key]))
throw new InvalidKeyException();
$removed = $this->data[$key];
unset($this->data[$key]);
return $removed;
}
/**
* @param mixed $key
* @return boolean
*/
public function contains($key) {
return isset($this->data[$key]);
}
/**
* @param mixed $key
* @return object Doctrine_Record return a specified dao
*/
public function get($key) {
if( ! isset($this->data[$key])) {
$this->expand();
if( ! isset($this->data[$key]))
$this->data[$key] = $this->table->create();
if(isset($this->reference_field)) {
$value = $this->reference->get($this->relation->getLocal());
if($value !== null) {
$this->data[$key]->set($this->reference_field, $value);
} else {
$this->data[$key]->set($this->reference_field, $this->reference);
}
}
}
return $this->data[$key];
}
/**
* @return array an array containing all primary keys
*/
public function getPrimaryKeys() {
$list = array();
foreach($this->data[$key] as $record):
$list[] = $record->getID();
endforeach;
return $list;
}
/**
* returns all keys
* @return array
*/
public function getKeys() {
return array_keys($this->data);
}
/**
* count
* this class implements interface countable
* @return integer number of data access objects in this collection
*/
public function count() {
return count($this->data);
}
/**
* set
* @param integer $key
* @param Doctrine_Record $record
* @return void
*/
public function set($key,Doctrine_Record $record) {
if(isset($this->reference_field))
$record->set($this->reference_field,$this->reference);
$this->data[$key] = $record;
}
/**
* add
* adds a dao instance to this collection
* @param Doctrine_Record $record data access object to be added
* @param string $key optional key for the DAO
* @return boolean
*/
public function add(Doctrine_Record $record,$key = null) {
if(isset($this->reference_field))
$record->rawSet($this->reference_field,$this->reference);
if(isset($key)) {
if(isset($this->data[$key]))
return false;
$this->data[$key] = $record;
}
$this->data[] = $record;
return true;
}
/**
* save
* saves all data access objects
*/
public function save() {
$this->table->getSession()->saveCollection($this);
}
/**
* single shot delete
* deletes all dao instances from this collection
* uses only one database query to perform this operation
* @return boolean
*/
public function delete() {
$ids = $this->table->getSession()->deleteCollection($this);
$this->data = array();
}
/**
* getIterator
* @return object ArrayIterator
*/
public function getIterator() {
$data = $this->data;
return new ArrayIterator($data);
}
/**
* returns a string representation of this object
*/
public function __toString() {
return Doctrine_Lib::getCollectionAsString($this);
}
}
?>
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Collection.class.php");
/**
* Doctrine_Collection_Batch a collection of data access objects,
* with batch load strategy
*
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*/
class Doctrine_Collection_Batch extends Doctrine_Collection {
/**
* @var integer $batchSize batch size
*/
private $batchSize;
/**
* @var array $loaded an array containing the loaded batches, keys representing the batch indexes
*/
private $loaded = array();
public function __construct(Doctrine_DQL_Parser $graph,$key) {
parent::__construct($graph->getTable($key));
$this->data = $graph->getData($key);
if( ! is_array($this->data))
$this->data = array();
$this->batchSize = $this->getTable()->getAttribute(Doctrine::ATTR_BATCH_SIZE);
}
/**
* @param integer $batchSize batch size
*/
public function setBatchSize($batchSize) {
$batchSize = (int) $batchSize;
if($batchSize <= 0)
return false;
$this->batchSize = $batchSize;
return true;
}
/**
* @return integer
*/
public function getBatchSize() {
return $this->batchSize;
}
/**
* load load a specified element, by loading the batch the element is part of
* @param Doctrine_Record $record data access object
* @return boolean whether or not the load operation was successful
*/
public function load(Doctrine_Record $record) {
if(empty($this->data))
return false;
$id = $record->getID();
foreach($this->data as $key => $v) {
if(is_object($v)) {
if($v->getID() == $id)
break;
} elseif(is_array($v["id"])) {
if($v["id"] == $id)
break;
}
}
$x = floor($key / $this->batchSize);
if( ! isset($this->loaded[$x])) {
$e = $x * $this->batchSize;
$e2 = ($x + 1)* $this->batchSize;
$a = array();
$proxies = array();
for($i = $e; $i < $e2 && $i < $this->count(); $i++):
if(is_object($this->data[$i]))
$id = $this->data[$i]->getID();
elseif(is_array($this->data[$i]))
$id = $this->data[$i]["id"];
$load = false;
// check the cache
// no need of fetching the same data twice
try {
$record = $this->table->getCache()->fetch($id);
} catch(InvalidKeyException $ex) {
$load = true;
}
if($load)
$a[] = $id;
endfor;
$c = count($a);
$query = $this->table->getQuery()." WHERE ";
$query .= ($c > 1)?"id IN (":"id = ";
$query .= substr(str_repeat("?, ",count($a)),0,-2);
$query .= ($c > 1)?")":"";
$stmt = $this->table->getSession()->execute($query,$a);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)):
$this->table->setData($row);
if(is_object($this->data[$e])) {
$this->data[$e]->factoryRefresh($this->table);
} else {
$this->data[$e] = $this->table->getRecord();
}
$e++;
endwhile;
$this->loaded[$x] = true;
return true;
} else {
return false;
}
}
/**
* get
* @param mixed $key the key of the data access object
* @return object Doctrine_Record data access object
*/
public function get($key) {
if(isset($this->data[$key])) {
switch(gettype($this->data[$key])):
case "array":
try {
// try to fetch the Doctrine_Record from cache
if( ! isset($this->data[$key]["id"]))
throw new InvalidKeyException();
$record = $this->table->getCache()->fetch($this->data[$key]["id"]);
} catch(InvalidKeyException $e) {
// Doctrine_Record didn't exist in cache
$this->table->setData($this->data[$key]);
$proxy = $this->table->getProxy();
$record = $proxy;
}
$record->addCollection($this);
break;
case "object":
$record = $this->data[$key];
break;
endswitch;
} else {
$this->expand();
if(isset($this->data[$key])) {
$record = $this->data[$key];
} else {
$record = $this->table->create();
}
}
if(isset($this->reference_field))
$record->set($this->reference_field,$this->reference);
$this->data[$key] = $record;
return $this->data[$key];
}
/**
* @return Doctrine_BatchIterator
*/
public function getIterator() {
return new Doctrine_BatchIterator($this);
}
}
?>
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Collection.class.php");
/**
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*/
class Doctrine_Collection_Immediate extends Doctrine_Collection {
/**
* @param Doctrine_DQL_Parser $graph
* @param integer $key
*/
public function __construct(Doctrine_DQL_Parser $graph,$key) {
parent::__construct($graph->getTable($key));
$name = $this->table->getComponentName();
$data = $graph->getData($name);
if(is_array($data)) {
foreach($data as $k=>$v):
$this->table->setData($v);
$this->add($this->table->getRecord());
endforeach;
}
}
}
?>
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Collection.class.php");
/**
* a collection of Doctrine_Record objects with lazy load strategy
* (batch load strategy with batch size 1)
*/
class Doctrine_Collection_Lazy extends Doctrine_Collection_Batch {
/**
* constructor
* @param Doctrine_DQL_Parser $graph
* @param string $key
*/
public function __construct(Doctrine_DQL_Parser $graph,$key) {
parent::setBatchSize(1);
parent::__construct($graph,$key);
}
}
?>
<?php
/**
* Doctrine_Configurable
* the base for Doctrine_Table, Doctrine_Manager and Doctrine_Session
*
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 beta2
*
*/
abstract class Doctrine_Configurable {
/**
* @var array $attributes an array of containing all attributes
*/
private $attributes = array();
/**
* @var $parent the parents of this component
*/
private $parent;
/**
* @throws Exception if the value is invalid
* @param integer $attribute
* @param mixed $value
* @return void
*/
final public function setAttribute($attribute,$value) {
switch($attribute):
case Doctrine::ATTR_BATCH_SIZE:
if($value < 0)
throw new Doctrine_Exception("Batch size should be greater than or equal to zero");
break;
case Doctrine::ATTR_CACHE_DIR:
if(substr(trim($value),0,6) == "%ROOT%") {
$dir = dirname(__FILE__);
$value = $dir.substr($value,6);
}
if(! is_dir($value) && ! file_exists($value))
mkdir($value,0777);
break;
case Doctrine::ATTR_CACHE_TTL:
if($value < 1)
throw new Doctrine_Exception("Cache TimeToLive should be greater than or equal to 1");
break;
case Doctrine::ATTR_CACHE_SIZE:
if($value < 1)
throw new Doctrine_Exception("Cache size should be greater than or equal to 1");
break;
case Doctrine::ATTR_CACHE_SLAM:
if($value < 0 || $value > 1)
throw new Doctrine_Exception("Cache slam defense should be a floating point number between 0 and 1");
break;
case Doctrine::ATTR_FETCHMODE:
if($value < 0)
throw new Doctrine_Exception("Unknown fetchmode. Fetchmode should be an integer between 0 and 2. See Doctrine::FETCH_* constants.");
break;
case Doctrine::ATTR_LISTENER:
$this->setEventListener($value);
break;
case Doctrine::ATTR_PK_COLUMNS:
if( ! is_array($value))
throw new Doctrine_Exception("The value of Doctrine::ATTR_PK_COLUMNS attribute must be an array");
break;
case Doctrine::ATTR_PK_TYPE:
if($value != Doctrine::INCREMENT_KEY && $value != Doctrine::UNIQUE_KEY)
throw new Doctrine_Exception("The value of Doctrine::ATTR_PK_TYPE attribute must be either Doctrine::INCREMENT_KEY or Doctrine::UNIQUE_KEY");
break;
case Doctrine::ATTR_LOCKMODE:
if($this instanceof Doctrine_Session) {
if($this->getState() != Doctrine_Session::STATE_OPEN)
throw new Doctrine_Exception("Couldn't set lockmode. There are transactions open.");
} elseif($this instanceof Doctrine_Manager) {
foreach($this as $session) {
if($session->getState() != Doctrine_Session::STATE_OPEN)
throw new Doctrine_Exception("Couldn't set lockmode. There are transactions open.");
}
} else {
throw new Doctrine_Exception("Lockmode attribute can only be set at the global or session level.");
}
break;
case Doctrine::ATTR_CREATE_TABLES:
$value = (bool) $value;
break;
case Doctrine::ATTR_VLD:
break;
case Doctrine::ATTR_CACHE:
if($value != Doctrine::CACHE_FILE && $value != Doctrine::CACHE_NONE)
throw new Doctrine_Exception("Unknown cache container. See Doctrine::CACHE_* constants for availible containers.");
break;
default:
throw new Exception("Unknown attribute.");
endswitch;
$this->attributes[$attribute] = $value;
}
/**
* @param Doctrine_EventListener $listener
* @return void
*/
final public function setEventListener(Doctrine_EventListener $listener) {
$i = Doctrine::ATTR_LISTENER;
$this->attributes[$i] = $listener;
}
/**
* @return mixed the value of the attribute
*/
final public function getAttribute($attribute) {
$attribute = (int) $attribute;
if($attribute < 1 || $attribute > 14)
throw new InvalidKeyException();
if( ! isset($this->attributes[$attribute])) {
if(isset($this->parent))
return $this->parent->getAttribute($attribute);
return null;
}
return $this->attributes[$attribute];
}
/**
* getAttributes
* @return array
*/
final public function getAttributes() {
return $this->attributes;
}
/**
* @param Doctrine_Configurable $component
* @return void
*/
final public function setParent(Doctrine_Configurable $component) {
$this->parent = $component;
}
/**
* getParent
*/
final public function getParent() {
return $this->parent;
}
}
?>
<?php
abstract class Doctrine_Component {
/**
* setTableName
* @param string $name table name
* @return void
*/
final public function setTableName($name) {
$this->getComponent()->setTableName($name);
}
/**
* setInheritanceMap
* @param array $inheritanceMap
* @return void
*/
final public function setInheritanceMap(array $inheritanceMap) {
$this->getComponent()->setInheritanceMap($inheritanceMap);
}
/**
* setAttribute
* @param integer $attribute
* @param mixed $value
* @see Doctrine::ATTR_* constants
* @return void
*/
final public function setAttribute($attribute,$value) {
$this->getComponent()->setAttribute($attribute,$value);
}
/**
* @param string $objTableName
* @param string $fkField
* @return void
*/
final public function ownsOne($componentName,$foreignKey) {
$this->getComponent()->bind($componentName,$foreignKey,Doctrine_Table::ONE_COMPOSITE);
}
/**
* @param string $objTableName
* @param string $fkField
* @return void
*/
final public function ownsMany($componentName,$foreignKey) {
$this->getComponent()->bind($componentName,$foreignKey,Doctrine_Table::MANY_COMPOSITE);
}
/**
* @param string $objTableName
* @param string $fkField
* @return void
*/
final public function hasOne($componentName,$foreignKey) {
$this->getComponent()->bind($componentName,$foreignKey,Doctrine_Table::ONE_AGGREGATE);
}
/**
* @param string $objTableName
* @param string $fkField
* @return void
*/
final public function hasMany($componentName,$foreignKey) {
$this->getComponent()->bind($componentName,$foreignKey,Doctrine_Table::MANY_AGGREGATE);
}
abstract public function getComponent();
}
?>
<?php
class Doctrine_DB extends PDO implements Countable, IteratorAggregate {
/**
* default DSN
*/
const DSN = "mysql://zYne:dc34@localhost/test";
/**
* executed queries
*/
private $queries = array();
/**
* execution times of the executed queries
*/
private $exectimes = array();
/**
* constructor
* @param string $dsn
* @param string $username
* @param string $password
*/
public function __construct($dsn,$username,$password) {
parent::__construct($dsn,$username,$password);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array("Doctrine_DBStatement",array($this)));
}
/**
* @param string $dsn PEAR::DB like DSN
* format: schema://user:password@address/dbname
*/
public static function getConnection($dsn = null) {
static $instance;
if( ! isset($instance)) {
if( ! isset($dsn)) {
$a = parse_url(self::DSN);
} else {
$a = parse_url($dsn);
}
$e = array();
$e[0] = $a["scheme"].":host=".$a["host"].";dbname=".substr($a["path"],1);
$e[1] = $a["user"];
$e[2] = $a["pass"];
$instance = new Doctrine_DB($e[0],$e[1],$e[2]);
}
return $instance;
}
/**
* @param string $query query to be executed
*/
public function query($query) {
$this->queries[] = $query;
$time = microtime();
$stmt = parent::query($query);
$this->exectimes[] = (microtime() - $time);
return $stmt;
}
/**
* @param string $query query to be prepared
*/
public function prepare($query) {
$this->queries[] = $query;
return parent::prepare($query);
}
/**
* @param string $time exectime of the last executed query
* @return void
*/
public function addExecTime($time) {
$this->exectimes[] = $time;
}
public function getExecTimes() {
return $this->exectimes;
}
/**
* @return array an array of executed queries
*/
public function getQueries() {
return $this->queries;
}
/**
* @return ArrayIterator
*/
public function getIterator() {
return new ArrayIterator($this->queries);
}
/**
* returns the number of executed queries
* @return integer
*/
public function count() {
return count($this->queries);
}
}
class Doctrine_DBStatement extends PDOStatement {
/**
* @param Doctrine_DB $dbh Doctrine Database Handler
*/
private $dbh;
/**
* @param Doctrine_DB $dbh
*/
private function __construct(Doctrine_DB $dbh) {
$this->dbh = $dbh;
}
/**
* @param array $params
*/
public function execute($params) {
$time = microtime();
$result = parent::execute($params);
$exectime = (microtime() - $time);
$this->dbh->addExecTime($exectime);
return $result;
}
}
?>
This diff is collapsed.
<?php
class Doctrine_DataDict {
private $table;
public function __construct(Doctrine_Table $table) {
$this->table = $table;
$manager = $this->table->getSession()->getManager();
require_once($manager->getRoot()."/adodb-hack/adodb.inc.php");
$dbh = $this->table->getSession()->getDBH();
$this->dict = NewDataDictionary($dbh);
}
public function metaColumns() {
return $this->dict->metaColumns($this->table->getTableName());
}
public function createTable() {
foreach($this->table->getColumns() as $name => $args) {
$r[] = $name." ".$this->getADOType($args[0],$args[1])." ".$args[2];
}
$dbh = $this->table->getSession()->getDBH();
$r = implode(", ",$r);
$a = $this->dict->createTableSQL($this->table->getTableName(),$r);
$return = true;
foreach($a as $sql) {
try {
$dbh->query($sql);
} catch(PDOException $e) {
$return = false;
}
}
return $return;
}
/**
* converts doctrine type to adodb type
*
* @param string $type column type
* @param integer $length column length
*/
public function getADOType($type,$length) {
switch($type):
case "string":
case "s":
if($length < 255)
return "C($length)";
elseif($length < 4000)
return "X";
break;
case "mbstring":
if($length < 255)
return "C2($length)";
return "X2";
case "clob":
return "XL";
break;
case "float":
case "f":
case "double":
return "F";
break;
case "timestamp":
case "t":
return "T";
break;
case "boolean":
case "bool":
return "L";
break;
case "integer":
case "int":
case "i":
if(empty($length))
return "I8";
elseif($length < 4)
return "I1";
elseif($length < 6)
return "I2";
elseif($length < 10)
return "I4";
elseif($length <= 20)
return "I8";
else
throw new Doctrine_Exception("Too long integer (max length is 20).");
break;
endswitch;
}
}
?>
<?php
require_once("EventListener.class.php");
class Doctrine_DebugMessage {
private $code;
private $object;
public function __construct($object, $code) {
$this->object = $object;
$this->code = $code;
}
final public function getCode() {
return $this->code;
}
final public function getObject() {
return $this->object;
}
}
class Doctrine_Debugger extends Doctrine_EventListener {
const EVENT_LOAD = 1;
const EVENT_PRELOAD = 2;
const EVENT_SLEEP = 3;
const EVENT_WAKEUP = 4;
const EVENT_UPDATE = 5;
const EVENT_PREUPDATE = 6;
const EVENT_CREATE = 7;
const EVENT_PRECREATE = 8;
const EVENT_SAVE = 9;
const EVENT_PRESAVE = 10;
const EVENT_INSERT = 11;
const EVENT_PREINSERT = 12;
const EVENT_DELETE = 13;
const EVENT_PREDELETE = 14;
const EVENT_EVICT = 15;
const EVENT_PREEVICT = 16;
const EVENT_CLOSE = 17;
const EVENT_PRECLOSE = 18;
const EVENT_OPEN = 19;
const EVENT_COMMIT = 20;
const EVENT_PRECOMMIT = 21;
const EVENT_ROLLBACK = 22;
const EVENT_PREROLLBACK = 23;
const EVENT_BEGIN = 24;
const EVENT_PREBEGIN = 25;
const EVENT_COLLDELETE = 26;
const EVENT_PRECOLLDELETE = 27;
private $debug;
public function getMessages() {
return $this->debug;
}
public function onLoad(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_LOAD);
}
public function onPreLoad(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PRELOAD);
}
public function onSleep(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_SLEEP);
}
public function onWakeUp(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_WAKEUP);
}
public function onUpdate(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_UPDATE);
}
public function onPreUpdate(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PREUPDATE);
}
public function onCreate(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_CREATE);
}
public function onPreCreate(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PRECREATE);
}
public function onSave(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_SAVE);
}
public function onPreSave(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PRESAVE);
}
public function onInsert(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_INSERT);
}
public function onPreInsert(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PREINSERT);
}
public function onDelete(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_DELETE);
}
public function onPreDelete(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PREDELETE);
}
public function onEvict(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_EVICT);
}
public function onPreEvict(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PREEVICT);
}
public function onClose(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_CLOSE);
}
public function onPreClose(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_PRECLOSE);
}
public function onOpen(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_OPEN);
}
public function onTransactionCommit(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_COMMIT);
}
public function onPreTransactionCommit(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_PRECOMMIT);
}
public function onTransactionRollback(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_ROLLBACK);
}
public function onPreTransactionRollback(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_PREROLLBACK);
}
public function onTransactionBegin(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_BEGIN);
}
public function onPreTransactionBegin(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_PREBEGIN);
}
public function onCollectionDelete(Doctrine_Collection $collection) {
$this->debug[] = new Doctrine_DebugMessage($collection,self::EVENT_COLLDELETE);
}
public function onPreCollectionDelete(Doctrine_Collection $collection) {
$this->debug[] = new Doctrine_DebugMessage($collection,self::EVENT_PRECOLLDELETE);
}
}
?>
<?php
require_once("Exception.class.php");
/**
* Doctrine
* the base class of Doctrine framework
*/
final class Doctrine {
/**
* ERROR MODE CONSTANTS
*/
/**
* NO PRIMARY KEY COLUMN ERROR
* no primary key column found error code
*/
const ERR_NO_PK = 0;
/**
* PRIMARY KEY MISMATCH ERROR
* this error code is used when user uses factory refresh for a
* given Doctrine_Record and the old primary key doesn't match the new one
*/
const ERR_REFRESH = 1;
/**
* FIND ERROR
* this code used when for example Doctrine_Table::find() is called and
* a Data Access Object is not found
*/
const ERR_FIND = 2;
/**
* TABLE NOT FOUND ERROR
* this error code is used when user tries to initialize
* a table and there is no database table for this factory
*/
const ERR_NOSUCH_TABLE = 3;
/**
* NAMING ERROR
* this code is used when user defined Doctrine_Table is badly named
*/
const ERR_NAMING = 5;
/**
* TABLE INSTANCE ERROR
* this code is used when user tries to initialize
* a table that is already initialized
*/
const ERR_TABLE_INSTANCE = 6;
/**
* NO OPEN SESSIONS ERROR
* error code which is used when user tries to get
* current session are there are no sessions open
*/
const ERR_NO_SESSIONS = 7;
/**
* MAPPING ERROR
* if there is something wrong with mapping logic
* this error code is used
*/
const ERR_MAPPING = 8;
/**
* ATTRIBUTE CONSTANTS
*/
/**
* event listener attribute
*/
const ATTR_LISTENER = 1;
/**
* fetchmode attribute
*/
const ATTR_FETCHMODE = 2;
/**
* cache directory attribute
*/
const ATTR_CACHE_DIR = 3;
/**
* cache time to live attribute
*/
const ATTR_CACHE_TTL = 4;
/**
* cache size attribute
*/
const ATTR_CACHE_SIZE = 5;
/**
* cache slam defense probability
*/
const ATTR_CACHE_SLAM = 6;
/**
* cache container attribute
*/
const ATTR_CACHE = 7;
/**
* batch size attribute
*/
const ATTR_BATCH_SIZE = 8;
/**
* primary key columns attribute
*/
const ATTR_PK_COLUMNS = 9;
/**
* primary key type attribute
*/
const ATTR_PK_TYPE = 10;
/**
* locking attribute
*/
const ATTR_LOCKMODE = 11;
/**
* validatate attribute
*/
const ATTR_VLD = 12;
/**
* name prefix attribute
*/
const ATTR_NAME_PREFIX = 13;
/**
* create tables attribute
*/
const ATTR_CREATE_TABLES = 14;
/**
* CACHE CONSTANTS
*/
/**
* file cache constant
*/
const CACHE_FILE = 0;
/**
* constant for disabling the caching
*/
const CACHE_NONE = 1;
/**
* FETCHMODE CONSTANTS
*/
/**
* IMMEDIATE FETCHING
* mode for immediate fetching
*/
const FETCH_IMMEDIATE = 0;
/**
* BATCH FETCHING
* mode for batch fetching
*/
const FETCH_BATCH = 1;
/**
* LAZY FETCHING
* mode for lazy fetching
*/
const FETCH_LAZY = 2;
/**
* LOCKMODE CONSTANTS
*/
/**
* mode for optimistic locking
*/
const LOCK_OPTIMISTIC = 0;
/**
* mode for pessimistic locking
*/
const LOCK_PESSIMISTIC = 1;
/**
* PRIMARY KEY TYPE CONSTANTS
*/
/**
* auto-incremented/(sequence updated) primary key
*/
const INCREMENT_KEY = 0;
/**
* unique key
*/
const UNIQUE_KEY = 1;
/**
* @var string $path doctrine root directory
*/
private static $path;
public static function getPath() {
if(! self::$path)
self::$path = dirname(__FILE__);
return self::$path;
}
/**
* loads all runtime classes
*/
public static function loadAll() {
if(! self::$path)
self::$path = dirname(__FILE__);
$dir = dir(self::$path);
$a = array();
while (false !== ($entry = $dir->read())) {
switch($entry):
case ".":
case "..":
break;
case "Cache":
case "Record":
case "Collection":
case "Table":
case "Validator":
case "Exception":
case "Session":
case "DQL":
$a[] = self::$path.DIRECTORY_SEPARATOR.$entry;
break;
default:
if(is_file(self::$path.DIRECTORY_SEPARATOR.$entry)) {
require_once($entry);
}
endswitch;
}
foreach($a as $dirname) {
$dir = dir($dirname);
$path = $dirname.DIRECTORY_SEPARATOR;
while (false !== ($entry = $dir->read())) {
if(is_file($path.$entry)) {
require_once($path.$entry);
}
}
}
}
/**
* simple autoload function
*/
public static function autoload($classname) {
if(! self::$path)
self::$path = dirname(__FILE__);
$e = explode("_",$classname);
if($e[0] != "Doctrine")
return false;
if(end($e) != "Exception") {
if(count($e) > 2) {
array_shift($e);
$dir = array_shift($e);
$class = self::$path.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.implode('',$e).".class.php";
} elseif(count($e) > 1) {
$class = self::$path.DIRECTORY_SEPARATOR.$e[1].".class.php";
} else
return false;
} else {
$class = self::$path.DIRECTORY_SEPARATOR."Exception".DIRECTORY_SEPARATOR.$e[1].".class.php";
}
if( ! file_exists($class)) {
return false;
}
require_once($class);
return true;
}
}
?>
<?php
/**
* interface for event listening, forces all classes that extend
* Doctrine_EventListener to have the same method arguments as their parent
*/
interface iDoctrine_EventListener {
public function onLoad(Doctrine_Record $record);
public function onPreLoad(Doctrine_Record $record);
public function onUpdate(Doctrine_Record $record);
public function onPreUpdate(Doctrine_Record $record);
public function onCreate(Doctrine_Record $record);
public function onPreCreate(Doctrine_Record $record);
public function onSave(Doctrine_Record $record);
public function onPreSave(Doctrine_Record $record);
public function onInsert(Doctrine_Record $record);
public function onPreInsert(Doctrine_Record $record);
public function onDelete(Doctrine_Record $record);
public function onPreDelete(Doctrine_Record $record);
public function onEvict(Doctrine_Record $record);
public function onPreEvict(Doctrine_Record $record);
public function onSaveCascade(Doctrine_Record $record);
public function onPreSaveCascade(Doctrine_Record $record);
public function onDeleteCascade(Doctrine_Record $record);
public function onPreDeleteCascade(Doctrine_Record $record);
public function onSleep(Doctrine_Record $record);
public function onWakeUp(Doctrine_Record $record);
public function onClose(Doctrine_Session $session);
public function onPreClose(Doctrine_Session $session);
public function onOpen(Doctrine_Session $session);
public function onTransactionCommit(Doctrine_Session $session);
public function onPreTransactionCommit(Doctrine_Session $session);
public function onTransactionRollback(Doctrine_Session $session);
public function onPreTransactionRollback(Doctrine_Session $session);
public function onTransactionBegin(Doctrine_Session $session);
public function onPreTransactionBegin(Doctrine_Session $session);
public function onCollectionDelete(Doctrine_Collection $collection);
public function onPreCollectionDelete(Doctrine_Collection $collection);
}
/**
* Doctrine_EventListener all event listeners extend this base class
* the empty methods allow child classes to only implement the methods they need to implement
*
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*
*/
abstract class Doctrine_EventListener implements iDoctrine_EventListener {
public function onLoad(Doctrine_Record $record) { }
public function onPreLoad(Doctrine_Record $record) { }
public function onSleep(Doctrine_Record $record) { }
public function onWakeUp(Doctrine_Record $record) { }
public function onUpdate(Doctrine_Record $record) { }
public function onPreUpdate(Doctrine_Record $record) { }
public function onCreate(Doctrine_Record $record) { }
public function onPreCreate(Doctrine_Record $record) { }
public function onSave(Doctrine_Record $record) { }
public function onPreSave(Doctrine_Record $record) { }
public function onInsert(Doctrine_Record $record) { }
public function onPreInsert(Doctrine_Record $record) { }
public function onDelete(Doctrine_Record $record) { }
public function onPreDelete(Doctrine_Record $record) { }
public function onEvict(Doctrine_Record $record) { }
public function onPreEvict(Doctrine_Record $record) { }
public function onSaveCascade(Doctrine_Record $record) { }
public function onPreSaveCascade(Doctrine_Record $record) { }
public function onDeleteCascade(Doctrine_Record $record) { }
public function onPreDeleteCascade(Doctrine_Record $record) { }
public function onClose(Doctrine_Session $session) { }
public function onPreClose(Doctrine_Session $session) { }
public function onOpen(Doctrine_Session $session) { }
public function onTransactionCommit(Doctrine_Session $session) { }
public function onPreTransactionCommit(Doctrine_Session $session) { }
public function onTransactionRollback(Doctrine_Session $session) { }
public function onPreTransactionRollback(Doctrine_Session $session) { }
public function onTransactionBegin(Doctrine_Session $session) { }
public function onPreTransactionBegin(Doctrine_Session $session) { }
public function onCollectionDelete(Doctrine_Collection $collection) { }
public function onPreCollectionDelete(Doctrine_Collection $collection) { }
}
/**
* an emtpy listener all components use this by default
*/
class EmptyEventListener extends Doctrine_EventListener { }
?>
<?php
class InvalidKeyException extends Exception { }
class InvalidTypeException extends Exception { }
class Doctrine_Exception extends Exception { }
class DQLException extends Doctrine_Exception { }
?>
<?php
/**
* thrown when user tries to find a Doctrine_Record for given primary key and that object is not found
*/
class Doctrine_Find_Exception extends Doctrine_Exception {
public function __construct() {
parent::__construct("Couldn't find Data Access Object.",Doctrine::ERR_FIND);
}
}
?>
<?php
/**
* thrown when user tries to get a foreign key object but the mapping is not done right
*/
class Doctrine_Mapping_Exception extends Doctrine_Exception {
public function __construct() {
parent::__construct("An error occured in the mapping logic.",Doctrine::ERR_MAPPING);
}
}
?>
<?php
/**
* thrown when user defined Doctrine_Table is badly named
*/
class Doctrine_Naming_Exception extends Doctrine_Exception {
public function __construct() {
parent::__construct("Badly named Doctrine_Table. Each Doctrine_Table
must be in format [Name]Table.", Doctrine::ERR_NAMING);
}
}
?>
<?php
/**
* thrown when Doctrine_Record is loaded and there is no primary key field
*/
class Doctrine_PrimaryKey_Exception extends Doctrine_Exception {
public function __construct() {
parent::__construct("No primary key column found. Each data set must have primary key column.", Doctrine::ERR_NO_PK);
}
}
?>
<?php
/**
* thrown when Doctrine_Record is refreshed and the refreshed primary key doens't match the old one
*/
class Doctrine_Refresh_Exception extends Doctrine_Exception {
public function __construct() {
parent::__construct("The refreshed primary key doesn't match the
one in the record memory.", Doctrine::ERR_REFRESH);
}
}
?>
<?php
/**
* thrown when user tries to get the current
* session and there are no open sessions
*/
class Doctrine_Session_Exception extends Doctrine_Exception {
public function __construct() {
parent::__construct("There are no opened sessions. Use
Doctrine_Manager::getInstance()->openSession() to open a new session.",Doctrine::ERR_NO_SESSIONS);
}
}
?>
<?php
/**
* thrown when user tries to initialize a new instance of Doctrine_Table,
* while there already exists an instance of that table
*/
class Doctrine_Table_Exception extends Doctrine_Exception {
public function __construct() {
parent::__construct("Couldn't initialize table. One instance of this
tabke already exists. Always use Doctrine_Session::getTable(\$name)
to get on instance of a Doctrine_Table.",Doctrine::ERR_TABLE_INSTANCE);
}
}
?>
<?php
class Doctrine_Validator_Exception extends Doctrine_Exception {
private $validator;
public function __construct(Doctrine_Validator $validator) {
$this->validator = $validator;
}
public function getErrorStack() {
return $this->validator->getErrorStack();
}
}
?>
<?php
/**
* Foreign Key
*/
class Doctrine_ForeignKey extends Doctrine_Relation { }
?>
<?php
/**
* Doctrine_Form_Builder
*/
class Doctrine_Form_Builder {
public static function buildForm(Doctrine_Record $record) {
}
}
class Doctrine_Element {
private $attributes = array();
private $data;
public function toHtml() {
return "<".$this->name.">"."</>";
}
}
class InputElement {
private $attributes = array();
}
?>
<?php
class Doctrine_Lib {
/**
* @param integer $state the state of record
* @see Doctrine_Record::STATE_* constants
* @return string string representation of given state
*/
public static function getRecordStateAsString($state) {
switch($state):
case Doctrine_Record::STATE_PROXY:
return "proxy";
break;
case Doctrine_Record::STATE_CLEAN:
return "persistent clean";
break;
case Doctrine_Record::STATE_DIRTY:
return "persistent dirty";
break;
case Doctrine_Record::STATE_TDIRTY:
return "transient dirty";
break;
case Doctrine_Record::STATE_TCLEAN:
return "transient clean";
break;
endswitch;
}
/**
* returns a string representation of Doctrine_Record object
* @param Doctrine_Record $record
* @return string
*/
public function getRecordAsString(Doctrine_Record $record) {
$r[] = "<pre>";
$r[] = "Component : ".$record->getTable()->getComponentName();
$r[] = "ID : ".$record->getID();
$r[] = "References : ".count($record->getReferences());
$r[] = "State : ".Doctrine_Lib::getRecordStateAsString($record->getState());
$r[] = "OID : ".$record->getOID();
$r[] = "</pre>";
return implode("\n",$r)."<br />";
}
/**
* getStateAsString
* returns a given session state as string
* @param integer $state session state
*/
public static function getSessionStateAsString($state) {
switch($state):
case Doctrine_Session::STATE_OPEN:
return "open";
break;
case Doctrine_Session::STATE_CLOSED:
return "closed";
break;
case Doctrine_Session::STATE_BUSY:
return "busy";
break;
case Doctrine_Session::STATE_ACTIVE:
return "active";
break;
endswitch;
}
/**
* returns a string representation of Doctrine_Session object
* @param Doctrine_Session $session
* @return string
*/
public function getSessionAsString(Doctrine_Session $session) {
$r[] = "<pre>";
$r[] = "Doctrine_Session object";
$r[] = "State : ".Doctrine_Session::getStateAsString($session->getState());
$r[] = "Open Transactions : ".$session->getTransactionLevel();
$r[] = "Open Factories : ".$session->count();
$sum = 0;
$rsum = 0;
$csum = 0;
foreach($session->getTables() as $objTable) {
if($objTable->getCache() instanceof Doctrine_Cache_File) {
$sum += array_sum($objTable->getCache()->getStats());
$rsum += $objTable->getRepository()->count();
$csum += $objTable->getCache()->count();
}
}
$r[] = "Cache Hits : ".$sum." hits ";
$r[] = "Cache : ".$csum." objects ";
$r[] = "Repositories : ".$rsum." objects ";
$queries = false;
if($session->getDBH() instanceof Doctrine_DB) {
$handler = "Doctrine Database Handler";
$queries = count($this->dbh->getQueries());
$sum = array_sum($this->dbh->getExecTimes());
} elseif($this->dbh instanceof PDO) {
$handler = "PHP Native PDO Driver";
} else
$handler = "Unknown Database Handler";
$r[] = "DB Handler : ".$handler;
if($queries) {
$r[] = "Executed Queries : ".$queries;
$r[] = "Sum of Exec Times : ".$sum;
}
$r[] = "</pre>";
return implode("\n",$r)."<br>";
}
/**
* returns a string representation of Doctrine_Table object
* @param Doctrine_Table $table
* @return string
*/
public function getTableAsString(Doctrine_Table $table) {
$r[] = "<pre>";
$r[] = "Component : ".$this->getComponentName();
$r[] = "Table : ".$this->getTableName();
$r[] = "Repository : ".$this->getRepository()->count()." objects";
if($this->cache instanceof Doctrine_Cache_File) {
$r[] = "Cache : ".$this->getCache()->count()." objects";
$r[] = "Cache hits : ".array_sum($this->getCache()->getStats())." hits";
}
$r[] = "</pre>";
return implode("\n",$r)."<br>";
}
/**
* returns a string representation of Doctrine_Collection object
* @param Doctrine_Collection $collection
* @return string
*/
public function getCollectionAsString(Doctrine_Collection $collection) {
$r[] = "<pre>";
$r[] = get_class($collection);
foreach($collection as $key => $record) {
$r[] = "Key : ".$key." ID : ".$record->getID();
}
$r[] = "</pre>";
return implode("\n",$r);
}
}
?>
<?php
/**
* Local Key
*/
class Doctrine_LocalKey extends Doctrine_Relation { }
?>
<?php
require_once("Configurable.class.php");
require_once("EventListener.class.php");
/**
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*/
class Doctrine_Manager extends Doctrine_Configurable implements Countable, IteratorAggregate {
/**
* @var array $session an array containing all the opened sessions
*/
private $sessions = array();
/**
* @var integer $index
*/
private $index = 0;
/**
* @var integer $currIndex
*/
private $currIndex = 0;
/**
* @var string $root
*/
private $root;
/**
* constructor
*/
private function __construct() {
$this->root = dirname(__FILE__);
}
/**
* setDefaultAttributes
*/
final public function setDefaultAttributes() {
static $init = false;
if( ! $init) {
$init = true;
$attributes = array(
Doctrine::ATTR_CACHE_DIR => "%ROOT%".DIRECTORY_SEPARATOR."cachedir",
Doctrine::ATTR_FETCHMODE => Doctrine::FETCH_LAZY,
Doctrine::ATTR_CACHE_TTL => 100,
Doctrine::ATTR_CACHE_SIZE => 100,
Doctrine::ATTR_CACHE => Doctrine::CACHE_FILE,
Doctrine::ATTR_BATCH_SIZE => 5,
Doctrine::ATTR_LISTENER => new EmptyEventListener(),
Doctrine::ATTR_PK_COLUMNS => array("id"),
Doctrine::ATTR_PK_TYPE => Doctrine::INCREMENT_KEY,
Doctrine::ATTR_LOCKMODE => 1,
Doctrine::ATTR_VLD => false,
Doctrine::ATTR_CREATE_TABLES => true
);
foreach($attributes as $attribute => $value) {
$old = $this->getAttribute($attribute);
if($old === null)
$this->setAttribute($attribute,$value);
}
}
}
/**
* @return string the root directory of Doctrine
*/
final public function getRoot() {
return $this->root;
}
/**
* getInstance this class uses the singleton pattern
*/
final public static function getInstance() {
static $instance;
if( ! isset($instance))
$instance = new Doctrine_Manager();
return $instance;
}
/**
* openSession open a new session and save it to Doctrine_Manager->sessions
* @param PDO $pdo PDO database driver
* @param string $name name of the session, if empty numeric key is used
* @return Doctrine_Session the opened session object
*/
final public function openSession(PDO $pdo, $name = null) {
// initialize the default attributes
$this->setDefaultAttributes();
if($name !== null) {
$name = (string) $name;
if(isset($this->sessions[$name]))
throw new InvalidKeyException();
} else {
$name = $this->index;
$this->index++;
}
switch($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)):
case "mysql":
$this->sessions[$name] = new Doctrine_Session_Mysql($this,$pdo);
break;
case "sqlite":
$this->sessions[$name] = new Doctrine_Session_Sqlite($this,$pdo);
break;
case "pgsql":
$this->sessions[$name] = new Doctrine_Session_Pgsql($this,$pdo);
break;
case "oci":
$this->sessions[$name] = new Doctrine_Session_Oracle($this,$pdo);
break;
case "mssql":
$this->sessions[$name] = new Doctrine_Session_Mssql($this,$pdo);
break;
case "firebird":
$this->sessions[$name] = new Doctrine_Session_Firebird($this,$pdo);
break;
case "informix":
$this->sessions[$name] = new Doctrine_Session_Informix($this,$pdo);
break;
endswitch;
$this->currIndex = $name;
return $this->sessions[$name];
}
/**
* getSession
* @param integer $index
* @return object Doctrine_Session
* @throws InvalidKeyException
*/
final public function getSession($index) {
if( ! isset($this->sessions[$index]))
throw new InvalidKeyException();
$this->currIndex = $index;
return $this->sessions[$index];
}
/**
* closes the session
*
* @param Doctrine_Session $session
* @return void
*/
final public function closeSession(Doctrine_Session $session) {
$session->close();
unset($session);
}
/**
* getSessions
* @return array
*/
final public function getSessions() {
return $this->sessions;
}
/**
* setCurrentSession
* @param mixed $key the session key
* @throws InvalidKeyException
* @return void
*/
final public function setCurrentSession($key) {
$key = (string) $key;
if( ! isset($this->sessions[$key]))
throw new InvalidKeyException();
$this->currIndex = $key;
}
/**
* count
* @return integer the number of open sessions
*/
public function count() {
return count($this->sessions);
}
/**
* getIterator
* @return ArrayIterator
*/
public function getIterator() {
return new ArrayIterator($this->sessions);
}
/**
* getCurrentSession
* @return object Doctrine_Session
*/
final public function getCurrentSession() {
$i = $this->currIndex;
if( ! isset($this->sessions[$i]))
throw new Doctrine_Session_Exception();
return $this->sessions[$i];
}
/**
* __toString
*/
public function __toString() {
$r[] = "<pre>";
$r[] = "Doctrine_Manager";
$r[] = "Sessions : ".count($this->sessions);
$r[] = "</pre>";
return implode("\n",$r);
}
}
?>
This diff is collapsed.
<?php
/**
* @class Doctrine_Relation
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*
*/
class Doctrine_Relation {
/**
* @var Doctrine_Table $table foreign factory
*/
private $table;
/**
* @var string $local local field
*/
private $local;
/**
* @var string $foreign foreign field
*/
private $foreign;
/**
* @var integer $type bind type
*/
private $type;
/**
* @param Doctrine_Table $table
* @param string $local
* @param string $foreign
* @param integer $type
*/
public function __construct(Doctrine_Table $table,$local,$foreign,$type) {
$this->table = $table;
$this->local = $local;
$this->foreign = $foreign;
$this->type = $type;
}
/**
* @see Doctrine_Table::BIND_ONE, Doctrine_Table::BIND_MANY
* @return integer bind type 1 or 0
*/
public function getType() {
return $this->type;
}
/**
* @return object Doctrine_Table foreign factory object
*/
public function getTable() {
return $this->table;
}
/**
* @return string the name of the local column
*/
public function getLocal() {
return $this->local;
}
/**
* @return string the name of the foreign column where
* the local column is pointing at
*/
public function getForeign() {
return $this->foreign;
}
/**
* __toString
*/
public function __toString() {
$r[] = "<pre>";
$r[] = "Class : ".get_class($this);
$r[] = "Component : ".$this->table->getComponentName();
$r[] = "Table : ".$this->table->getTableName();
$r[] = "Local key : ".$this->local;
$r[] = "Foreign key : ".$this->foreign;
$r[] = "Type : ".$this->type;
$r[] = "</pre>";
return implode("\n", $r);
}
}
?>
<?php
/**
* Doctrine_Repository
* each record is added into Doctrine_Repository at the same time they are created,
* loaded from the database or retrieved from the cache
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*
*/
class Doctrine_Repository implements Countable, IteratorAggregate {
/**
* @var object Doctrine_Table $table
*/
private $table;
/**
* @var array $registry
* an array of all records
* keys representing record object identifiers
*/
private $registry = array();
/**
* constructor
*/
public function __construct(Doctrine_Table $table) {
$this->table = $table;
}
/**
* @return object Doctrine_Table
*/
public function getTable() {
return $this->table;
}
/**
* add
* @param Doctrine_Record $record record to be added into registry
*/
public function add(Doctrine_Record $record) {
$oid = $record->getOID();
if(isset($this->registry[$oid]))
return false;
$this->registry[$oid] = $record;
return true;
}
/**
* get
* @param integer $oid
* @throws InvalidKeyException
*/
public function get($oid) {
if( ! isset($this->registry[$oid]))
throw new InvalidKeyException();
return $this->registry[$oid];
}
/**
* count
* Doctrine_Registry implements interface Countable
* @return integer the number of records this registry has
*/
public function count() {
return count($this->registry);
}
/**
* @param integer $oid object identifier
* @return boolean whether ot not the operation was successful
*/
public function evict($oid) {
if( ! isset($this->registry[$oid]))
return false;
unset($this->registry[$oid]);
return true;
}
/**
* @return integer number of records evicted
*/
public function evictAll() {
$evicted = 0;
foreach($this->registry as $oid=>$record) {
if($this->evict($oid))
$evicted++;
}
return $evicted;
}
/**
* getIterator
* @return ArrayIterator
*/
public function getIterator() {
return new ArrayIterator($this->registry);
}
/**
* contains
* @param integer $oid object identifier
*/
public function contains($oid) {
return isset($this->registry[$oid]);
}
/**
* loadAll
* @return void
*/
public function loadAll() {
$this->table->findAll();
}
}
?>
This diff is collapsed.
<?php
/**
* standard session, the parent of pgsql, mysql and sqlite
*/
class Doctrine_Session_Common extends Doctrine_Session {
public function modifyLimitQuery($query,$limit,$offset) {
return $query." LIMIT ".$limit." OFFSET ".$offset;
}
}
?>
<?php
/**
* firebird driver
*/
class Doctrine_Session_Firebird extends Doctrine_Session {
public function modifyLimitQuery($query,$limit,$offset) {
return preg_replace("/([\s(])*SELECT/i","\\1SELECT TOP($from, $count)", $query);
}
/**
* returns the next value in the given sequence
* @param string $sequence
* @return integer
*/
public function getNextID($sequence) {
$stmt = $this->query("SELECT UNIQUE FROM ".$sequence);
$data = $stmt->fetch(PDO::FETCH_NUM);
return $data[0];
}
}
?>
<?php
/**
* informix database driver
*/
class Doctrine_Session_Informix extends Doctrine_Session { }
?>
<?php
/**
* mssql driver
*/
class Doctrine_Session_Mssql extends Doctrine_Session {
/**
* returns the next value in the given sequence
* @param string $sequence
* @return integer
*/
public function getNextID($sequence) {
$this->query("INSERT INTO $sequence (vapor) VALUES (0)");
$stmt = $this->query("SELECT @@IDENTITY FROM $sequence");
$data = $stmt->fetch(PDO::FETCH_NUM);
return $data[0];
}
}
?>
<?php
/**
* mysql driver
*/
class Doctrine_Session_Mysql extends Doctrine_Session_Common {
/**
* deletes all data access object from the collection
* @param Doctrine_Collection $coll
*/
/**
public function deleteCollection(Doctrine_Collection $coll) {
$a = $coll->getTable()->getCompositePaths();
$a = array_merge(array($coll->getTable()->getComponentName()),$a);
$graph = new Doctrine_DQL_Parser($this);
foreach($coll as $k=>$record) {
switch($record->getState()):
case Doctrine_Record::STATE_DIRTY:
case Doctrine_Record::STATE_CLEAN:
$ids[] = $record->getID();
break;
endswitch;
}
if(empty($ids))
return array();
$graph->parseQuery("FROM ".implode(", ",$a)." WHERE ".$coll->getTable()->getTableName().".id IN(".implode(", ",$ids).")");
$query = $graph->buildDelete();
$this->getDBH()->query($query);
return $ids;
}
*/
}
?>
<?php
/**
* oracle driver
*/
class Doctrine_Session_Oracle extends Doctrine_Session {
public function modifyLimitQuery($query,$limit,$offset) {
$e = explode("select ",strtolower($query));
$e2 = explode(" from ",$e[1]);
$fields = $e2[0];
$query = "SELECT $fields FROM (SELECT rownum as linenum, $fields FROM ($query) WHERE rownum <= ($offset + $limit)) WHERE linenum >= ".++$offset;
return $query;
}
/**
* returns the next value in the given sequence
* @param string $sequence
* @return integer
*/
public function getNextID($sequence) {
$stmt = $this->query("SELECT $sequence.nextval FROM dual");
$data = $stmt->fetch(PDO::FETCH_NUM);
return $data[0];
}
}
?>
<?php
/**
* pgsql driver
*/
class Doctrine_Session_Pgsql extends Doctrine_Session_Common {
/**
* returns the next value in the given sequence
* @param string $sequence
* @return integer
*/
public function getNextID($sequence) {
$stmt = $this->query("SELECT NEXTVAL('$sequence')");
$data = $stmt->fetch(PDO::FETCH_NUM);
return $data[0];
}
}
?>
<?php
/**
* sqlite driver
*/
class Doctrine_Session_Sqlite extends Doctrine_Session_Common { }
?>
This diff is collapsed.
<?php
class Doctrine_Validator {
const ERR_LENGTH = 0;
const ERR_TYPE = 1;
const ERR_VALID = 2;
const ERR_UNIQUE = 3;
/**
* @var array $stack error stack
*/
private $stack = array();
/**
* @var array $validators
*/
private static $validators = array();
/**
* @param string $name
*/
public static function getValidator($name) {
if( ! isset(self::$validators[$name])) {
$class = "Doctrine_Validator_".ucwords(strtolower($name));
if(class_exists($class)) {
self::$validators[$name] = new $class;
} elseif(class_exists($name."Validator")) {
self::$validators[$name] = new $name."Validator";
} else
throw new Doctrine_Exception("Validator named '$name' not availible.");
}
return self::$validators[$name];
}
/**
* @param Doctrine_Record $record
* @return void
*/
public function validateRecord(Doctrine_Record $record) {
$modified = $record->getModified();
$columns = $record->getTable()->getColumns();
$name = $record->getTable()->getComponentName();
$err = array();
foreach($modified as $key => $value) {
$column = $columns[$key];
if(strlen($value) > $column[1]) {
$err[$key] = Doctrine_Validator::ERR_LENGTH;
continue;
}
if(self::gettype($value) !== $column[0]) {
$err[$key] = Doctrine_Validator::ERR_TYPE;
continue;
}
$e = explode("|",$column[2]);
foreach($e as $k => $arg) {
if(empty($arg) || $arg == "primary")
continue;
$validator = self::getValidator($arg);
if( ! $validator->validate($record,$key,$value)) {
switch(strtolower($arg)):
case "unique":
$err[$key] = Doctrine_Validator::ERR_UNIQUE;
break;
default:
$err[$key] = Doctrine_Validator::ERR_VALID;
break;
endswitch;
}
if(isset($err[$key]))
break;
}
}
if( ! empty($err)) {
$this->stack[$name][] = $err;
return false;
}
return true;
}
/**
* @return boolean
*/
public function hasErrors() {
return (count($this->stack) > 0);
}
/**
* @return array
*/
public function getErrorStack() {
return $this->stack;
}
/**
* @param mixed $var
*/
public static function gettype($var) {
$type = gettype($var);
switch($type):
case "string":
if(preg_match("/^[0-9]*$/",$var)) return "integer";
elseif(is_numeric($var)) return "float";
else return $type;
break;
default:
return $type;
endswitch;
}
}
?>
<?php
class Doctrine_Validator_Blank {
/**
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
$string = str_replace("\n","",$value);
$string = str_replace("\r","",$string);
$string = str_replace("\t","",$string);
$string = str_replace("\s","",$string);
$string = str_replace(" ","",$string);
if($string == "") return true;
}
}
?>
<?php
class Doctrine_Validator_Country {
private static $countries = array(
"ad" => "Andorra",
"ae" => "United Arab Emirates",
"af" => "Afghanistan",
"ag" => "Antigua and Barbuda",
"ai" => "Anguilla",
"al" => "Albania",
"am" => "Armenia",
"an" => "Netherlands Antilles",
"ao" => "Angola",
"aq" => "Antarctica",
"ar" => "Argentina",
"as" => "American Samoa",
"at" => "Austria",
"au" => "Australia",
"aw" => "Aruba",
"az" => "Azerbaijan",
"ba" => "Bosnia Hercegovina",
"bb" => "Barbados",
"bd" => "Bangladesh",
"be" => "Belgium",
"bf" => "Burkina Faso",
"bg" => "Bulgaria",
"bh" => "Bahrain",
"bi" => "Burundi",
"bj" => "Benin",
"bm" => "Bermuda",
"bn" => "Brunei Darussalam",
"bo" => "Bolivia",
"br" => "Brazil",
"bs" => "Bahamas",
"bt" => "Bhutan",
"bv" => "Bouvet Island",
"bw" => "Botswana",
"by" => "Belarus (Byelorussia)",
"bz" => "Belize",
"ca" => "Canada",
"cc" => "Cocos Islands",
"cd" => 'Congo, The Democratic Republic of the',
"cf" => "Central African Republic",
"cg" => "Congo",
"ch" => "Switzerland",
"ci" => "Ivory Coast",
"ck" => "Cook Islands",
"cl" => "Chile",
"cm" => "Cameroon",
"cn" => "China",
"co" => "Colombia",
"cr" => "Costa Rica",
"cs" => "Czechoslovakia",
"cu" => "Cuba",
"cv" => "Cape Verde",
"cx" => "Christmas Island",
"cy" => "Cyprus",
"cz" => 'Czech Republic',
"de" => "Germany",
"dj" => "Djibouti",
"dk" => 'Denmark',
"dm" => "Dominica",
"do" => "Dominican Republic",
"dz" => "Algeria",
"ec" => "Ecuador",
"ee" => "Estonia",
"eg" => "Egypt",
"eh" => "Western Sahara",
"er" => 'Eritrea',
"es" => "Spain",
"et" => "Ethiopia",
"fi" => "Finland",
"fj" => "Fiji",
"fk" => "Falkland Islands",
"fm" => "Micronesia",
"fo" => "Faroe Islands",
"fr" => "France",
"fx" => 'France, Metropolitan FX',
"ga" => "Gabon",
"gb" => 'United Kingdom (Great Britain)',
"gd" => "Grenada",
"ge" => "Georgia",
"gf" => "French Guiana",
"gh" => "Ghana",
"gi" => "Gibraltar",
"gl" => "Greenland",
"gm" => "Gambia",
"gn" => "Guinea",
"gp" => "Guadeloupe",
"gq" => "Equatorial Guinea",
"gr" => "Greece",
"gs" => 'South Georgia and the South Sandwich Islands',
"gt" => "Guatemala",
"gu" => "Guam",
"gw" => "Guinea-bissau",
"gy" => "Guyana",
"hk" => "Hong Kong",
"hm" => "Heard and McDonald Islands",
"hn" => "Honduras",
"hr" => "Croatia",
"ht" => "Haiti",
"hu" => "Hungary",
"id" => "Indonesia",
"ie" => "Ireland",
"il" => "Israel",
"in" => "India",
"io" => "British Indian Ocean Territory",
"iq" => "Iraq",
"ir" => "Iran",
"is" => "Iceland",
"it" => "Italy",
"jm" => "Jamaica",
"jo" => "Jordan",
"jp" => "Japan",
"ke" => "Kenya",
"kg" => "Kyrgyzstan",
"kh" => "Cambodia",
"ki" => "Kiribati",
"km" => "Comoros",
"kn" => "Saint Kitts and Nevis",
"kp" => "North Korea",
"kr" => "South Korea",
"kw" => "Kuwait",
"ky" => "Cayman Islands",
"kz" => "Kazakhstan",
"la" => "Laos",
"lb" => "Lebanon",
"lc" => "Saint Lucia",
"li" => "Lichtenstein",
"lk" => "Sri Lanka",
"lr" => "Liberia",
"ls" => "Lesotho",
"lt" => "Lithuania",
"lu" => "Luxembourg",
"lv" => "Latvia",
"ly" => "Libya",
"ma" => "Morocco",
"mc" => "Monaco",
"md" => "Moldova Republic",
"mg" => "Madagascar",
"mh" => "Marshall Islands",
"mk" => 'Macedonia, The Former Yugoslav Republic of',
"ml" => "Mali",
"mm" => "Myanmar",
"mn" => "Mongolia",
"mo" => "Macau",
"mp" => "Northern Mariana Islands",
"mq" => "Martinique",
"mr" => "Mauritania",
"ms" => "Montserrat",
"mt" => "Malta",
"mu" => "Mauritius",
"mv" => "Maldives",
"mw" => "Malawi",
"mx" => "Mexico",
"my" => "Malaysia",
"mz" => "Mozambique",
"na" => "Namibia",
"nc" => "New Caledonia",
"ne" => "Niger",
"nf" => "Norfolk Island",
"ng" => "Nigeria",
"ni" => "Nicaragua",
"nl" => "Netherlands",
"no" => "Norway",
"np" => "Nepal",
"nr" => "Nauru",
"nt" => "Neutral Zone",
"nu" => "Niue",
"nz" => "New Zealand",
"om" => "Oman",
"pa" => "Panama",
"pe" => "Peru",
"pf" => "French Polynesia",
"pg" => "Papua New Guinea",
"ph" => "Philippines",
"pk" => "Pakistan",
"pl" => "Poland",
"pm" => "St. Pierre and Miquelon",
"pn" => "Pitcairn",
"pr" => "Puerto Rico",
"pt" => "Portugal",
"pw" => "Palau",
"py" => "Paraguay",
"qa" => 'Qatar',
"re" => "Reunion",
"ro" => "Romania",
"ru" => "Russia",
"rw" => "Rwanda",
"sa" => "Saudi Arabia",
"sb" => "Solomon Islands",
"sc" => "Seychelles",
"sd" => "Sudan",
"se" => "Sweden",
"sg" => "Singapore",
"sh" => "St. Helena",
"si" => "Slovenia",
"sj" => "Svalbard and Jan Mayen Islands",
"sk" => 'Slovakia (Slovak Republic)',
"sl" => "Sierra Leone",
"sm" => "San Marino",
"sn" => "Senegal",
"so" => "Somalia",
"sr" => "Suriname",
"st" => "Sao Tome and Principe",
"sv" => "El Salvador",
"sy" => "Syria",
"sz" => "Swaziland",
"tc" => "Turks and Caicos Islands",
"td" => "Chad",
"tf" => "French Southern Territories",
"tg" => "Togo",
"th" => "Thailand",
"tj" => "Tajikistan",
"tk" => "Tokelau",
"tm" => "Turkmenistan",
"tn" => "Tunisia",
"to" => "Tonga",
"tp" => "East Timor",
"tr" => "Turkey",
"tt" => "Trinidad, Tobago",
"tv" => "Tuvalu",
"tw" => "Taiwan",
"tz" => "Tanzania",
"ua" => "Ukraine",
"ug" => "Uganda",
"uk" => "United Kingdom",
"um" => "United States Minor Islands",
"us" => "United States of America",
"uy" => "Uruguay",
"uz" => "Uzbekistan",
"va" => "Vatican City",
"vc" => "Saint Vincent, Grenadines",
"ve" => "Venezuela",
"vg" => "Virgin Islands (British)",
"vi" => "Virgin Islands (USA)",
"vn" => "Viet Nam",
"vu" => "Vanuatu",
"wf" => 'Wallis and Futuna Islands',
"ws" => "Samoa",
"ye" => 'Yemen',
"yt" => 'Mayotte',
"yu" => "Yugoslavia",
"za" => "South Africa",
"zm" => "Zambia",
"zr" => "Zaire",
"zw" => "Zimbabwe");
/**
* @return array
*/
public static function getCountries() {
return self::$countries;
}
/**
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
return isset(self::$countries[$value]);
}
}
?>
<?php
class Doctrine_Validator_Date {
/**
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
return checkdate($value);
}
}
?>
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
<?php
class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase {
public function testAdd() {
}
}
?>
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment