Commit b41259b0 authored by Jonathan.Wage's avatar Jonathan.Wage

--no commit message

--no commit message
parent 340dbf77
......@@ -431,6 +431,95 @@ final class Doctrine
Doctrine::autoload($class);
}
}
/**
* loadModels
*
* Recursively load all models from a directory or array of directories
*
* @param string $directory Path to directory of models or array of directory paths
* @return void
* @author Jonathan H. Wage
*/
public static function loadModels($directory)
{
$declared = get_declared_classes();
if ($directory !== null) {
foreach ((array) $directory as $dir) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($it as $file) {
$e = explode('.', $file->getFileName());
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
require_once $file->getPathName();
}
}
}
$declared = array_diff(get_declared_classes(), $declared);
}
return self::getLoadedModels($declared);
}
/**
* getLoadedModels
*
* @package default
* @author Jonathan H. Wage
*/
public static function getLoadedModels($classes = null)
{
if ($classes === null) {
$classes = get_declared_classes();
}
$parent = new ReflectionClass('Doctrine_Record');
$loadedModels = array();
// we iterate trhough the diff of previously declared classes
// and currently declared classes
foreach ($classes as $name) {
$class = new ReflectionClass($name);
$conn = Doctrine_Manager::getInstance()->getConnectionForComponent($name);
// check if class is an instance of Doctrine_Record and not abstract
// class must have method setTableDefinition (to avoid non-Record subclasses like symfony's sfDoctrineRecord)
// we have to recursively iterate through the class parents just to be sure that the classes using for example
// column aggregation inheritance are properly exported to database
while ($class->isAbstract() ||
! $class->isSubclassOf($parent) ||
! $class->hasMethod('setTableDefinition') ||
( $class->hasMethod('setTableDefinition') &&
$class->getMethod('setTableDefinition')->getDeclaringClass()->getName() !== $class->getName())) {
$class = $class->getParentClass();
if ($class === false) {
break;
}
}
if ($class === false) {
continue;
}
$loadedModels[] = $name;
}
return $loadedModels;
}
public function getConnectionByTableName($tableName)
{
$loadedModels = Doctrine::getLoadedModels();
foreach ($loadedModels AS $name) {
$model = new $model();
$table = $model->getTable();
if ($table->getTableName() == $tableName) {
return $table->getConnection();
}
}
}
/**
* importSchema
* method for importing existing schema to Doctrine_Record classes
......@@ -568,4 +657,4 @@ final class Doctrine
return true;
}
}
}
\ No newline at end of file
......@@ -194,8 +194,8 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
if ($record->hasReference($alias)) {
$obj = $record->$alias;
// check that the related object is not Doctrine_Null
if ($obj instanceof Doctrine_Record) {
// check that the related object is either an instance of Doctrine_Record or an instance of Doctrine_Collection
if ($obj instanceof Doctrine_Record || $obj instanceof Doctrine_Collection) {
$obj->save($conn);
}
}
......
......@@ -45,15 +45,12 @@ class Doctrine_Migration
static public function migration($directory, $from, $to)
{
if ($from === $to OR $from === 0) {
if ($from === $to || $from === 0) {
throw new Doctrine_Migration_Exception('You specified an invalid migration path. The from and to cannot be the same and from cannot be zero.');
}
$direction = $from > $to ? 'down':'up';
$fromPath = $directory.DIRECTORY_SEPARATOR;
$toPath = $directory.DIRECTORY_SEPARATOR;
if ($direction === 'up') {
for ($i = $from + 1; $i <= $to; $i++) {
self::doDirectionStep($directory, $direction, $i);
......@@ -67,9 +64,9 @@ class Doctrine_Migration
public static function doDirectionStep($directory, $direction, $num)
{
$className = 'Migration'.$num;
$fileName = $className.'.class.php';
$filePath = $directory.DIRECTORY_SEPARATOR.$fileName;
$className = 'Migration' . $num;
$fileName = $className . '.class.php';
$filePath = $directory . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($filePath)) {
require_once($filePath);
......@@ -94,7 +91,7 @@ class Doctrine_Migration
{
foreach ($this->changes as $type => $changes) {
$process = new Doctrine_Migration_Process();
$funcName = 'process'.Doctrine::classify($type);
$funcName = 'process' . Doctrine::classify($type);
$process->$funcName($changes);
}
}
......@@ -125,7 +122,7 @@ class Doctrine_Migration
$this->addChange('renamed_tables', $options);
}
public function addColumn($tableName, $columnName, $type, $options = array())
public function addColumn($tableName, $columnName, $type, array $options = array())
{
$options = get_defined_vars();
......
......@@ -31,38 +31,43 @@
*/
class Doctrine_Migration_Process
{
public function processCreatedTables($tables)
public function getConnection($tableName)
{
$conn = Doctrine_Manager::connection();
return Doctrine::getConnectionByTableName($tableName);
}
public function processCreatedTables($tables)
{
foreach ($tables as $table) {
$conn = $this->getConnection($table['tableName']);
$conn->export->createTable($table['tableName'], $table['fields'], $table['options']);
}
}
public function processDroppedTables($tables)
{
$conn = Doctrine_Manager::connection();
foreach ($tables as $table) {
$conn = $this->getConnection($table['tableName']);
$conn->export->dropTable($table['tableName']);
}
}
public function processRenamedTables($tables)
{
$conn = Doctrine_Manager::connection();
foreach ($tables as $table) {
$conn = $this->getConnection($table['newTableName']);
$conn->export->alterTable($table['oldTableName'], array('name' => $table['newTableName']), true);
}
}
public function processAddedColumns($columns)
{
$conn = Doctrine_Manager::connection();
foreach ($columns as $column) {
$conn = $this->getConnection($column['tableName']);
$options = array();
$options = $column['options'];
$options['type'] = $column['type'];
......@@ -73,18 +78,18 @@ class Doctrine_Migration_Process
public function processRenamedColumns($columns)
{
$conn = Doctrine_Manager::connection();
foreach ($columns as $column) {
$conn = $this->getConnection($column['tableName']);
$conn->export->alterTable($column['tableName'], array('rename' => array($column['oldColumnName'] => array('name' => $column['newColumnName']))), true);
}
}
public function processChangedColumns($columns)
{
$conn = Doctrine_Manager::connection();
foreach ($columns as $column) {
$conn = $this->getConnection($column['tableName']);
$options = array();
$options = $column['options'];
$options['type'] = $column['type'];
......@@ -95,27 +100,27 @@ class Doctrine_Migration_Process
public function processRemovedColumns($columns)
{
$conn = Doctrine_Manager::connection();
foreach ($columns as $column) {
$conn = $this->getConnection($column['tableName']);
$conn->export->alterTable($column['tableName'], array('remove' => array($column['columnName'] => array())));
}
}
public function processAddedIndexes($indexes)
{
$conn = Doctrine_Manager::connection();
foreach ($indexes as $index) {
$conn = $this->getConnection($index['tableName']);
$conn->export->createIndex($index['tableName'], $index['indexName'], $index['definition']);
}
}
public function processRemovedIndexes($indexes)
{
$conn = Doctrine_Manager::connection();
foreach ($indexes as $index) {
$conn = $this->getConnection($index['tableName']);
$conn->export->dropIndex($index['tableName'], $index['indexName']);
}
}
......
<?php
define('LOAD_MODELS', false);
require_once('playground.php');
require_once('connection.php');
require_once('models.php');
require_once('playground.php');
\ No newline at end of file
Doctrine_Migration::migration('migration', 1, 3);
Doctrine_Migration::migration('migration', 3, 1);
<?php
ini_set('max_execution_time', 900);
// include doctrine, and register it's autoloader
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'doctrine/Doctrine.php';
spl_autoload_register(array('Doctrine', 'autoload'));
if (constant('LOAD_MODELS')) {
$modelsPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'models';
// include the models
$models = new DirectoryIterator($modelsPath);
foreach($models as $key => $file) {
if ($file->isFile() && ! $file->isDot()) {
$e = explode('.', $file->getFileName());
if (end($e) === 'php') {
require_once $file->getPathname();
}
}
}
}
error_reporting(E_ALL | E_STRICT);
$dbh = new PDO('sqlite::memory:');
$conn = Doctrine_Manager::connection($dbh);
$manager = Doctrine_Manager::getInstance();
if (constant('LOAD_MODELS')) {
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
$tables = array('entity',
'entityReference',
'email',
'phonenumber',
'groupuser',
'album',
'song',
'element',
'error',
'description',
'address',
'account',
'task',
'resource',
'assignment',
'resourceType',
'resourceReference');
$conn->export->exportClasses($tables);
require_once('data.php');
}
\ No newline at end of file
error_reporting(E_ALL | E_STRICT);
\ No newline at end of file
......@@ -337,6 +337,8 @@ $test->addTestCase($cache);
$test->addTestCase(new Doctrine_Query_ApplyInheritance_TestCase());
$test->addTestCase(new Doctrine_Migration_TestCase());
class CliReporter extends HtmlReporter{
public function paintHeader(){
echo "Doctrine UnitTests\n";
......
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