Commit 2f66d604 authored by Jonathan.Wage's avatar Jonathan.Wage

Initial entry of sfDoctrinePlugin.

parent 9fbde039
Copyright (c) 2004-2006 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sfDoctrine symfony plugin
=========================
Overview
--------
The sfDoctrine plugin allows you to totally replace propel with doctrine (http://www.phpdoctrine.com/) which is a powerful and easy-to-use ORM.
Contents
--------
This plugin contains:
- necessary tools to use doctrine: the main tool is sfDoctrine::getTable() which will allow you to send queries
- pake tasks that convert between doctrine and propel schema formats and build doctrine model classes automatically
- an admin generator: to use it just specify sfDoctrineAdmin as the main class in your generator.yml config
You will find more information in the wiki page dedicated to sfDoctrine: http://www.symfony-project.com/trac/wiki/sfDoctrine.
License
-------
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineClassSchema.class.php 4696 2007-07-20 17:04:44Z gnat $
*/
class sfDoctrineClassSchema
{
// the table associated to this class
protected $table;
// class name
protected $phpName;
// list of columns
protected $columns = array();
// list of relations (foreign keys) linking to this class
protected $many = array();
// inheritance description
protected $inheritance = array();
// i18n description
protected $i18n = array();
// indexes
protected $indexes = array();
// Uniques
protected $uniques = array();
// options
protected $options = array();
public function __construct($name, array $cd = array())
{
$this->setPhpName($name);
// elementary key verification
$illegalKeys = array_diff_key($cd, array_flip(array('columns', 'tableName', 'inheritance', 'i18n', 'indexes', 'uniques', 'options')));
if ($illegalKeys)
throw new sfDoctrineSchemaException(sprintf('Invalid key "%s" in description of class "%s"', array_shift(array_keys($illegalKeys)), $name));
if (isset($cd['inheritance']))
{
$this->setInheritance($cd['inheritance']);
}
// set i18n
if (isset($cd['i18n']))
$this->setI18n($cd['i18n']);
// add indexes
if (isset($cd['indexes']))
$this->addIndexes($cd['indexes']);
// add uniques
if (isset($cd['uniques']))
$this->addUniques($cd['uniques']);
// add options
if (isset($cd['options']))
$this->addOptions($cd['options']);
// add columns
if (isset($cd['columns']))
foreach ($cd['columns'] as $colName => $column)
{
$docCol = new sfDoctrineColumnSchema($colName, $column);
$this->addColumn($docCol);
}
}
// add a column if none with the same name is already there
public function addColumn($docCol)
{
if (isset($this->columns[$docCol->getName()]))
return;
// sets up the possible relation for that column
$docCol->setUpForeignRelation($this->getPhpName());
$this->columns[$docCol->getName()] = $docCol;
}
public function getColumns()
{
return $this->columns;
}
// for testing only
public function getColumn($colName)
{
if (!isset($this->columns[$colName]))
throw new sfDoctrineSchemaException(sprintf('Column "%s" is not defined', $colName));
return $this->columns[$colName];
}
public function addToMany($relation)
{
$this->many[] = $relation;
}
public function getPhpName()
{
return $this->phpName;
}
public function setPhpName($newName)
{
$this->phpName = $newName;
}
public function setTable($table)
{
$this->table = $table;
}
public function getTable()
{
if (!$this->hasTable())
throw new sfDoctrineSchemaException(sprintf('Table not defined for class "%s"', $this->getPhpName()));
return $this->table;
}
public function getTableName()
{
return $this->getTable()->getName();
}
public function hasTable()
{
return isset($this->table);
}
public function setI18n($i18n)
{
$check = array('class', 'cultureField');
foreach ($check as $key)
if (!isset($i18n[$key]))
throw new sfDoctrineSchemaException(sprintf('The key "%s" is missing from the i18n information for class "%s".', $key, $this->getPhpName()));
$this->i18n = $i18n;
}
public function hasI18n()
{
return !empty($this->i18n);
}
public function getI18n($key)
{
return $this->i18n[$key];
}
public function setInheritance($inh)
{
$check = array('extends');
if (isset($inh['keyField']) || isset($inh['keyValue']))
$check = array_merge($check, array('keyField', 'keyValue'));
elseif (isset($inh['keyFields']))
$check = array_merge($check, array('keyFields'));
foreach ($check as $key)
if (!isset($inh[$key]))
throw new sfDoctrineSchemaException(sprintf('The key "%s" is missing from the inheritance information for class "%s".', $key, $this->getPhpName()));
$this->inheritance = $inh;
}
public function getInheritance()
{
return $this->inheritance;
}
public function hasOneTableInheritance()
{
if ($inh = $this->inheritance)
if (isset($inh['keyValue']) || isset($inh['keyFields']))
return true;
return false;
}
public function getOptions()
{
return $this->options;
}
public function addOptions($options)
{
$this->options = $options;
}
public function hasOptions()
{
return count($this->options) ? true : false;
}
public function getIndexes()
{
return $this->indexes;
}
public function addIndexes($indexes)
{
$this->indexes = $indexes;
}
public function hasIndexes()
{
return count($this->indexes) ? true : false;
}
public function addUniques($uniques)
{
$this->uniques = $uniques;
}
public function hasUniques()
{
return count($this->uniques) ? true : false;
}
public function getParentClassName()
{
return $this->inheritance['extends'];
}
// generates the name of the generated class
public function basePhpName($name = null)
{
if (!$name)
$name = $this->getPhpName();
return 'Base'.$name;
}
// outputs a function in php
public static function outputFunction($functionName, $contents, $phpdoc = '')
{
if (is_array($contents))
$contents = implode("\n ", $contents);
return "
$phpdoc
public function $functionName()
{
$contents
}
";
}
// output a class in php
public static function outputClass($className, $extends, $contents, $phpdoc = '')
{
$signature = sprintf("auto-generated by the sfDoctrine plugin");
return "<?php
/*
* $phpdoc
*
* $signature
*/
class $className extends $extends
{
$contents
}
";
}
public function getRelation($columnName)
{
return $this->columns[$columnName]->getRelation();
}
// this function returns an array ('className'=><class name>, 'source'=><class file contents>, 'base'=>true/false) of PHP classes
// corresponding to this class
public function asPHP()
{
$classes = array();
// main base class
$out = array();
$tableDef = array();
$setup = array();
// if that class inherits from another we call the parent methods
if ($this->inheritance)
{
$tableDef[] = "parent::setTableDefinition();\n";
$setup[] = "parent::setUp();\n";
}
// if it is a table we define the table name
if ($this->hasTable())
{
$tableDef[] = "\$this->setTableName('{$this->getTableName()}');\n";
}
foreach ($this->columns as $column)
{
$args = array();
$tableDef[] = $column->asPhp();
}
// declare indexes if any
foreach ($this->indexes as $name => $value)
{
// only write option if value is set
if(!empty($value))
{
$valueExport = is_array($value) ? var_export($value, true) : "'$value'";
$tableDef[] = "\$this->index('$name', $valueExport);";
}
}
// declare uniques if any
foreach ($this->uniques as $name => $value)
{
// only write option if value is set
if(!empty($value))
{
$valueExport = is_array($value) ? var_export($value, true) : "'$value'";
$tableDef[] = "\$this->unique('$name', $valueExport);";
}
}
foreach ($this->options as $name => $value)
{
// only write option if value is set
if(!empty($value))
{
$valueExport = is_array($value) ? var_export($value, true) : "'$value'";
$tableDef[] = "\$this->option('$name', $valueExport);";
}
}
$out[] = self::outputFunction('setTableDefinition', $tableDef);
// has/own one
foreach($this->columns as $col)
if ($rel = $col->getRelation())
{
$setup[] = $rel->asOnePhp();
}
// has/own many
foreach($this->many as $rel)
{
$setup[] = $rel->asManyPhp();
}
// declare inheritance if needed
if ($this->hasOneTableInheritance())
{
$inh = $this->getInheritance();
if (isset($inh['keyFields']))
{
$keyFields = $inh['keyFields'];
$keyFields = is_array($keyFields) ? $keyFields : array($keyFields);
}
else
$keyFields = array($inh['keyField'] => $inh['keyValue']);
$setup[] = '$this->setInheritanceMap('.var_export($keyFields, true).');';
}
// declare i18n if any
if ($this->hasI18n())
$setup[] = "\$this->hasI18nTable('{$this->getI18n('class')}', '{$this->getI18n('cultureField')}');";
$out[] = self::outputFunction('setUp', $setup);
// the following could also be: if ($this->inheritance)
// FIXME: create a global class!
if (isset($this->inheritance['extends']))
$parentName = $this->inheritance['extends'];
else
$parentName = ($this->hasI18n() ? 'sfDoctrineRecordI18n' : 'sfDoctrineRecord');
$class = array
(
'name' => $this->getPhpName(), // name of the child class; used only internally
'className' => $this->basePHPName(),
'source' => self::outputClass($this->basePHPName(), $parentName, implode("\n", $out), 'Base class; DO NOT EDIT'),
'overwrite' => true, // carful! even this set to false will overwrite!!!
);
$classes[] = $class;
$package = $this->getTable()->getPackage();
// generate the empty user and table classes
foreach ($classes as $baseClass)
{
$name = $baseClass['name'];
$parentClass = $baseClass['className'];
$tableName = $name.'Table'; // convention imposed by Doctrine
if (isset($this->inheritance['extends']))
$parentTable = $this->inheritance['extends'].'Table';
else
$parentTable = 'Doctrine_Table';
if ($package)
{
$pluginClassName = 'Plugin'.$name;
$classes[] = array
(
'className'=> $pluginClassName,
'source' => self::outputClass($pluginClassName, $parentClass, '', 'Plugin class'),
'plugin' => true,
);
// we hook the plugin class name in
$parentClass = $pluginClassName;
// same for tables
$pluginTableName = 'Plugin'.$tableName;
$classes[] = array
(
'className' => $pluginTableName,
'source' => self::outputClass($pluginTableName, $parentTable, '', 'Plugin table'),
'plugin' => true,
);
$parentTable = $pluginTableName;
}
$classes[] = array
(
'className'=>$name,
'source'=>self::outputClass($name, $parentClass, '', 'Edit this file to customise your model class'),
);
$classes[] = array
(
'className'=>$tableName,
'source'=>self::outputClass($tableName, $parentTable, '', 'Edit this file to customise your model table'),
);
}
return $classes;
}
// outputs a nested array
public function asDoctrineYml()
{
$output = array();
if ($this->inheritance)
$output['inheritance'] = $this->inheritance;
else
$output['tableName'] = $this->getTableName();
$cols = array();
foreach ($this->columns as $col)
{
$cols[$col->getName()] = $col->asDoctrineYml();
}
$output['columns'] = $cols;
return $output;
}
// outputs the columns of that class in propel xml format
public function addPropelXmlColumns(&$table)
{
// we add the id column which is automatically created in doctrine
$this->addColumn(new sfDoctrineColumnSchema('id', array('type'=>'integer', 'size'=>10, 'primary'=>true, 'autoincrement'=>true)));
foreach($this->columns as $col)
{
$col->addPropelXml($table);
}
}
public function debug()
{
$debug = array();
$debug['inheritance'] = $this->inheritance;
$debug['many'] = $this->many;
$debug['i18n'] = $this->i18n;
foreach ($this->columns as $col)
{
$debug['columns'][$col->getName()] = $col->debug();
}
return $debug;
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineColumnSchema.class.php 4084 2007-05-23 09:48:50Z chtito $
*/
/*
This class stores information about a column in two arrays:
- properties: contains the name, type, size and constraints
- columnInfo: contains also the foreign relation information
*/
class sfDoctrineColumnSchema
{
protected static $propel2docDictionary = array(
'types'=> array(
'tinyint' => 'integer',
'smallint' => 'integer',
'bigint' => 'integer',
'real' => 'float',
'decimal' => 'float',
'char' => 'string',
'varchar' => 'string',
'longvarchar' => 'string',
# 'smallint'=> 'enum', // enums are converted to smallints
# 'blob' => 'array', // arrays will be blobs
# 'integer' => 'integer', // to convert doc integer to integer
/*
'double' => 'double',
'float' => 'float',
'boolean' => 'boolean',
'date' => 'date',
'time' => 'timestamp',
'timestamp' => 'timestamp',
'blob' => 'blob',
'clob' => 'clob'
*/
),
'constraints' => array('autoIncrement' => 'autoincrement', 'primaryKey' => 'primary')
);
//FIXME: double, float,real???
protected static $defaultPropelSize = array(
'tinyint' => 3,
'smallint' => 5,
'integer' => 11,
'bigint' => 20,
'longvarchar'=>4000,
);
protected static $defaultDoctrineSize = array(
'string'=> 4000,
'integer' => 10,
'double' => 10,
'float' => 10,
'enum' => 2,
'array' => 100,
);
static $allowedConstraints = array('primary', 'autoincrement', 'default', 'enum', 'unique', 'nospace', 'notblank', 'notnull', 'email', 'scale', 'zerofill');
// column properties: name, size, type and constraints
protected $properties;
// column name
protected $name;
// temporary storage of the description array; used when the class sets up the relation
protected $columnInfo;
// set if the column is a foreign key
protected $relation = null;
// we essentially set up the properties array
// and translate from propel if needed
public function __construct($colName, $columnDescription = array(), $translatePropel = false)
{
// sometimes we get null if the yml line is empty
if ($columnDescription == null)
$columnDescription = array();
// for the short syntax type(size)
if (is_string($columnDescription))
$columnDescription = array('type'=>$columnDescription);
$this->setName($colName);
$columnInfo = new sfParameterHolder();
$columnInfo->add($columnDescription);
if ($translatePropel)
{
// we translate the propel types to doctrine ones
$propelType = strtolower($columnInfo->get('type'));
if (array_key_exists($propelType, self::$propel2docDictionary['types']))
$columnInfo->set('type', self::$propel2docDictionary['types'][$propelType]);
else
$columnInfo->set('type', $propelType); // we store it in lowercase
// if there is a default propel size we set it
if (!$columnInfo->get('size'))
if (isset(self::$defaultPropelSize[$propelType]))
$columnInfo->set('size', self::$defaultPropelSize[$propelType]);
// we translate the constraints
foreach ($columnInfo->getAll() as $key=>$value)
{
if (array_key_exists($key, self::$propel2docDictionary['constraints']))
$columnInfo->set(self::$propel2docDictionary['constraints'][$key], $columnInfo->get($key));
}
}
// we store the raw description, only used in setUpForeignRelation
$this->columnInfo = $columnInfo;
// name
$this->setProperty('name', $colName);
$this->setProperty('columnName', $columnInfo->get('columnName'));
// type
if (!($type = $columnInfo->get('type')))
{
// we try to figure out the type
// FIXME: write a method to detect relations?
if ($columnInfo->get('foreignClass') || $columnInfo->get('foreignTable'))
$type = 'integer'; // foreign key
else
$type = 'string'; // default type
}
elseif(is_string($type)) // we check for the short syntax type
{
preg_match('/([^\(\s]+)\s*\([\s]*([\d]+)[\s]*\)/', $type, $matches);
if (!empty($matches))
{
$type = $matches[1];
$columnInfo->set('size', $matches[2]);
}
}
$this->setProperty('type', $type);
// size
if (!($size = $columnInfo->get('size')))
{
if (is_string($type))
{
if (isset(self::$defaultDoctrineSize[$type]))
$size = self::$defaultDoctrineSize[$type]; // we have a default size for this type
}
}
if (!$size)
$size = 'null';
$this->setProperty('size', $size);
// constraints
if ($constraints = array_intersect_key($columnDescription, array_flip(self::$allowedConstraints)))
$this->properties = array_merge($this->properties, $constraints);
}
// FIXME: simplify this function
public function setUpForeignRelation($className)
{
$colInfo = $this->getColumnInfo();
$colName = $this->getName();
// If there is no relation info for this column
if (!$colInfo->has('foreignTable') && !$colInfo->has('foreignClass'))
return;
$foreignClass = $colInfo->get('foreignClass');
// if the localName (plural name) is not specified, we add an "s"
// as propel does
$localName = $colInfo->get('localName', $className.'s');
$foreignTable = $colInfo->get('foreignTable');
$foreignName = $colInfo->get('foreignName', null);
$fr = $colInfo->get('foreignReference', 'id');
$counterpart = $colInfo->get('counterpart');
$relationInfo = array
(
'localReference'=>$colName,
'foreignReference'=>$fr,
'localName'=>$localName,
'foreignName'=>$foreignName,
'counterpart' => $counterpart,
'foreignClass'=>$foreignClass,
'foreignTable'=>$foreignTable, // used only for propel import
'localClass'=>$className,
'options'=>$colInfo, // the remaining relation options
);
$this->relation = new sfDoctrineRelationSchema($relationInfo);
}
public function getColumnInfo()
{
return $this->columnInfo;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function getRelation()
{
return $this->relation;
}
public function hasRelation()
{
return isset($this->relation);
}
public function setProperty($name, $value)
{
$this->properties[$name] = $value;
}
public function getProperty($name)
{
return $this->properties[$name];
}
public function getProperties()
{
return $this->properties;
}
protected function niceVarExport($array)
{
return str_replace(array("\n"), array(''), var_export($array, 1));
}
static protected $doctrineArgs = array('name' => false, 'type' => true, 'size' => false);
// generates the doctrine description of a column in PHP
public function asPhp()
{
$props = $this->getProperties();
$args = array();
// take care of the enum type
// FIXME: remove this "trick" some day?
if (is_array($props['type']))
{
$props['values'] = $props['type'];
$props['type'] = 'enum';
}
$output = array();
foreach (self::$doctrineArgs as $argName => $isString)
{
$arg = $props[$argName];
unset($props[$argName]);
if ($isString)
$arg = sprintf("'%s'", $arg);
$args[] = $arg;
}
$columnAlias = '';
if ($props['columnName'])
{
$columnAlias = $props['columnName'] . ' as ';
}
unset($props['columnName']);
$args[0] = sprintf("'%s%s'", $columnAlias, $args[0]);
// what remains is considered to be constraints
$args[] = $this->niceVarExport($props);
$output[] = sprintf('$this->hasColumn(%s);', implode(', ', $args));
return implode("\n", $output);
}
// exports this column in propel xml format
public function addPropelXml(&$node)
{
$c = $node->addChild('column');
$doc2proplDict = array_flip(self::$propel2docDictionary['types']);
$c->addAttribute('name', $this->getName());
// type
$type = $this->properties['type'];
if (array_key_exists($this->properties['type'], $doc2proplDict))
$type = $doc2proplDict[$type];
$c->addAttribute('type', $type);
// size
$size = $this->properties['size'];
if ($type == 'varchar')
$c->addAttribute('size', $size);
// constraints
$constraints = array_diff_key($this->properties, array_flip(array('name', 'type', 'size')));
$doc2propelDict = array_flip(self::$propel2docDictionary['constraints']);
foreach ($constraints as $constraint=>$value)
{
if (array_key_exists($constraint, $doc2propelDict))
$constraint = $doc2propelDict[$constraint];
$c->addAttribute($constraint, ($value ? 'true' : 'false'));
}
if ($rel = $this->getRelation())
{
$r = $node->addChild('foreign-key');
$r->addAttribute('foreignTable', $rel['foreignTable']);
$ref = $r->addChild('reference');
$ref->addAttribute('local', $this->getName());
$ref->addAttribute('foreign', $rel['foreignReference']);
}
}
// exports this column in doctrine yml format
public function asDoctrineYml()
{
$output = array();
foreach($this->getProperties() as $key=>$value)
{
if ($key != 'name')
$output[$key] = $value;
}
if ($relation = $this->getRelation())
{
$output = array_merge($output, $relation->asDoctrineYml());
}
return $output;
}
public function debug()
{
$debug = array();
$debug['properties'] = $this->properties;
$debug['relation'] = $this->relation;
$debug['columnInfo'] = $this->getColumnInfo()->getAll();
return $debug;
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineDatabaseSchema.class.php 3455 2007-02-14 16:17:48Z chtito $
*/
/*
- class: contains a bunch of columns, toMany relationships, inheritance
information, i18n information
- table: a special class that is actually a table
- column: contains the doctrine properties (name, type, size) and the toOne relation information
*/
class sfDoctrineDatabaseSchema
{
// the class descriptions
protected $classes = array();
// a subset of the array above: classes which are also tables
protected $tables = array();
public function getClasses()
{
return $this->classes;
}
protected function getClass($className)
{
if (isset($this->classes[$className]))
return $this->classes[$className];
throw new sfDoctrineSchemaException(sprintf('The class "%s" has no description', $className));
}
// retrieves a class object from its table name
protected function findClassByTableName($tableName)
{
foreach ($this->tables as $table)
if ($table->getName() == $tableName)
{
$tableClasses = $table->getClasses();
if (count($tableClasses) != 1)
throw new sfDoctrineSchemaException(sprintf('No unique class is associated to table "%s"', $tableName));
return array_pop($tableClasses);
}
throw new sfDoctrineSchemaException(sprintf('Table "%s" not found', $tableName));
}
// set the one to many and many to many relationships
// finds out what are the foreign classes or foreign tables
protected function fixRelationships()
{
foreach ($this->classes as $className => $class)
{
foreach ($class->getColumns() as $relCol)
if ($relation = $relCol->getRelation())
{
// if no foreignClass was specified (import from propel) we find it out
if (!$relation->get('foreignClass'))
{
$foreignClass = $this->findClassByTableName($relation->get('foreignTable'));
$relation->set('foreignClass', $foreignClass->getPhpName());
}
// if foreignTable was not set (only used for export to propel)
// we figure it out
if (!$relation->get('foreignTable'))
{
$className = $relation->get('foreignClass');
$relation->set('foreignTable', $this->getClass($className)->getTableName());
}
// the relation is a many2many
if ($relation->get('counterpart'))
{
$counterpartRel = $class->getRelation($relation->get('counterpart'));
$relation->set('otherClass', $counterpartRel->get('foreignClass'));
}
// we copy all the toOne relations to the corresponding
// foreign class
$rel = $relCol->getRelation();
$this->getClass($rel->get('foreignClass'))->addToMany($rel); // FIXME: don't copy here
}
}
}
// exports the current schema as a propel xml file
public function asPropelXml()
{
$xml = new SimpleXmlElement(sprintf('<?xml version="1.0" encoding="UTF-8" ?>
<database name="%s" defaultIdMethod="native"></database>', 'connection'));
foreach ($this->tables as $table)
{
$table->addPropelXmlClasses($xml);
}
return array('source'=>$xml->asXml());
}
// exports the current schema in a sfDoctrine yml file
public function asDoctrineYml()
{
$ymlClasses = array();
foreach ($this->classes as $class)
{
$ymlClasses[$class->getPhpName()] = $class->asDoctrineYml();
}
return array('source'=>sfYaml::dump($ymlClasses));
}
public function debug()
{
$debug = array();
foreach ($this->classes as $class)
{
$debug[$class->getPhpName()] = $class->debug();
}
return $debug;
}
}
class sfDoctrineSchemaException extends sfException
{
public function __construct($message = null, $code = 0)
{
$this->setName('sfDoctrineSchemaException');
parent::__construct($message, $code);
}
}
\ No newline at end of file
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineRelationSchema.class.php 4705 2007-07-24 20:45:46Z Jonathan.Wage $
*/
class sfDoctrineRelationSchema
{
protected $relationInfo = array();
public function __construct($relationInfo)
{
foreach ($relationInfo as $key => $value)
{
$this->set($key, $value);
}
}
public function set($key, $value)
{
// we set the default foreign name
if ($key == 'foreignClass')
{
if (!isset($this->relationInfo['foreignName']))
{
$this->relationInfo['foreignName'] = $value;
}
}
$this->relationInfo[$key] = $value;
}
public function get($key)
{
if (isset($this->relationInfo[$key]))
{
return $this->relationInfo[$key];
}
else if (isset($this->relationInfo['options']))
{
if ($option = $this->relationInfo['options']->get($key))
{
return $option;
}
}
return null;
}
public function asDoctrineYml()
{
$output = array();
foreach(array('foreignClass', 'foreignReference', 'localName', 'foreignName', 'cascadeDelete', 'unique') as $key)
{
if ($value = $this->get($key))
{
$output[$key] = $value;
}
}
// FIXME: this is clumsy: change the schema syntax?
if ($verb == 'owns')
{
$output['cascadeDelete'] = true;
}
return $output;
}
public function asPhpArray($array)
{
$phpArray = 'array(';
if( !empty($array) )
{
foreach($array AS $key => $value)
{
$phpArray .= "'{$key}' => '{$value}', ";
}
$phpArray = substr($phpArray, 0, strlen($phpArray) - 2);
}
$phpArray .= ')';
return $phpArray;
}
public function asOnePhp()
{
// special behaviour for xref tables with cascade delete
$verb = ($this->get('cascadeDelete') && ($this->get('counterpart') || $this->get('unique'))) ? 'owns' : 'has';
$options['local'] = $this->get('localReference');
$options['foreign'] = $this->get('foreignReference');
//support old and new cascade declarations
if ($verb == 'owns' || $this->get('cascadeDelete') === true)
{
$options['onDelete'] = 'CASCADE';
}
if ($this->get('onDelete'))
{
$options['onDelete'] = strtoupper($this->get('onDelete'));
}
$phpOptions = $this->asPhpArray($options);
return "\$this->$verb"."One('{$this->get('foreignClass')} as {$this->get('foreignName')}', $phpOptions);";
}
public function asManyPhp()
{
$quantity = $this->get('unique') ? 'One':'Many';
// using "owns" for cascade delete except in xref table
$verb = ($this->get('cascadeDelete') && !$this->get('counterpart')) ? 'has':'has';
$otherClass = $this->get('localClass');
if ($quantity == 'Many' && $this->get('counterpart'))
{
$localReference = $this->relationInfo['localReference'];
$foreignReference = $this->relationInfo['options']->get('counterpart');
$otherClass = $this->get('otherClass');
} else {
$localReference = $this->get('foreignReference');
$foreignReference = $this->get('localReference');
}
$localClass = $this->get('localClass');
// Set refClass to localClass if it is a Many-Many relationship
if ($quantity == 'Many' && $this->get('counterpart'))
{
$refClass = $this->get('localClass');
}
if (isset($refClass) && $refClass)
{
$options['refClass'] = $refClass;
}
if ($localReference)
{
$options['local'] = $localReference;
}
if ($foreignReference)
{
$options['foreign'] = $foreignReference;
}
$phpOptions = $this->asPhpArray($options);
return "\$this->$verb$quantity('$otherClass as {$this->get('localName')}', $phpOptions);";
}
public function debug()
{
return $this->relationInfo;
}
}
\ No newline at end of file
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineSchemaDoctrineLoader.class.php 3455 2007-02-14 16:17:48Z chtito $
*/
class sfDoctrineSchemaDoctrineLoader extends sfDoctrineDatabaseSchema
{
// recursively finds out what a class table is
// FIXME: check for infinite loop?
protected function parentTable($class)
{
if ($class->hasTable())
return $class->getTable();
return $this->parentTable($this->getClass($class->getParentClassName()));
}
// associate a table to each class
protected function fixTables()
{
foreach ($this->classes as $className => $class)
{
$table = $this->parentTable($class);
$table->addClass($class);
}
}
// set up the necessary fields in the i18n table: culture, id
protected function addI18nFields()
{
foreach ($this->classes as $className => $class)
{
if (!$class->hasI18n())
continue;
$i18nClass = $this->getClass($class->getI18n('class'));
$cultureColumn = new sfDoctrineColumnSchema($class->getI18n('cultureField'), array('type'=> 'string', 'size'=> 100, 'primary'=> true));
$i18nClass->addColumn($cultureColumn);
// add the foreign key to the main table
$idDesc = array('foreignClass'=>$className, 'localName'=>$i18nClass->getPhpName(), 'onDelete'=>'cascade', 'primary'=>true);
$i18nClass->addColumn(new sfDoctrineColumnSchema('id', $idDesc));
}
}
// adds the class key fields
protected function addInheritanceFields()
{
foreach ($this->classes as $className => $class)
if ($class->hasOneTableInheritance())
{
$inh = $class->getInheritance();
$class->getTable()->addColumn(new sfDoctrineColumnSchema($inh['keyField'], array('type'=>'integer')));
}
}
public function load($file, $package = null)
{
$schema = sfYaml::load($file);
foreach ($schema as $className => $cd)
{
if (!isset($cd['tableName']) && !isset($cd['inheritance']))
throw new sfDoctrineSchemaException(sprintf('Class "%s" must have either a table or a parent', $className));
$class = new sfDoctrineClassSchema($className, $cd);
// add a table if necessary
if (isset($cd['tableName']))
{
// this top class is actually a table
$table = new sfDoctrineTableSchema($cd['tableName'], $package);
$table->addClass($class);
$this->tables[$cd['tableName']] = $table;
}
$this->classes[$className] = $class;
}
}
public function process()
{
$this->fixTables();
$this->addI18nFields();
$this->fixRelationships();
$this->addInheritanceFields();
}
}
\ No newline at end of file
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineSchemaPropelLoader.class.php 3455 2007-02-14 16:17:48Z chtito $
*/
class sfDoctrineSchemaPropelLoader extends sfDoctrineDatabaseSchema
{
// get the attributes parsed by the sfPropelDatabaseSchema class
protected function getAttribute($tag, $attribute)
{
return isset($tag['_attributes'][$attribute]) ? $tag['_attributes'][$attribute] : null;
}
public function load($file, $package = null)
{
// we figure out what kind of file we are given
$type = array_pop(explode('.', $file));
$type2method = array('yml'=>'loadYAML', 'xml'=>'loadXML');
if (isset($type2method[$type]))
$method = $type2method[$type];
else
throw new sfDoctrineSchemaException(sprintf('Unkwnown method for extension "%s"', $type));
$propelDatabaseSchema = new sfPropelDatabaseSchema();
$propelDatabaseSchema->$method($file);
$data = $propelDatabaseSchema->asArray();
foreach ($propelDatabaseSchema->getTables() as $tb_name => $tableDesc)
{
// special table class
// propel has only such classes (no inheritance support)
$table = new sfDoctrineTableSchema($tb_name, $package);
$this->tables[$tb_name] = $table;
if (!($className = $this->getAttribute($tableDesc, 'phpName')))
$className = sfInflector::camelize($tb_name); // wild guess
$class = new sfDoctrineClassSchema($className);
$table->addClass($class);
// columns
foreach ($propelDatabaseSchema->getChildren($tableDesc) as $col_name => $columnDescription)
{
if (($col_name == 'id')) // id is automatically generated in doctrine
continue;
$docCol = new sfDoctrineColumnSchema($col_name, $columnDescription, true);
$class->addColumn($docCol);
}
$this->classes[$class->getPhpName()] = $class;
}
}
public function process()
{
$this->fixRelationships();
}
}
\ No newline at end of file
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineTableSchema.class.php 3455 2007-02-14 16:17:48Z chtito $
*/
class sfDoctrineTableSchema extends sfDoctrineClassSchema
{
// the classes associated to that table
protected $classes;
// table name
protected $name;
// package of that table (usually either a plugin name, or a propel schema name)
protected $package;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function __construct($tableName, $package)
{
$this->setName($tableName);
$this->package = $package;
}
public function addClass($class)
{
// we add the class and try to avoid duplication
$this->classes[$class->getPhpName()] = $class;
$class->setTable($this);
}
public function getClasses()
{
return $this->classes;
}
// exports this table in propel xml format
public function addPropelXmlClasses(&$node)
{
$t = $node->addChild('table');
$t->addAttribute('name', $this->getName());
$t->addAttribute('phpName', $this->getPhpName());
foreach($this->classes as $class)
{
$class->addPropelXmlColumns($t);
}
}
public function getPackage()
{
return $this->package;
}
}
autoload:
# Doctrine:
# name: Doctrine classes
# ext: .php
# path: %SF_PLUGINS_DIR%/sfDoctrine/doctrine
# recursive: on
doctrine_model_classes:
name: Doctrine model classes
ext: .class.php
path: %SF_LIB_DIR%/model/doctrine
\ No newline at end of file
config/doctrine.yml:
class: sfDoctrineConfigHandler
config/schemas.yml:
class: sfDoctrineSchemasConfigHandler
all:
#doctrine attributes
attributes:
#automatic table creation (none, tables, constraints, all)
export: all
#default fetch mode (immediate, batch, lazy, offset, lazy_offset)
# fetchmode: immediate
#collection limit (integer 1+)
# coll_limit: 5
#global event listener
# listener: sfDoctrineEventListener
#enable doctrine side validation (true, false)
validate: false
# enable quoting
quote_identifier: false
all:
orm: doctrine
# default_database: <your default connection here>
\ No newline at end of file
<?php
/**
* ##MODULE_NAME## actions.
*
* @package ##PROJECT_NAME##
* @subpackage ##MODULE_NAME##
* @author ##AUTHOR_NAME##
* @version SVN: $Id: actions.class.php,v 1.1 2006/12/08 18:49:35 nathanael Exp $
*/
class ##MODULE_NAME##Actions extends auto##MODULE_NAME##Actions
{
}
generator:
class: sfDoctrineAdminGenerator
param:
model_class: ##MODEL_CLASS##
theme: crud
[?php
/**
* <?php echo $this->getGeneratedModuleName() ?> actions.
*
* @package ##PROJECT_NAME##
* @subpackage <?php echo $this->getGeneratedModuleName() ?>
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: actions.class.php 3923 2007-05-03 19:42:33Z gnat $
*/
class <?php echo $this->getGeneratedModuleName() ?>Actions extends sfActions
{
public function executeIndex ()
{
return $this->forward('<?php echo $this->getModuleName() ?>', 'list');
}
public function executeList ()
{
$this-><?php echo $this->getPluralName() ?> = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->findAll();
}
public function executeShow ()
{
$this-><?php echo $this->getSingularName() ?> = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->find(<?php echo $this->getRetrieveByPkParamsForAction('') ?>);
$this->forward404Unless($this-><?php echo $this->getSingularName() ?>);
}
public function executeCreate ()
{
$this-><?php echo $this->getSingularName() ?> = new <?php echo $this->getClassName() ?>();
$this->setTemplate('edit');
}
public function executeEdit ()
{
$this-><?php echo $this->getSingularName() ?> = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->find(<?php echo $this->getRetrieveByPkParamsForAction('') ?>);
$this->forward404Unless($this-><?php echo $this->getSingularName() ?>);
}
public function executeDelete ()
{
$this-><?php echo $this->getSingularName() ?> = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->find(<?php echo $this->getRetrieveByPkParamsForAction('') ?>);
$this->forward404Unless($this-><?php echo $this->getSingularName() ?>);
try
{
$this-><?php echo $this->getSingularName() ?>->delete();
$this->redirect('<?php echo $this->getModuleName() ?>/list');
}
catch (Doctrine_Exception $e)
{
$this->getRequest()->setError('delete', 'Could not delete the selected <?php echo sfInflector::humanize($this->getSingularName()) ?>. Make sure it does not have any associated items.');
return $this->forward('<?php echo $this->getModuleName() ?>', 'list');
}
}
public function executeUpdate ()
{
if (<?php echo $this->getTestPksForGetOrCreate(false) ?>)
{
$<?php echo $this->getSingularName() ?> = new <?php echo $this->getClassName() ?>();
}
else
{
$<?php echo $this->getSingularName() ?> = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->find(<?php echo $this->getRetrieveByPkParamsForAction('') ?>);
$this->forward404Unless($<?php echo $this->getSingularName() ?>);
}
$formData = $this->getRequestParameter('<?php echo $this->getSingularName() ?>');
<?php foreach ($this->getColumns('') as $index => $column):
$type = $column->getDoctrineType();
$name = $column->getName(); ?>
<?php if($column->isPrimaryKey()) continue ?>
<?php if ($name == 'created_at' || $name == 'updated_at') continue ?>
<?php if ($type == 'boolean'): ?>
<?php $boolVar = "\$formData['$name']";
echo $this->getColumnSetter($column, "isset($boolVar) ? $boolVar : 0", false, '')?>;
<?php continue; ?>
<?php endif; // boolean case ?>
if ($newValue = $formData['<?php echo $name ?>'])
{
<?php if ($type == 'date' || $type == 'timestamp'): ?>
<?php $inputPattern = ($type == 'date' ? 'd' : 'g');
$outputPattern = ($type == 'date' ? 'i' : 'I'); ?>
$dateFormat = new sfDateFormat($this->getUser()->getCulture());
<?php echo $this->getColumnSetter($column, sprintf('$dateFormat->format($newValue, \'%s\', $dateFormat->getInputPattern(\'%s\'))', $outputPattern, $inputPattern), false, '');?>;
<?php elseif ($column->isForeignKey()): ?>
$<?php echo $this->getSingularName()?>->set('<?php echo $column->getColumnName()?>', (empty($newValue) ? null : $newValue));
<?php else: ?>
<?php echo $this->getColumnSetter($column, '$newValue', false, '');?>;
<?php endif; ?>
}
<?php endforeach; ?>
$<?php echo $this->getSingularName() ?>->save();
return $this->redirect('<?php echo $this->getModuleName() ?>/show?<?php echo $this->getPrimaryKeyUrlParams() ?>);
<?php //' ?>
}
}
[?php use_helper('ObjectDoctrineAdmin', 'Object', 'Date') ?]
[?php echo form_tag('<?php echo $this->getModuleName() ?>/update', 'multipart=true') ?]
<?php foreach ($this->getPrimaryKey() as $pk): ?>
[?php echo object_input_hidden_tag($<?php echo $this->getSingularName() ?>, 'get<?php echo $pk->getPhpName() ?>') ?]
<?php endforeach; ?>
<table>
<tbody>
<?php foreach ($this->getColumns('') as $index => $column): ?>
<?php if ($column->isPrimaryKey()) continue ?>
<?php if ($column->getName() == 'created_at' || $column->getName() == 'updated_at') continue ?>
<tr>
<th><?php echo sfInflector::humanize(sfInflector::underscore($column->getPhpName())) ?>: </th>
<td>[?php echo <?php echo $this->getColumnEditTag($column) ?> ?]</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<hr />
[?php echo submit_tag('save') ?]
[?php if (<?php echo $this->getPrimaryKeyIsSet() ?>): ?]
&nbsp;[?php echo link_to('delete', '<?php echo $this->getModuleName() ?>/delete?<?php echo $this->getPrimaryKeyUrlParams() ?>, 'post=true&confirm=Are you sure?') ?]
&nbsp;[?php echo link_to('cancel', '<?php echo $this->getModuleName() ?>/show?<?php echo $this->getPrimaryKeyUrlParams() ?>) ?]
[?php else: ?]
&nbsp;[?php echo link_to('cancel', '<?php echo $this->getModuleName() ?>/list') ?]
[?php endif; ?]
</form>
<h1><?php echo $this->getModuleName() ?></h1>
<table>
<thead>
<tr>
<?php foreach ($this->getColumns('') as $column): ?>
<th><?php echo sfInflector::humanize($column->getName()) ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
[?php foreach ($<?php echo $this->getPluralName() ?> as $<?php echo $this->getSingularName() ?>): ?]
<tr>
<?php foreach ($this->getColumns('') as $column): ?>
<?php if ($column->isPrimaryKey()): ?>
<td>[?php echo link_to($<?php echo $this->getSingularName() ?>->get('<?php echo $column->getPhpName() ?>'), '<?php echo $this->getModuleName() ?>/show?<?php echo $this->getPrimaryKeyUrlParams() ?>); ?]</td>
<?php else: ?>
<td>[?php echo $<?php echo $this->getSingularName() ?>->get('<?php echo $column->getPhpName() ?>'); ?]</td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
[?php endforeach; ?]
<tr><td>Number of <?php echo $this->getPluralName() ?>: [?php echo count($<?php echo $this->getPluralName()?>) ?]</td></tr>
</tbody>
</table>
[?php echo link_to ('create', '<?php echo $this->getModuleName() ?>/create') ?]
<table>
<tbody>
<?php foreach ($this->getAllColumns() as $column): ?>
<tr>
<th><?php echo sfInflector::humanize(sfInflector::underscore($column->getPhpName())) ?>: </th>
<td>[?= $<?php echo $this->getSingularName() ?>->get<?php echo $column->getPhpName() ?>() ?]</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<hr />
[?php echo link_to('edit', '<?php echo $this->getModuleName() ?>/edit?<?php echo $this->getPrimaryKeyUrlParams() ?>) ?]
&nbsp;[?php echo link_to('list', '<?php echo $this->getModuleName() ?>/list') ?]
<?php
/**
* ##MODULE_NAME## actions.
*
* @package ##PROJECT_NAME##
* @subpackage ##MODULE_NAME##
* @author Your name here
* @version SVN: $Id: actions.class.php 1415 2006-06-11 08:33:51Z fabien $
*/
class ##MODULE_NAME##Actions extends auto##MODULE_NAME##Actions
{
}
generator:
class: sfDoctrineAdminGenerator
param:
model_class: ##MODEL_CLASS##
theme: default
[?php
/**
* <?php echo $this->getGeneratedModuleName() ?> actions.
*
* @package ##PROJECT_NAME##
* @subpackage <?php echo $this->getGeneratedModuleName() ?>
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: actions.class.php 4836 2007-08-07 19:10:21Z Jonathan.Wage $
*/
class <?php echo $this->getGeneratedModuleName() ?>Actions extends sfActions
{
public function preExecute ()
{
$this->getResponse()->addStylesheet('<?php echo $this->getParameterValue('css', sfConfig::get('sf_admin_web_dir').'/css/main') ?>');
}
public function executeIndex ()
{
return $this->forward('<?php echo $this->getModuleName() ?>', 'list');
}
public function executeList ()
{
$this->processSort();
$this->processFilters();
<?php if ($this->getParameterValue('list.filters')): ?>
$this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/<?php echo $this->getSingularName() ?>/filters');
<?php endif; ?>
// pager
$this->pager = new sfDoctrinePager('<?php echo $this->getClassName() ?>', <?php echo $this->getParameterValue('list.max_per_page', 20) ?>);
<?php if ($peerMethod = $this->getParameterValue('list.peer_method')): ?>
$q = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')-><?php echo $peerMethod ?>();
$this->pager->setQuery($q);
<?php endif; ?>
$this->addSortCriteria($this->pager->getQuery());
$this->addFiltersCriteria($this->pager->getQuery());
$this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/<?php echo $this->getSingularName() ?>')));
$this->pager->init();
// Save page
if ($this->getRequestParameter('page')) {
$this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/<?php echo $this->getSingularName() ?>');
}
}
public function executeCreate ()
{
return $this->forward('<?php echo $this->getModuleName() ?>', 'edit');
}
public function executeSave ()
{
return $this->forward('<?php echo $this->getModuleName() ?>', 'edit');
}
public function executeEdit ()
{
$this-><?php echo $this->getSingularName() ?> = $this->get<?php echo $this->getClassName() ?>OrCreate();
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
$this->update<?php echo $this->getClassName() ?>FromRequest();
$this->save<?php echo $this->getClassName() ?>($this-><?php echo $this->getSingularName() ?>);
$this->setFlash('notice', 'Your modifications have been saved');
if ($this->getRequestParameter('save_and_add'))
{
return $this->redirect('<?php echo $this->getModuleName() ?>/create');
}
else if ($this->getRequestParameter('save_and_list'))
{
return $this->redirect('<?php echo $this->getModuleName() ?>/list');
}
else
{
return $this->redirect('<?php echo $this->getModuleName() ?>/edit?<?php echo $this->getPrimaryKeyUrlParams('this->') ?>);
}
}
else
{
$this->addJavascriptsForEdit();
$this->labels = $this->getLabels();
}
// temporary fix to avoid using a distinct editSuccess.php template
sfLoader::loadHelpers(array('Helper', 'ObjectDoctrineAdmin'));
}
public function executeDelete ()
{
$this-><?php echo $this->getSingularName() ?> = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->find(<?php echo $this->getRetrieveByPkParamsForAction(40) ?>);
$this->forward404Unless($this-><?php echo $this->getSingularName() ?>);
try
{
$this->delete<?php echo $this->getClassName() ?>($this-><?php echo $this->getSingularName() ?>);
}
catch (Doctrine_Exception $e)
{
$this->getRequest()->setError('delete', 'Could not delete the selected <?php echo sfInflector::humanize($this->getSingularName()) ?>. Make sure it does not have any associated items.');
return $this->forward('<?php echo $this->getModuleName() ?>', 'list');
}
<?php foreach ($this->getColumnCategories('edit.display') as $category): ?>
<?php foreach ($this->getColumns('edit.display', $category) as $name => $column): ?>
<?php $input_type = $this->getParameterValue('edit.fields.'.$column->getName().'.type') ?>
<?php if ($input_type == 'admin_input_file_tag'): ?>
<?php $upload_dir = $this->replaceConstants($this->getParameterValue('edit.fields.'.$column->getName().'.upload_dir')) ?>
$currentFile = sfConfig::get('sf_upload_dir')."/<?php echo $upload_dir ?>/".<?php echo $this->getColumnGetter($column, true, 'this->')?>;
if (is_file($currentFile))
{
unlink($currentFile);
}
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
return $this->redirect('<?php echo $this->getModuleName() ?>/list');
}
public function handleErrorEdit()
{
$this->preExecute();
$this-><?php echo $this->getSingularName() ?> = $this->get<?php echo $this->getClassName() ?>OrCreate();
$this->update<?php echo $this->getClassName() ?>FromRequest();
$this->addJavascriptsForEdit();
$this->labels = $this->getLabels();
// temporary fix to avoid using a distinct editSuccess.php template
sfLoader::loadHelpers(array('Helper', 'ObjectDoctrineAdmin'));
return sfView::SUCCESS;
}
protected function save<?php echo $this->getClassName() ?>($<?php echo $this->getSingularName() ?>)
{
$<?php echo $this->getSingularName() ?>->save();
}
protected function delete<?php echo $this->getClassName() ?>($<?php echo $this->getSingularName() ?>)
{
$<?php echo $this->getSingularName() ?>->delete();
}
protected function update<?php echo $this->getClassName() ?>FromRequest()
{
$<?php echo $this->getSingularName() ?> = $this->getRequestParameter('<?php echo $this->getSingularName() ?>');
<?php foreach ($this->getColumnCategories('edit.display') as $category): ?>
<?php foreach ($this->getColumns('edit.display', $category) as $column): $type = $column->getDoctrineType(); ?>
<?php $name = $column->getName(); ?>
<?php if ($column->isPrimaryKey()) continue ?>
<?php $credentials = $this->getParameterValue('edit.fields.'.$name.'.credentials') ?>
<?php $input_type = $this->getParameterValue('edit.fields.'.$name.'.type') ?>
<?php if ($credentials): $credentials = str_replace("\n", ' ', var_export($credentials, true)) ?>
if ($this->getUser()->hasCredential(<?php echo $credentials ?>))
{
<?php endif; ?>
<?php if ($input_type == 'admin_input_file_tag'): ?>
<?php $upload_dir = $this->replaceConstants($this->getParameterValue('edit.fields.'.$column->getName().'.upload_dir')) ?>
$currentFile = sfConfig::get('sf_upload_dir')."/<?php echo $upload_dir ?>/".<?php echo $this->getColumnGetter($column, true, 'this->')?>;
if (!$this->getRequest()->hasErrors() && isset($<?php echo $this->getSingularName() ?>['<?php echo $name ?>_remove']))
{
<?php echo $this->getColumnSetter($column, '', true) ?>;
if (is_file($currentFile))
{
unlink($currentFile);
}
}
if (!$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('<?php echo $this->getSingularName() ?>[<?php echo $name ?>]'))
{
<?php elseif ($type != 'boolean'): ?>
if (isset($<?php echo $this->getSingularName() ?>['<?php echo $name ?>']))
{
<?php endif; ?>
<?php if ($input_type == 'admin_input_file_tag'): ?>
<?php if ($this->getParameterValue('edit.fields.'.$name.'.filename')): ?>
$fileName = "<?php echo str_replace('"', '\\"', $this->replaceConstants($this->getParameterValue('edit.fields.'.$column->getName().'.filename'))) ?>";
<?php else: ?>
$fileName = md5($this->getRequest()->getFileName('<?php echo $this->getSingularName() ?>[<?php echo $name ?>]').time());
<?php endif; ?>
$ext = $this->getRequest()->getFileExtension('<?php echo $this->getSingularName() ?>[<?php echo $name ?>]');
if (is_file($currentFile))
{
unlink($currentFile);
}
$this->getRequest()->moveFile('<?php echo $this->getSingularName() ?>[<?php echo $name ?>]', sfConfig::get('sf_upload_dir')."/<?php echo $upload_dir ?>/".$fileName.$ext);
<?php echo $this->getColumnSetter($column, '$fileName.$ext')?>;
<?php elseif ($type == 'date' || $type == 'timestamp'): ?>
if ($<?php echo $this->getSingularName() ?>['<?php echo $name ?>'])
{
$dateFormat = new sfDateFormat($this->getUser()->getCulture());
<?php
$inputPattern = ($type == 'date' ? 'd' : 'g');
$outputPattern = ($type == 'date' ? 'i' : 'I');
?>
// if this is a direct date input (rich == true)
if (!is_array($<?php echo $this->getSingularName() ?>['<?php echo $name ?>']))
{
try
{
$value = $dateFormat->format($<?php echo $this->getSingularName() ?>['<?php echo $name ?>'], '<?php echo $outputPattern ?>', $dateFormat->getInputPattern('<?php echo $inputPattern ?>'));
}
catch (sfException $e)
{
// not a valid date
}
}
else // rich == false
{
$value_array = $<?php echo $this->getSingularName() ?>['<?php echo $name ?>'];
$value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
}
<?php echo $this->getColumnSetter($column, '$value') ?>;
}
else
{
<?php echo $this->getColumnSetter($column, 'null') ?>;
}
<?php elseif ($type == 'boolean'): ?>
<?php $boolVar = "\${$this->getSingularName()}['$name']";
echo $this->getColumnSetter($column, "isset($boolVar) ? $boolVar : 0") ?>;
<?php elseif ($column->isForeignKey()): ?>
$foreignKey = $<?php echo $this->getSingularName() ?>['<?php echo $name ?>'];
$foreignKey = empty($foreignKey) ? null : $foreignKey;
$this-><?php echo $this->getSingularName()?>->set('<?php echo $column->getColumnName()?>', $foreignKey);
<?php else: ?>
$this-><?php echo $this->getSingularName() ?>->set('<?php echo $column->getName() ?>', $<?php echo $this->getSingularName() ?>['<?php echo $name ?>']);
<?php endif; ?>
<?php if ($type != 'boolean'): ?>
}
<?php endif; ?>
<?php // double lists
if (in_array($input_type, array('doctrine_admin_double_list', 'doctrine_admin_check_list', 'doctrine_admin_select_list'))): ?>
// Update many-to-many for "<?php echo $name ?>"
$<?php echo $name?>Table = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->getRelation('<?php echo $name ?>')->getTable();
$associationName = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->getRelation('<?php echo $name ?>')->getAssociationTable()->getOption('name');
$this-><?php echo $this->getSingularName()?>->$associationName->delete();
$ids = $this->getRequestParameter('associated_<?php echo $name ?>');
if (is_array($ids))
{
foreach ($ids as $id)
{
$id = explode('/', $id);
$this-><?php echo $this->getSingularName()?>->get('<?php echo $name ?>')->add($<?php echo $name?>Table->find($id));
}
}
<?php endif; // double lists ?>
<?php if ($credentials): ?>
}
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
}
protected function get<?php echo $this->getClassName() ?>OrCreate (<?php echo $this->getMethodParamsForGetOrCreate() ?>)
{
if (<?php echo $this->getTestPksForGetOrCreate() ?>)
{
$<?php echo $this->getSingularName() ?> = new <?php echo $this->getClassName() ?>();
}
else
{
$<?php echo $this->getSingularName() ?> = sfDoctrine::getTable('<?php echo $this->getClassName() ?>')->find(array(<?php echo $this->getRetrieveByPkParamsForGetOrCreate() ?>));
$this->forward404Unless($<?php echo $this->getSingularName() ?>);
}
return $<?php echo $this->getSingularName() ?>;
}
protected function processFilters ()
{
<?php if ($this->getParameterValue('list.filters')): ?>
if ($this->getRequest()->hasParameter('filter'))
{
$filters = $this->getRequestParameter('filters');
<?php foreach ($this->getColumns('list.filters') as $column): $type = $column->getDoctrineType() ?>
<?php if ($type == 'date' || $type == 'timestamp'):
$inputPattern = ($type == 'date' ? 'd' : 'g');
$outputPattern = ($type == 'date' ? 'i' : 'I'); ?>
$dateFormat = new sfDateFormat($this->getUser()->getCulture());
if (isset($filters['<?php echo $column->getName() ?>']['from']) && $filters['<?php echo $column->getName() ?>']['from'] !== '')
{
$filters['<?php echo $column->getName() ?>']['from'] = $dateFormat->format($filters['<?php echo $column->getName() ?>']['from'], '<?php echo $outputPattern?>', $dateFormat->getInputPattern('<?php echo $inputPattern ?>'));
}
if (isset($filters['<?php echo $column->getName() ?>']['to']) && $filters['<?php echo $column->getName() ?>']['to'] !== '')
{
$filters['<?php echo $column->getName() ?>']['to'] = $dateFormat->format($filters['<?php echo $column->getName() ?>']['to'], '<?php echo $outputPattern?>', $dateFormat->getInputPattern('<?php echo $inputPattern ?>'));
}
<?php endif; ?>
<?php endforeach; ?>
$this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/<?php echo $this->getSingularName() ?>');
$this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/<?php echo $this->getSingularName() ?>/filters');
$this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/<?php echo $this->getSingularName() ?>/filters');
}
<?php endif; ?>
}
protected function processSort ()
{
if ($this->getRequestParameter('sort'))
{
$this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
$this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
}
if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort'))
{
<?php if ($sort = $this->getParameterValue('list.sort')): ?>
<?php if (is_array($sort)): ?>
$this->getUser()->setAttribute('sort', '<?php echo $sort[0] ?>', 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
$this->getUser()->setAttribute('type', '<?php echo $sort[1] ?>', 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
<?php else: ?>
$this->getUser()->setAttribute('sort', '<?php echo $sort ?>', 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
$this->getUser()->setAttribute('type', 'asc', 'sf_admin/<?php echo $this->getSingularName() ?>/sort');
<?php endif; ?>
<?php endif; ?>
}
}
protected function addFiltersCriteria ($q)
{
<?php if ($this->getParameterValue('list.filters')): ?>
<?php foreach ($this->getColumns('list.filters') as $column): $type = $column->getDoctrineType() ?>
<?php if (($column->isPartial() || $column->isComponent()) && $this->getParameterValue('list.fields.'.$column->getName().'.filter_criteria_disabled')) continue ?>
<?php
$filterColumnName = $column->getName();
if ($column->isForeignKey())
$filterColumnName = $column->getColumnName();
$queryColumn = $this->getClassName().'.'.$filterColumnName;?>
if (isset($this->filters['<?php echo $column->getName() ?>_is_empty']))
{
$q->addWhere("<?php echo $queryColumn?> = '' OR <?php echo $queryColumn?> IS NULL");
}
<?php if ($type == 'date' || $type == 'timestamp'): ?>
else if (isset($this->filters['<?php echo $column->getName() ?>']))
{
if (isset($this->filters['<?php echo $column->getName() ?>']['from']) && $this->filters['<?php echo $column->getName() ?>']['from'] !== '')
{
<?php
$dateArg = "\$this->filters['{$column->getName()}']['%s']";
?>
$q->addWhere('<?php echo $queryColumn?> >= ?', <?php echo sprintf($dateArg, 'from') ?>);
}
if (isset($this->filters['<?php echo $column->getName() ?>']['to']) && $this->filters['<?php echo $column->getName() ?>']['to'] !== '')
{
$q->addWhere('<?php echo $queryColumn?> <= ?', <?php echo sprintf($dateArg, 'to') ?>);
}
}
<?php else: ?>
else if (isset($this->filters['<?php echo $column->getName() ?>']) && $this->filters['<?php echo $column->getName() ?>'] !== '')
{
<?php if ($type == 'char' || $type == 'string'): ?>
$q->addWhere("<?php echo $queryColumn?> LIKE ?", '%'.$this->filters['<?php echo $column->getName() ?>'].'%');
<?php else: ?>
$q->addWhere("<?php echo $queryColumn?> = ?", $this->filters['<?php echo $column->getName() ?>']);
<?php endif; ?>
}
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
}
protected function addSortCriteria ($q)
{
if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort'))
{
$table = sfDoctrine::getTable('<?php echo $this->getClassName()?>');
$colNames = array_keys($table->getColumns());
if (!in_array($sort_column, $colNames)) // illegal column name
return;
if ($this->getUser()->getAttribute('type', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort') == 'asc')
{
$q->orderBy('<?php echo $this->getClassName()?>.'.$sort_column);
}
else
{
$q->orderBy('<?php echo $this->getClassName()?>.'.$sort_column.' desc');
}
}
}
protected function addJavascriptsForEdit()
{
$this->getResponse()->addJavascript(sfConfig::get('sf_prototype_web_dir').'/js/prototype');
$this->getResponse()->addJavascript(sfConfig::get('sf_admin_web_dir').'/js/collapse');
$this->getResponse()->addJavascript(sfConfig::get('sf_admin_web_dir').'/js/double_list');
}
protected function getLabels()
{
return array(
<?php foreach ($this->getColumnCategories('edit.display') as $category): ?>
<?php foreach ($this->getColumns('edit.display', $category) as $name => $column): ?>
'<?php echo $this->getSingularName() ?>{<?php echo $column->getName() ?>}' => '<?php $label_name = str_replace("'", "\\'", $this->getParameterValue('edit.fields.'.$column->getName().'.name')); echo $label_name ?><?php if ($label_name): ?>:<?php endif; ?>',
<?php endforeach; ?>
<?php endforeach; ?>
);
}
}
<ul class="sf_admin_actions">
<?php $editActions = $this->getParameterValue('edit.actions') ?>
<?php if (null !== $editActions): ?>
<?php foreach ((array) $editActions as $actionName => $params): ?>
<?php if ($actionName == '_delete') continue ?>
<?php echo $this->addCredentialCondition($this->getButtonToAction($actionName, $params, true), $params) ?>
<?php endforeach; ?>
<?php else: ?>
<?php echo $this->getButtonToAction('_list', array(), true) ?>
<?php echo $this->getButtonToAction('_save', array(), true) ?>
<?php echo $this->getButtonToAction('_save_and_add', array(), true) ?>
<?php endif; ?>
</ul>
[?php echo form_tag('<?php echo $this->getModuleName() ?>/save', array(
'id' => 'sf_admin_edit_form',
'name' => 'sf_admin_edit_form',
'multipart' => true,
<?php foreach ($this->getColumnCategories('edit.display') as $category): ?>
<?php foreach ($this->getColumns('edit.display', $category) as $name => $column): ?>
<?php if (false !== strpos($this->getParameterValue('edit.fields.'.$column->getName().'.type'), 'admin_double_list')): ?>
'onsubmit' => 'double_list_submit(); return true;'
<?php break 2; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
)) ?]
<?php foreach ($this->getPrimaryKey() as $pk): ?>
[?php echo object_input_hidden_tag($<?php echo $this->getSingularName() ?>, 'get<?php echo $pk->getPhpName() ?>') ?]
<?php endforeach; ?>
<?php $first = true ?>
<?php foreach ($this->getColumnCategories('edit.display') as $category): ?>
<?php
if ($category[0] == '-')
{
$category_name = substr($category, 1);
$collapse = true;
if ($first)
{
$first = false;
echo "[?php use_javascript(sfConfig::get('sf_prototype_web_dir').'/js/prototype') ?]\n";
echo "[?php use_javascript(sfConfig::get('sf_admin_web_dir').'/js/collapse') ?]\n";
}
}
else
{
$category_name = $category;
$collapse = false;
}
?>
<fieldset id="sf_fieldset_<?php echo preg_replace('/[^a-z0-9_]/', '_', strtolower($category_name)) ?>" class="<?php if ($collapse): ?> collapse<?php endif; ?>">
<?php if ($category != 'NONE'): ?><h2>[?php echo __('<?php echo $category_name ?>') ?]</h2>
<?php endif; ?>
<?php $hides = $this->getParameterValue('edit.hide', array()) ?>
<?php foreach ($this->getColumns('edit.display', $category) as $name => $column): ?>
<?php if (in_array($column->getName(), $hides)) continue ?>
<?php if ($column->isPrimaryKey()) continue ?>
<?php $credentials = $this->getParameterValue('edit.fields.'.$column->getName().'.credentials') ?>
<?php if ($credentials): $credentials = str_replace("\n", ' ', var_export($credentials, true)) ?>
[?php if ($sf_user->hasCredential(<?php echo $credentials ?>)): ?]
<?php endif; ?>
<div class="form-row">
[?php echo label_for('<?php echo $this->getParameterValue("edit.fields.".$column->getName().".label_for", $this->getSingularName()."[".$column->getName()."]") ?>', __($labels['<?php echo $this->getSingularName() ?>{<?php echo $column->getName() ?>}']), '<?php if ($column->isNotNull()): ?>class="required" <?php endif; ?>') ?]
<div class="content[?php if ($sf_request->hasError('<?php echo $this->getSingularName() ?>{<?php echo $column->getName() ?>}')): ?] form-error[?php endif; ?]">
[?php if ($sf_request->hasError('<?php echo $this->getSingularName() ?>{<?php echo $column->getName() ?>}')): ?]
[?php echo form_error('<?php echo $this->getSingularName() ?>{<?php echo $column->getName() ?>}', array('class' => 'form-error-msg')) ?]
[?php endif; ?]
[?php $value = <?php echo $this->getColumnEditTag($column); ?>; echo $value ? $value : '&nbsp;' ?]
<?php echo $this->getHelp($column, 'edit') ?>
</div>
</div>
<?php if ($credentials): ?>
[?php endif; ?]
<?php endif; ?>
<?php endforeach; ?>
</fieldset>
<?php endforeach; ?>
[?php include_partial('edit_actions', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>)) ?]
</form>
<ul class="sf_admin_actions">
<?php
/*
* WARNING: delete is a form, it must be outside the main form
*/
$editActions = $this->getParameterValue('edit.actions');
?>
<?php if (null === $editActions || (null !== $editActions && array_key_exists('_delete', $editActions))): ?>
<?php echo $this->addCredentialCondition($this->getButtonToAction('_delete', $editActions['_delete'], true), $editActions['_delete']) ?>
<?php endif; ?>
</ul>
[?php if ($sf_request->hasErrors()): ?]
<div class="form-errors">
<h2>[?php echo __('There are some errors that prevent the form to validate') ?]</h2>
<dl>
[?php foreach ($sf_request->getErrorNames() as $name): ?]
<dt>[?php echo __($labels[$name]) ?]</dt>
<dd>[?php echo $sf_request->getError($name) ?]</dd>
[?php endforeach; ?]
</dl>
</div>
[?php elseif ($sf_flash->has('notice')): ?]
<div class="save-ok">
<h2>[?php echo __($sf_flash->get('notice')) ?]</h2>
</div>
[?php endif; ?]
[?php use_helper('Object') ?]
<?php if ($this->getParameterValue('list.filters')): ?>
<div class="sf_admin_filters">
[?php echo form_tag('<?php echo $this->getModuleName() ?>/list', array('method' => 'get')) ?]
<fieldset>
<h2>[?php echo __('filters') ?]</h2>
<?php foreach ($this->getColumns('list.filters') as $column): $type = $column->getCreoleType() ?>
<?php $credentials = $this->getParameterValue('list.fields.'.$column->getName().'.credentials') ?>
<?php if ($credentials): $credentials = str_replace("\n", ' ', var_export($credentials, true)) ?>
[?php if ($sf_user->hasCredential(<?php echo $credentials ?>)): ?]
<?php endif; ?>
<div class="form-row">
<label for="<?php echo $column->getName() ?>">[?php echo __('<?php echo str_replace("'", "\\'", $this->getParameterValue('list.fields.'.$column->getName().'.name')) ?>:') ?]</label>
<div class="content">
[?php echo <?php echo $this->getColumnFilterTag($column) ?> ?]
<?php if ($this->getParameterValue('list.fields.'.$column->getName().'.filter_is_empty')): ?>
<div>[?php echo checkbox_tag('filters[<?php echo $column->getName() ?>_is_empty]', 1, isset($filters['<?php echo $column->getName() ?>_is_empty']) ? $filters['<?php echo $column->getName() ?>_is_empty'] : null) ?]&nbsp;<label for="filters[<?php echo $column->getName() ?>_is_empty]">[?php echo __('is empty') ?]</label></div>
<?php endif; ?>
</div>
</div>
<?php if ($credentials): ?>
[?php endif; ?]
<?php endif; ?>
<?php endforeach; ?>
</fieldset>
<ul class="sf_admin_actions">
<li>[?php echo button_to(__('reset'), '<?php echo $this->getModuleName() ?>/list?filter=filter', 'class=sf_admin_action_reset_filter') ?]</li>
<li>[?php echo submit_tag(__('filter'), 'name=filter class=sf_admin_action_filter') ?]</li>
</ul>
</form>
</div>
<?php endif; ?>
<table cellspacing="0" class="sf_admin_list">
<thead>
<tr>
[?php include_partial('list_th_<?php echo $this->getParameterValue('list.layout', 'tabular') ?>') ?]
<?php if ($this->getParameterValue('list.object_actions')): ?>
<th id="sf_admin_list_th_sf_actions">[?php echo __('Actions') ?]</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
[?php $i = 1; foreach ($pager->getResults() as $<?php echo $this->getSingularName() ?>): $odd = fmod(++$i, 2) ?]
<tr class="sf_admin_row_[?php echo $odd ?]">
[?php include_partial('list_td_<?php echo $this->getParameterValue('list.layout', 'tabular') ?>', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>)) ?]
[?php include_partial('list_td_actions', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>)) ?]
</tr>
[?php endforeach; ?]
</tbody>
<tfoot>
<tr><th colspan="<?php echo $this->getParameterValue('list.object_actions') ? count($this->getColumns('list.display')) + 1 : count($this->getColumns('list.display')) ?>">
<div class="float-right">
[?php if ($pager->haveToPaginate()): ?]
[?php echo link_to(image_tag(sfConfig::get('sf_admin_web_dir').'/images/first.png', array('align' => 'absmiddle', 'alt' => __('First'), 'title' => __('First'))), '<?php echo $this->getModuleName() ?>/list?page=1') ?]
[?php echo link_to(image_tag(sfConfig::get('sf_admin_web_dir').'/images/previous.png', array('align' => 'absmiddle', 'alt' => __('Previous'), 'title' => __('Previous'))), '<?php echo $this->getModuleName() ?>/list?page='.$pager->getPreviousPage()) ?]
[?php foreach ($pager->getLinks() as $page): ?]
[?php echo link_to_unless($page == $pager->getPage(), $page, '<?php echo $this->getModuleName() ?>/list?page='.$page) ?]
[?php endforeach; ?]
[?php echo link_to(image_tag(sfConfig::get('sf_admin_web_dir').'/images/next.png', array('align' => 'absmiddle', 'alt' => __('Next'), 'title' => __('Next'))), '<?php echo $this->getModuleName() ?>/list?page='.$pager->getNextPage()) ?]
[?php echo link_to(image_tag(sfConfig::get('sf_admin_web_dir').'/images/last.png', array('align' => 'absmiddle', 'alt' => __('Last'), 'title' => __('Last'))), '<?php echo $this->getModuleName() ?>/list?page='.$pager->getLastPage()) ?]
[?php endif; ?]
</div>
[?php echo format_number_choice('[0] no result|[1] 1 result|(1,+Inf] %1% results', array('%1%' => $pager->getNbResults()), $pager->getNbResults()) ?]
</th></tr>
</tfoot>
</table>
<ul class="sf_admin_actions">
<?php $listActions = $this->getParameterValue('list.actions') ?>
<?php if (null !== $listActions): ?>
<?php foreach ((array) $listActions as $actionName => $params): ?>
<?php echo $this->addCredentialCondition($this->getButtonToAction($actionName, $params, false), $params) ?>
<?php endforeach; ?>
<?php else: ?>
<?php echo $this->getButtonToAction('_create', array(), false) ?>
<?php endif; ?>
</ul>
[?php if ($sf_request->getError('delete')): ?]
<div class="form-errors">
<h2>[?php echo __('Could not delete the selected %name%', array('%name%' => '<?php echo sfInflector::humanize($this->getSingularName()) ?>')) ?]</h2>
<ul>
<li>[?php echo $sf_request->getError('delete') ?]</li>
</ul>
</div>
[?php endif; ?]
<?php if ($this->getParameterValue('list.object_actions')): ?>
<td>
<ul class="sf_admin_td_actions">
<?php foreach ($this->getParameterValue('list.object_actions') as $actionName => $params): ?>
<?php echo $this->addCredentialCondition($this->getLinkToAction($actionName, $params, true), $params) ?>
<?php endforeach; ?>
</ul>
</td>
<?php endif; ?>
<td colspan="<?php echo count($this->getColumns('list.display')) ?>">
<?php if ($this->getParameterValue('list.params')): ?>
<?php echo $this->getI18NString('list.params') ?>
<?php else: ?>
<?php $hides = $this->getParameterValue('list.hide', array()) ?>
<?php foreach ($this->getColumns('list.display') as $column): ?>
<?php if (in_array($column->getName(), $hides)) continue ?>
<?php if ($column->isLink()): ?>
[?php echo link_to(<?php echo $this->getColumnListTag($column) ?> ? <?php echo $this->getColumnListTag($column) ?> : __('-'), '<?php echo $this->getModuleName() ?>/edit?<?php echo $this->getPrimaryKeyUrlParams() ?>) ?]
<?php else: ?>
[?php echo <?php echo $this->getColumnListTag($column) ?> ?]
<?php endif; ?>
-
<?php endforeach; ?>
<?php endif; ?>
</td>
\ No newline at end of file
<?php $hs = $this->getParameterValue('list.hide', array()) ?>
<?php foreach ($this->getColumns('list.display') as $column): ?>
<?php if (in_array($column->getName(), $hs)) continue ?>
<?php $credentials = $this->getParameterValue('list.fields.'.$column->getName().'.credentials') ?>
<?php if ($credentials): $credentials = str_replace("\n", ' ', var_export($credentials, true)) ?>
[?php if ($sf_user->hasCredential(<?php echo $credentials ?>)): ?]
<?php endif; ?>
<?php if ($column->isLink()): ?>
<td>[?php echo link_to(<?php echo $this->getColumnListTag($column) ?> ? <?php echo $this->getColumnListTag($column) ?> : __('-'), '<?php echo $this->getModuleName() ?>/edit?<?php echo $this->getPrimaryKeyUrlParams() ?>) ?]</td>
<?php else: ?>
<td>[?php echo <?php echo $this->getColumnListTag($column) ?> ?]</td>
<?php endif; ?>
<?php if ($credentials): ?>
[?php endif; ?]
<?php endif; ?>
<?php endforeach; ?>
<?php $hides = $this->getParameterValue('list.hide', array()) ?>
<?php foreach ($this->getColumns('list.display') as $column): ?>
<?php if (in_array($column->getName(), $hides)) continue ?>
<?php $credentials = $this->getParameterValue('list.fields.'.$column->getName().'.credentials') ?>
<?php if ($credentials): $credentials = str_replace("\n", ' ', var_export($credentials, true)) ?>
[?php if ($sf_user->hasCredential(<?php echo $credentials ?>)): ?]
<?php endif; ?>
<th id="sf_admin_list_th_<?php echo $column->getName() ?>">
<?php if ($column->isReal()): ?>
[?php if ($sf_user->getAttribute('sort', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort') == '<?php echo $column->getName() ?>'): ?]
[?php echo link_to(__('<?php echo str_replace("'", "\\'", $this->getParameterValue('list.fields.'.$column->getName().'.name')) ?>'), '<?php echo $this->getModuleName() ?>/list?sort=<?php echo $column->getName() ?>&type='.($sf_user->getAttribute('type', 'asc', 'sf_admin/<?php echo $this->getSingularName() ?>/sort') == 'asc' ? 'desc' : 'asc')) ?]
([?php echo __($sf_user->getAttribute('type', 'asc', 'sf_admin/<?php echo $this->getSingularName() ?>/sort')) ?])
[?php else: ?]
[?php echo link_to(__('<?php echo str_replace("'", "\\'", $this->getParameterValue('list.fields.'.$column->getName().'.name')) ?>'), '<?php echo $this->getModuleName() ?>/list?sort=<?php echo $column->getName() ?>&type=asc') ?]
[?php endif; ?]
<?php else: ?>
[?php echo __('<?php echo str_replace("'", "\\'", $this->getParameterValue('list.fields.'.$column->getName().'.name')) ?>') ?]
<?php endif; ?>
<?php echo $this->getHelpAsIcon($column, 'list') ?>
</th>
<?php if ($credentials): ?>
[?php endif; ?]
<?php endif; ?>
<?php endforeach; ?>
[?php use_helper('Object', 'Validation', 'ObjectAdmin', 'I18N', 'Date') ?]
[?php use_stylesheet('<?php echo $this->getParameterValue('css', sfConfig::get('sf_admin_web_dir').'/css/main') ?>') ?]
<div id="sf_admin_container">
<h1><?php echo $this->getI18NString('edit.title', 'edit '.$this->getModuleName()) ?></h1>
<div id="sf_admin_header">
[?php include_partial('<?php echo $this->getModuleName() ?>/edit_header', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>)) ?]
</div>
<div id="sf_admin_content">
[?php include_partial('<?php echo $this->getModuleName() ?>/edit_messages', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>, 'labels' => $labels)) ?]
[?php include_partial('<?php echo $this->getModuleName() ?>/edit_form', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>, 'labels' => $labels)) ?]
</div>
<div id="sf_admin_footer">
[?php include_partial('<?php echo $this->getModuleName() ?>/edit_footer', array('<?php echo $this->getSingularName() ?>' => $<?php echo $this->getSingularName() ?>)) ?]
</div>
</div>
[?php use_helper('I18N', 'Date') ?]
[?php use_stylesheet('<?php echo $this->getParameterValue('css', sfConfig::get('sf_admin_web_dir').'/css/main') ?>') ?]
<div id="sf_admin_container">
<h1><?php echo $this->getI18NString('list.title', $this->getModuleName().' list') ?></h1>
<div id="sf_admin_header">
[?php include_partial('<?php echo $this->getModuleName() ?>/list_header', array('pager' => $pager)) ?]
[?php include_partial('<?php echo $this->getModuleName() ?>/list_messages', array('pager' => $pager)) ?]
</div>
<div id="sf_admin_bar">
<?php if ($this->getParameterValue('list.filters')): ?>
[?php include_partial('filters', array('filters' => $filters)) ?]
<?php endif; ?>
</div>
<div id="sf_admin_content">
[?php if (!$pager->getNbResults()): ?]
[?php echo __('no result') ?]
[?php else: ?]
[?php include_partial('<?php echo $this->getModuleName() ?>/list', array('pager' => $pager)) ?]
[?php endif; ?]
[?php include_partial('list_actions') ?]
</div>
<div id="sf_admin_footer">
[?php include_partial('<?php echo $this->getModuleName() ?>/list_footer', array('pager' => $pager)) ?]
</div>
</div>
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @author Nathanael D. Noblet <nathanael@gnat.ca>
* @version SVN: $Id: sfPakeDoctrine.php 4878 2007-08-17 17:45:54Z Jonathan.Wage $
*/
pake_desc('converts propel schema.*ml into doctrine schema');
pake_task('doctrine-import', 'project_exists');
pake_desc('exports doctrine schemas to sql');
pake_task('doctrine-build-sql', 'project_exists');
pake_desc('insert sql for doctrine schemas in to database');
pake_task('doctrine-insert-sql', 'project_exists');
pake_desc('build Doctrine classes');
pake_task('doctrine-build-model', 'project_exists');
pake_desc('Creates Doctrine CRUD Module');
pake_task('doctrine-generate-crud', 'app_exists');
pake_desc('initialize a new doctrine admin module');
pake_task('doctrine-init-admin', 'app_exists');
pake_desc('dump data to yaml fixtures file');
pake_task('doctrine-dump-data', 'project_exists');
pake_desc('load data from yaml fixtures file');
pake_task('doctrine-load-data', 'project_exists');
pake_desc('load doctrine nested set data from nested set fixtures file');
pake_task('doctrine-load-nested-set', 'project_exists');
pake_desc('doctrine build all - generate model and initialize database, drops current database if exists');
pake_task('doctrine-build-all', 'project_exists');
pake_desc('doctrine build all load - generate model, initialize database, and load data from fixtures. Drops current database if exists');
pake_task('doctrine-build-all-load', 'project_exists');
pake_desc('doctrine build schema - build schema from an existing database');
pake_task('doctrine-build-schema', 'project_exists');
pake_desc('doctrine drop all - drop all database tables');
pake_task('doctrine-drop-all-tables', 'project_exists');
pake_desc('doctrine build database - initialize database, drop current database if exists');
pake_task('doctrine-build-db', 'project_exists');
pake_desc('doctrine drop database - drops database');
pake_task('doctrine-drop-db', 'project_exists');
function run_doctrine_drop_all_tables($task, $args)
{
if (!count($args))
{
throw new Exception('You must provide the app.');
}
$app = $args[0];
$env = empty($args[1]) ? 'dev' : $args[1];
_load_application_environment($app, $env);
$sf_root_dir = sfConfig::get('sf_root_dir');
$declared = get_declared_classes();
$directory = sfConfig::get('sf_lib_dir').DIRECTORY_SEPARATOR.'model'.DIRECTORY_SEPARATOR.'doctrine'.DIRECTORY_SEPARATOR;
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);
}
$parent = new ReflectionClass('Doctrine_Record');
$sql = array();
$fks = array();
// we iterate trhough the diff of previously declared classes
// and currently declared classes
foreach ($declared 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)
if ($class->isSubclassOf($parent) && ! $class->isAbstract() && method_exists($class->getName(), 'setTableDefinition'))
{
$record = new $name();
$table = $record->getTable();
try {
pake_echo_action('doctrine', "dropping table '".$table->getTableName()."'");
$table->getConnection()->export->dropTable($table->getTableName());
} catch(Exception $e) {
continue;
}
}
}
}
function run_doctrine_load_data($task, $args)
{
if (!count($args))
{
throw new Exception('You must provide the app.');
}
$app = $args[0];
if (!is_dir(sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.$app))
{
throw new Exception('The app "'.$app.'" does not exist.');
}
if (count($args) > 1 && $args[count($args) - 1] == 'append')
{
array_pop($args);
$delete = false;
}
else
{
$delete = true;
}
$env = empty($args[1]) ? 'dev' : $args[1];
_load_application_environment($app, $env);
if (count($args) == 1)
{
if (!$pluginDirs = glob(sfConfig::get('sf_root_dir').'/plugins/*/data'))
{
$pluginDirs = array();
}
$fixtures_dirs = pakeFinder::type('dir')->name('fixtures')->in(array_merge($pluginDirs, array(sfConfig::get('sf_data_dir'))));
}
else
{
$fixtures_dirs = array_slice($args, 1);
}
$data = new sfDoctrineData();
$data->setDeleteCurrentData($delete);
foreach ($fixtures_dirs as $fixtures_dir)
{
if (!is_readable($fixtures_dir))
{
continue;
}
pake_echo_action('doctrine', sprintf('load data from "%s"', $fixtures_dir));
$data->loadData($fixtures_dir);
}
}
function run_doctrine_import($task, $args)
{
$type = 'xml';
if (isset($args[0]))
$type = $args[0];
$schemas = _doctrine_load('propel', $type, false);
$doctrineSchemasDir = sfConfig::get('sf_config_dir').DIRECTORY_SEPARATOR.'doctrine'.DIRECTORY_SEPARATOR;
pake_mkdirs($doctrineSchemasDir);
foreach($schemas as $schema)
{
$doctrineYml = $schema->asDoctrineYml();
$classes = $schema->getClasses();
$class = array_pop($classes);
$package = $class->getTable()->getPackage();
$filePath = $package.'.yml';
pake_echo_action('writing', $filePath);
file_put_contents($doctrineSchemasDir.$filePath, $doctrineYml['source']);
}
}
function run_doctrine_export($task, $args)
{
$schemas = _doctrine_load('doctrine', 'yml', false);
$configDir = sfConfig::get('sf_config_dir').DIRECTORY_SEPARATOR;
foreach($schemas as $schema)
{
$propelXml = $schema->asPropelXml();
// we do some tidying before echoing the xml
$source = preg_replace(array('#</database#', '#<(/?)table#', '#<column#', '#<(/?)foreign-key#', '#<reference#'), array("\n</database", "\n<\\1table", "\n <column", "\n <\\1foreign-key", "\n <reference",), $propelXml['source']);
$filePath = $propelXml['name'].'-schema.xml';
pake_echo_action('writing', $filePath);
file_put_contents($configDir.$filePath, $source);
}
}
function run_doctrine_insert_sql($task, $args)
{
if (!count($args))
{
throw new Exception('You must provide the app.');
}
$app = $args[0];
$env = empty($args[1]) ? 'dev' : $args[1];
_load_application_environment($app, $env);
$sf_root_dir = sfConfig::get('sf_root_dir');
$directories = sfFinder::type('dir')->maxdepth(0)->ignore_version_control()->in(sfConfig::get('sf_model_lib_dir').'/doctrine');
Doctrine::exportSchema($directories);
pake_echo_action('doctrine', 'sql was inserted successfully');
return;
}
function run_doctrine_build_sql($task,$args)
{
if(count($args) < 1)
{
throw new Exception('You must provide your app name.');
}
$sf_root_dir = sfConfig::get('sf_root_dir');
define('SF_APP', $args[0]);
$connection = isset($args[1])?$args[1]:'all';
simpleAutoloader::registerCallable(array('Doctrine','autoload'));
sfConfig::set('sf_app_module_dir',$sf_root_dir.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR);
$doctrineSchemaPathScheme = DIRECTORY_SEPARATOR.'model'.DIRECTORY_SEPARATOR.'doctrine'.DIRECTORY_SEPARATOR;
$doctrineModelDir = sfConfig::get('sf_lib_dir').$doctrineSchemaPathScheme;
$generatedDir = $doctrineModelDir.'generated'.DIRECTORY_SEPARATOR;
$tmp_dir = $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
$db_connections = sfYaml::load($sf_root_dir.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'databases.yml');
if(!isset($db_connections[$connection]))
{
throw new sfException('Unable to find connection: '.$connection);
}
$connection = current($db_connections[$connection]);
$db = new sfDoctrineDatabase();
$db->initialize($connection['param']);
$directories = sfFinder::type('dir')->maxdepth(0)->ignore_version_control()->in(sfConfig::get('sf_model_lib_dir').'/doctrine');
foreach ($directories AS $directory)
{
$basename = basename($directory);
$name = $basename == 'generated' ? 'doctrine':'doctrine-'.$basename;
pake_echo_action("Building SQL", $name);
$sql = implode(";\n\n",Doctrine::exportSql($directory)).";\n";
$sql = str_replace(array(" (",") ",","),array("(\n ",")\n",",\n"),$sql);
if (!is_dir($sf_root_dir.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'sql'))
{
mkdir($sf_root_dir.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'sql');
}
$fd = fopen($sf_root_dir.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'sql'.DIRECTORY_SEPARATOR.$name.'.model.sql','w+');
fwrite($fd,$sql);
fclose($fd);
}
return;
}
function run_doctrine_build_model($task, $args)
{
$schemas = _doctrine_load('doctrine', 'yml', true);
$doctrineSchemaPathScheme = DIRECTORY_SEPARATOR.'model'.DIRECTORY_SEPARATOR.'doctrine'.DIRECTORY_SEPARATOR;
$doctrineModelDir = sfConfig::get('sf_lib_dir').$doctrineSchemaPathScheme;
$generatedDir = $doctrineModelDir.'generated'.DIRECTORY_SEPARATOR;
pake_mkdirs($generatedDir);
foreach($schemas as $db_schema)
{
foreach ($db_schema->getClasses() as $class)
{
foreach ($class->asPHP() as $cd)
{
$path = $doctrineModelDir;
$package = $class->getTable()->getPackage();
if ($package)
{
if (isset($cd['plugin']))
{
$path = sfConfig::get('sf_plugins_dir').DIRECTORY_SEPARATOR.$package.DIRECTORY_SEPARATOR.'lib'.$doctrineSchemaPathScheme;
}
else
{
$path.= $package.DIRECTORY_SEPARATOR;
}
}
if (isset($cd['overwrite']))
{
$path .= 'generated'.DIRECTORY_SEPARATOR;
}
pake_mkdirs($path);
$filePath = $cd['className'].'.class.php';
// we overwrite only the base classes
if (isset($cd['overwrite']) || !file_exists($path.$filePath))
{
pake_echo_action('writing', $filePath);
file_put_contents($path.$filePath, $cd['source']);
}
}
}
}
}
function run_doctrine_build_all($task, $args)
{
run_doctrine_drop_db($task, $args);
run_doctrine_build_db($task, $args);
run_doctrine_build_model($task, $args);
//run_doctrine_insert_sql($task, $args);
}
function run_doctrine_build_all_load($task, $args)
{
run_doctrine_build_all($task, $args);
run_doctrine_load_data($task, $args);
}
function run_doctrine_build_schema($task, $args)
{
// This will build schema from an existing database
throw new Exception('Not implemented.');
}
function run_doctrine_drop_db($task, $args)
{
if (!count($args))
{
throw new Exception('You must provide the app.');
}
$app = $args[0];
$env = empty($args[1]) ? 'dev' : $args[1];
_load_application_environment($app, $env);
$databases = sfYaml::load(sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'databases.yml');
$connectionBlockKey = $env;
if(!isset($databases[$connectionBlockKey]))
{
$connectionBlockKey = 'all';
}
$connections = $databases[$connectionBlockKey];
$manager = Doctrine_Manager::getInstance();
foreach ($connections AS $name => $info)
{
$dsnInfo = $manager->parseDsn($info['param']['dsn']);
$connection = $manager->getConnection($name);
try {
echo "Drop database '".$dsnInfo['database']."' are you sure Y/N ?";
$confirmation = strtolower(trim(fgets(STDIN)));
if ($confirmation!='y') {
pake_echo_action("cancelled");
exit(1);
}
pake_echo_action('doctrine', "dropping database '".$dsnInfo['database']."'");
$connection->export->dropDatabase($dsnInfo['database']);
} catch (Exception $e) {
pake_echo_action('doctrine', "could not drop database '".$dsnInfo['database']."'");
}
}
}
function run_doctrine_build_db($task, $args)
{
$connectionName = isset($args[0]) ? $args[0]:'all';
simpleAutoloader::registerCallable(array('Doctrine','autoload'));
$databases = sfYaml::load(sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'databases.yml');
if(!isset($databases[$connectionName]))
{
$connectionName = 'all';
}
$connections = $databases[$connectionName];
foreach($connections AS $name => $connection)
{
$dsn = $connection['param']['dsn'];
$info = Doctrine_Manager::getInstance()->parseDsn($dsn);
$dsn = $info['scheme'].':host='.$info['host'];
$user = $info['user'];
$password = $info['pass'];
$connection = Doctrine_Manager::getInstance()->openConnection(new PDO($dsn, $user, $password), $name.'2');
pake_echo_action('doctrine', "creating database '".$info['database']."'");
try {
$connection->export->createDatabase($info['database']);
} catch(Exception $e) {
pake_echo_action('doctrine', "could not create database '".$info['database']."'");
}
}
}
// FIXME: has to be rewritten to avoid code duplication
function run_doctrine_generate_crud($task,$args)
{
if (count($args) < 2)
{
throw new Exception('You must provide your module name.');
}
if (count($args) < 3)
{
throw new Exception('You must provide your model class name.');
}
$app = $args[0];
$module = $args[1];
$model_class = $args[2];
$theme = isset($args[3]) ? $args[3] : 'crud';
// function variables
$doctrineModelDir = sfConfig::get('sf_lib_dir').DIRECTORY_SEPARATOR.'model'.DIRECTORY_SEPARATOR.'doctrine'.DIRECTORY_SEPARATOR;
$sf_root_dir = sfConfig::get('sf_root_dir');
$sf_symfony_lib_dir = sfConfig::get('sf_symfony_lib_dir');
$pluginDir = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..');
$doctrineLibDir =$pluginDir.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'doctrine'.DIRECTORY_SEPARATOR.'Doctrine'.DIRECTORY_SEPARATOR;
$tmp_dir = $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.md5(uniqid(rand(), true));
sfConfig::set('sf_module_cache_dir', $tmp_dir);
sfConfig::set('sf_app_dir', $tmp_dir);
// add classes to autoload function
pake_echo_action('PluginDir', $pluginDir);
simpleAutoloader::registerCallable(array('Doctrine','autoload'));
// generate module
$generator_manager = new sfGeneratorManager();
$generator_manager->initialize();
$generator_manager->generate('sfDoctrineAdminGenerator', array('model_class' => $model_class, 'moduleName' => $module, 'theme' => $theme));
$moduleDir = $sf_root_dir.'/'.sfConfig::get('sf_apps_dir_name').'/'.$app.'/'.sfConfig::get('sf_app_module_dir_name').'/'.$module;
// copy our generated module
$finder = pakeFinder::type('any');
pake_mirror($finder, $tmp_dir.'/auto'.ucfirst($module), $moduleDir);
// change module name
pake_replace_tokens($moduleDir.'/actions/actions.class.php', getcwd(), '', '', array('auto'.ucfirst($module) => $module));
try
{
$author_name = $task->get_property('author', 'symfony');
}
catch (pakeException $e)
{
$author_name = 'Your name here';
}
$constants = array(
'PROJECT_NAME' => $task->get_property('name', 'symfony'),
'APP_NAME' => $app,
'MODULE_NAME' => $module,
'MODEL_CLASS' => $model_class,
'AUTHOR_NAME' => $author_name,
);
// customize php files
$finder = pakeFinder::type('file')->name('*.php');
pake_replace_tokens($finder, $moduleDir, '##', '##', $constants);
// delete temp files
$finder = pakeFinder::type('any');
pake_remove($finder, $tmp_dir);
// for some reason the above does not remove the tmp dir as it should.
// delete temp dir
@rmdir($tmp_dir);
// delete cache/tmp
@rmdir(sfConfig::get('sf_cache_dir').'tmp');
}
// FIXME: has to be rewritten to avoid code duplication
function run_doctrine_init_admin($task, $args)
{
if (count($args) < 2)
{
throw new Exception('You must provide your module name.');
}
if (count($args) < 3)
{
throw new Exception('You must provide your model class name.');
}
$app = $args[0];
$module = $args[1];
$model_class = $args[2];
$theme = isset($args[3]) ? $args[3] : 'default';
try
{
$author_name = $task->get_property('author', 'symfony');
}
catch (pakeException $e)
{
$author_name = 'Your name here';
}
$constants = array(
'PROJECT_NAME' => $task->get_property('name', 'symfony'),
'APP_NAME' => $app,
'MODULE_NAME' => $module,
'MODEL_CLASS' => $model_class,
'AUTHOR_NAME' => $author_name,
'THEME' => $theme,
);
$moduleDir = sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR.sfConfig::get('sf_apps_dir_name').DIRECTORY_SEPARATOR.$app.DIRECTORY_SEPARATOR.sfConfig::get('sf_app_module_dir_name').DIRECTORY_SEPARATOR.$module;
// create module structure
$finder = pakeFinder::type('any')->ignore_version_control()->discard('.sf');
$dirs = sfLoader::getGeneratorSkeletonDirs('sfDoctrineAdmin', $theme);
foreach($dirs as $dir)
{
echo $dir;
if(is_dir($dir))
{
pake_mirror($finder, $dir, $moduleDir);
break;
}
}
// customize php and yml files
$finder = pakeFinder::type('file')->name('*.php', '*.yml');
pake_replace_tokens($finder, $moduleDir, '##', '##', $constants);
}
/**
* run_doctrine_load_nested_set
*
* @param mixed $task
* @param mixed $args
* @access public
* @return void
*/
function run_doctrine_load_nested_set($task, $args)
{
if (!count($args))
{
throw new Exception('You must provide the app.');
}
$app = $args[0];
if (!is_dir(sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.$app))
{
throw new Exception('The app "'.$app.'" does not exist.');
}
if (!isset($args[1]))
{
throw new Exception('You must provide a filename.');
}
$filename = $args[1];
$env = empty($args[2]) ? 'dev' : $args[2];
_load_application_environment($app, $env);
$model = sfInflector::classify($args[1]);
$ymlName = sfInflector::tableize($args[1]);
$ymlPath = sfConfig::get('sf_data_dir').'/'.$ymlName.'.yml';
pake_echo_action('doctrine', 'loading nested set data for '.$model);
pake_echo_action('doctrine', 'loading '.$ymlPath);
$nestedSetData = sfYaml::load($ymlPath);
_doctrine_load_nested_set_data($model, $nestedSetData);
}
/**
* run_doctrine_dump_data
*
* @param mixed $task
* @param mixed $args
* @access public
* @return void
*/
function run_doctrine_dump_data($task, $args)
{
if (!count($args))
{
throw new Exception('You must provide the app.');
}
$app = $args[0];
if (!is_dir(sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.$app))
{
throw new Exception('The app "'.$app.'" does not exist.');
}
if (!isset($args[1]))
{
throw new Exception('You must provide a filename.');
}
$filename = $args[1];
$env = empty($args[2]) ? 'dev' : $args[2];
_load_application_environment($app, $env);
if (!sfToolkit::isPathAbsolute($filename))
{
$dir = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'fixtures';
pake_mkdirs($dir);
$filename = $dir.DIRECTORY_SEPARATOR.$filename;
}
pake_echo_action('doctrine', sprintf('dumping data to "%s"', $filename));
$data = new sfDoctrineData();
$data->dumpData($filename);
}
/**
* _doctrine_load_nested_set_data
*
* @param mixed $model
* @param mixed $nestedSetData
* @param mixed $parent
* @access protected
* @return void
*/
function _doctrine_load_nested_set_data($model, $nestedSetData, $parent = null)
{
$manager = Doctrine_Manager::getInstance();
foreach($nestedSetData AS $name => $data)
{
$children = array();
$setters = array();
if( array_key_exists('children', $data) )
{
$children = $data['children'];
unset($data['children']);
}
if( array_key_exists('setters', $data) )
{
$setters = $data['setters'];
unset($data['setters']);
}
$record = new $model();
if( is_array($setters) AND !empty($setters) )
{
foreach($setters AS $key => $value)
{
$record->set($key, $value);
}
}
if( !$parent )
{
$manager->getTable($model)->getTree()->createRoot($record);
} else {
$parent->getNode()->addChild($record);
}
pake_echo_action('doctrine', 'loading '.str_repeat(' ', $record->getNode()->getLevel()).$name);
if( is_array($children) AND !empty($children) )
{
_doctrine_load_nested_set_data($model, $children, $record);
}
}
}
function _findPropelSchemas($type)
{
$preGlob = '*schema';
$root = 'config';
$extension = '.'.$type;
$schemas = pakeFinder::type('file')->name($preGlob.$extension)->in($root);
$schemasToLoad = array();
foreach ($schemas as $schema)
{
// we store the name of the file as "package"
$schemasToLoad[$schema] = basename($schema, $extension);
}
return $schemasToLoad;
}
function _findDoctrineSchemas()
{
$schemasToLoad = array();
// first we try with a connection mapping config file
$connectionMappingPath = 'config/schemas.yml';
if (file_exists($connectionMappingPath))
{
$connectionMapping = sfYaml::load($connectionMappingPath);
foreach ($connectionMapping as $connection => $schemas)
{
foreach ($schemas as $schema)
{
$components = explode('/', $schema);
$name = array_pop($components);
$schemaPath = 'config/doctrine/'.$name.'.yml';
if (!empty($components))
{
$packageName = $components[0];
$schemaPath = sfConfig::get('sf_plugins_dir').DIRECTORY_SEPARATOR.$packageName.DIRECTORY_SEPARATOR.$schemaPath;
}
else
$packageName = null;
if (file_exists($schemaPath))
{
$schemasToLoad[$schemaPath] = $packageName;
}
}
}
}
else // otherwise we load all the schemas in the doctrine directories
{
$preGlob = '*';
$root = 'config'.DIRECTORY_SEPARATOR.'doctrine';
$schemas = pakeFinder::type('file')->name($preGlob.'.yml')->in($root);
if(count($schemas))
$schemas = array_combine($schemas, array_fill(0, count($schemas), null));
// adding the plugin schemas
$pluginSchemas = array();
$pluginSchemas = pakeFinder::type('file')->name($preGlob.'.yml')->in(glob('plugins/*/'.$root));
$schemasToLoad = array();
foreach ($pluginSchemas as $pluginSchema)
{
// we get the plugin name from the path file; not very elegant...
$pluginName = basename(substr(dirname($pluginSchema), 0, -strlen($root)));
$schemasToLoad[$pluginSchema] = $pluginName;
}
$schemasToLoad = array_merge($schemas, $schemasToLoad);
}
return $schemasToLoad;
}
function _doctrine_load($mode, $type, $aggregate)
{
$schemasToLoad = array();
if ($mode == 'doctrine')
{
$schemasToLoad = _findDoctrineSchemas();
}
elseif ($mode == 'propel')
{
$schemasToLoad = _findPropelSchemas($type);
}
if (!count($schemasToLoad))
{
throw new Exception('No schemas were found');
}
$dbSchemas = array();
// schema loader class
$schemaClass = 'sfDoctrineSchema'.ucfirst($mode).'Loader';
$db_schema = new $schemaClass();
$db_schemas = array();
foreach ($schemasToLoad as $schema => $package)
{
if (!$aggregate)
{
$db_schema = new $schemaClass();
}
$relativeSchema = substr($schema, strlen(sfConfig::get('sf_root_dir'))+1);
pake_echo_action('loading', 'Class descriptions from "'.$schema.'"');
$db_schema->load($schema, $package);
if (!$aggregate)
{
$db_schema->process();
$db_schemas[] = $db_schema;
}
}
if ($aggregate)
{
$db_schema->process();
$db_schemas = array($db_schema);
}
return $db_schemas;
}
function _load_application_environment($app, $env)
{
// define constants
define('SF_ROOT_DIR', sfConfig::get('sf_root_dir'));
define('SF_APP', $app);
define('SF_ENVIRONMENT', $env);
define('SF_DEBUG', true);
// get configuration
require_once SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php';
sfContext::getInstance();
}
../../../../lib/
\ No newline at end of file
<?php
use_helper('ObjectAdmin');
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* ObjectHelper for doctrine admin generator.
*
* @package symfony
* @subpackage helper
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: ObjectDoctrineAdminHelper.php 3339 2007-01-28 09:47:52Z chtito $
*/
function object_doctrine_admin_double_list($object, $method, $options = array())
{
return object_admin_double_list($object, $method, $options, '_get_doctrine_object_list');
}
function object_doctrine_admin_select_list($object, $method, $options = array())
{
return object_admin_select_list($object, $method, $options, '_get_doctrine_object_list');
}
function object_doctrine_admin_check_list($object, $method, $options = array())
{
return object_admin_check_list($object, $method, $options, '_get_doctrine_object_list');
}
function _get_doctrine_object_list($object, $method, $options)
{
$foreignTable = $object->getTable()->getRelation($method[1][0])->getTable();
$foreignClass = $foreignTable->getComponentName();
$allObjects = $foreignTable->findAll();
$associatedObjects = $object->get($method[1][0]);
$ids = array();
foreach ($associatedObjects as $associatedObject)
{
$ids[] = $associatedObject->obtainIdentifier();
}
// fix due to the fact that an empty doctrine collection $c (count == 0)
// will return true on: if ($c)
// if (!empty($c)) won't work either...
// the if($c) test is in _get_options_from_objects
if (count($associatedObjects) == 0)
$associatedObjects = null;
return array($allObjects, $associatedObjects, $ids);
}
function object_enum_tag($object, $method, $options)
{
$enumValues = _get_option($options, 'enumValues', array());
$currentValue = _get_object_value($object, $method);
$enumValues = array_combine($enumValues, $enumValues);
return select_tag(_convert_method_to_name($method, $options), options_for_select($enumValues, $currentValue), $options);
}
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @version SVN: $Id: sfDoctrine.class.php 4092 2007-05-23 17:37:26Z chtito $
*/
class sfDoctrine
{
// uses the default connection if none is given
static public function connection($connection = null)
{
if ($connection === null)
{
return Doctrine_Manager::getInstance()->getCurrentConnection();
}
return Doctrine_Manager::getInstance()->getConnection($connection);
}
// returns either the connection connectionName or uses the doctrine manager
// to find out the connection bound to the class (or the current one)
public static function connectionForClass($className, $connectionName = null)
{
if (isset($connectionName))
{
return Doctrine_Manager::getInstance()->getConnection($connectionName);
}
return Doctrine_Manager::getInstance()->getConnectionForComponent($className);
}
public static function getTable($className)
{
return Doctrine_Manager::getInstance()->getTable($className);
}
public static function queryFrom($className)
{
sfContext::getInstance()->getLogger()->err('The sfDoctrine::queryFrom() method is deprecated; use "Doctrine_Query::create()->from($className)" instead.');
return self::getTable($className)->createQuery();
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineAdminGenerator.class.php 4533 2007-07-03 23:36:10Z gnat $
*/
class sfDoctrineAdminGenerator extends sfAdminGenerator
{
protected $table;
public function initialize($generatorManager)
{
// otherwise the class never gets loaded... don't ask me why...
include_once(sfConfig::get('sf_symfony_lib_dir').'/vendor/creole/CreoleTypes.php');
parent::initialize($generatorManager);
$this->setGeneratorClass('sfDoctrineAdmin');
}
protected function loadMapBuilderClasses()
{
$conn = Doctrine_Manager::getInstance()->openConnection('mock://no-one@localhost/empty', null, false);
$this->table = $conn->getTable($this->getClassName());
}
protected function getTable()
{
return $this->table;
}
protected function loadPrimaryKeys()
{
foreach ($this->getTable()->getPrimaryKeys() as $primaryKey)
$this->primaryKey[] = new sfDoctrineAdminColumn($primaryKey);
// FIXME: check that there is at least one primary key
}
public function getColumns($paramName, $category='NONE')
{
$columns = parent::getColumns($paramName, $category);
// set the foreign key indicator
$relations = $this->getTable()->getRelations();
$cols = $this->getTable()->getColumns();
foreach ($columns as $index => $column)
{
if (isset($relations[$column->getName()]))
{
$fkcolumn = $relations[$column->getName()];
$columnName = $relations[$column->getName()]->getLocal();
if ($columnName != 'id') // i don't know why this is necessary
{
$column->setRelatedClassName($fkcolumn->getTable()->getComponentName());
$column->setColumnName($columnName);
if (isset($cols[$columnName])) // if it is not a many2many
$column->setColumnInfo($cols[$columnName]);
$columns[$index] = $column;
}
}
}
return $columns;
}
function getAllColumns()
{
$cols = $this->getTable()->getColumns();
$rels = $this->getTable()->getRelations();
$columns = array();
foreach ($cols as $name => $col)
{
// we set out to replace the foreign key to their corresponding aliases
$found = null;
foreach ($rels as $alias=>$rel)
{
$relType = $rel->getType();
if ($rel->getLocal() == $name && $relType != Doctrine_Relation::MANY_AGGREGATE && $relType != Doctrine_Relation::MANY_COMPOSITE)
$found = $alias;
}
if ($found)
{
$name = $found;
}
$columns[] = new sfDoctrineAdminColumn($name, $col);
}
return $columns;
}
function getAdminColumnForField($field, $flag = null)
{
$cols = $this->getTable()->getColumns(); // put this in an internal variable?
return new sfDoctrineAdminColumn($field, (isset($cols[$field]) ? $cols[$field] : null), $flag);
}
function getPHPObjectHelper($helperName, $column, $params, $localParams = array())
{
$params = $this->getObjectTagParams($params, $localParams);
// special treatment for object_select_tag:
if ($helperName == 'select_tag')
{
$column = new sfDoctrineAdminColumn($column->getColumnName(), null, null);
}
return sprintf ('object_%s($%s, %s, %s)', $helperName, $this->getSingularName(), var_export($this->getColumnGetter($column), true), $params);
}
function getColumnGetter($column, $developed = false, $prefix = '')
{
if ($developed)
return sprintf("$%s%s->get('%s')", $prefix, $this->getSingularName(), $column->getName());
// no parenthesis, we return a method+parameters array
return array('get', array($column->getName()));
}
function getColumnSetter($column, $value, $singleQuotes = false, $prefix = 'this->')
{
if ($singleQuotes)
$value = sprintf("'%s'", $value);
return sprintf('$%s%s->set(\'%s\', %s)', $prefix, $this->getSingularName(), $column->getName(), $value);
}
function getRelatedClassName($column)
{
return $column->getRelatedClassName();
}
public function getColumnEditTag($column, $params = array())
{
if ($column->getDoctrineType() == 'enum')
{
// FIXME: this is called already in the sfAdminGenerator class!!!
$params = array_merge(array('control_name' => $this->getSingularName().'['.$column->getName().']'), $params);
$values = $this->getTable()->getEnumValues($column->getName());
$params = array_merge(array('enumValues'=>$values), $params);
return $this->getPHPObjectHelper('enum_tag', $column, $params);
}
return parent::getColumnEditTag($column, $params);
}
}
class sfDoctrineAdminColumn extends sfAdminColumn
{
// doctrine to creole type conversion
static $docToCreole = array(
'boolean' => CreoleTypes::BOOLEAN,
'string' => CreoleTypes::TEXT,
'integer' => CreoleTypes::INTEGER,
'date' => CreoleTypes::DATE,
'timestamp' => CreoleTypes::TIMESTAMP,
'time' => CreoleTypes::TIME,
'enum' => CreoleTypes::TINYINT,
'float' => CreoleTypes::FLOAT,
'double' => CreoleTypes::FLOAT,
'clob' => CreoleTypes::CLOB,
'blob' => CreoleTypes::BLOB,
'object' => CreoleTypes::VARCHAR,
'array' => CreoleTypes::VARCHAR,
'decimal' => CreoleTypes::DECIMAL,
);
protected $relatedClassName = null;
protected $name = null;
protected $columnName; // stores the real foreign id column
function getDoctrineType()
{
return isset($this->column['type']) ? $this->column['type'] : null;
}
function getCreoleType()
{
$dType = $this->getDoctrineType();
// we simulate the CHAR/VARCHAR types to generate input_tags
if(($dType == 'string') and ($this->getSize() < 256))
{
return CreoleTypes::VARCHAR;
}
return $dType ? self::$docToCreole[$dType] : CreoleTypes::OTHER;
}
function getSize()
{
return $this->column['length'];
}
function isNotNull()
{
//FIXME THIS NEEDS TO BE UPDATE-but I don't know the format for the column array
if (isset($this->column[2]['notnull']))
return $this->column[2]['notnull'];
return false;
}
function isPrimaryKey()
{
if (isset($this->column['primary']))
return $this->column['primary'];
return false;
}
function setRelatedClassName($newName)
{
$this->relatedClassName = $newName;
}
function getRelatedClassName()
{
return $this->relatedClassName;
}
function setColumnName($newName)
{
$this->columnName = $newName;
}
function getColumnName()
{
return $this->columnName;
}
function setColumnInfo($col)
{
$this->column = $col;
}
// FIXME: this method is never used... remove it?
function setName($newName)
{
$this->name = $newName;
}
function getName()
{
if (isset($this->name))
{
return $this->name;
}
// a bit kludgy: the field name is actually in $this->phpName
return parent::getPhpName();
}
function isForeignKey()
{
return isset($this->relatedClassName);
}
// all the calls that were forwarded to the table object with propel
// have to be dealt with explicitly here, otherwise:
public function __call($name, $arguments)
{
throw new Exception(sprintf('Unhandled call: "%s"', $name));
}
}
<?php
/**
*
* sfDoctrineConfigHandler parses the PHPDoctrine config file
*
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Amadeus
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @author Dan Porter
* @version SVN: $Id: sfDoctrineConfigHandler.class.php 4132 2007-05-31 19:50:01Z gnat $
*/
class sfDoctrineConfigHandler extends sfYamlConfigHandler
{
public function execute($configFiles)
{
// Parse yaml config files
$configs = $this->parseYamls($configFiles);
// Default config: all.attributes
$default_config = array ();
if (isset ($configs['all']['attributes']) && is_array($configs['all']['attributes']))
{
$default_config = $configs['all']['attributes'];
unset ($configs['all']['attributes']);
}
// Environment specific defaults: <env>.attributes
$env = sfConfig::get('sf_environment');
if (isset ($configs[$env]['attributes']) && is_array($configs[$env]['attributes']))
{
$default_config = sfToolKit::arrayDeepMerge($default_config, $configs[$env]['attributes']);
unset ($configs[$env]['attributes']);
}
// Connection specific configs
$conn_configs = array();
foreach ($configs as $env => $env_config)
{
foreach ($env_config as $conn => $conn_config)
{
$conn_configs[$conn] = sfToolkit::arrayDeepMerge($default_config, $conn_config);
}
}
// Prepare default config data
$data = array();
foreach ($this->configToAttributes($default_config) as $key => $value)
{
$data[] = sprintf('$default_attributes["%s"] = %s;', $key, $this->attributeToPhp($value));
}
$data[] = '';
// Prepare connection specific data
foreach ($conn_configs as $conn_name => $conn_config)
{
foreach ($this->configToAttributes($conn_config) as $key => $value)
{
$data[] = sprintf('$attributes["%s"]["%s"] = %s;', $conn_name, $key, $this->attributeToPHP($value));
}
$data[] = '';
}
// compile data
$retval = sprintf("<?php\n" .
"// auto-generated by sfDoctrineConfigHandler\n" .
"// date: %s\n%s\n", date('Y-m-d H:i:s'), implode("\n", $data));
return $retval;
}
protected function configToAttributes($config)
{
$attributes = array();
foreach ($config as $key => $value)
{
$attr_key = 'ATTR_' . strtoupper($key);
switch ($key)
{
// event listener (name of the listener class)
case 'listener':
$attributes[$attr_key] = array ('php', "new $value()");
break;
// fetch mode (immediate, batch, offset, lazy_offset)
case 'fetchmode':
$attributes[$attr_key] = array ('constant', 'FETCH_' . strtoupper($value));
break;
// locking (optimistic, pessimistic)
case 'lockmode':
$attributes[$attr_key] = array ('constant', 'LOCK_' . strtoupper($value));
break;
// export (none, tables, constraints, all)
case 'export':
$attributes[$attr_key] = array('constant', 'EXPORT_'.strtoupper($value));
break;
// accessors (none, get, set, both)
/* case 'accessors':
$attributes[$attr_key] = array ('constant', 'ACCESSOR_' . strtoupper($value));
break;
*/
// portability (none, fix_case, rtrim, delete_count, empty_to_null, fix_assoc_field_names, all)
case 'portability':
$attributes[$attr_key] = array ('constant', 'PORTABILITY_' . strtoupper($value));
break;
// the default will set the value as a string or a boolean (depending on the type returned by the yaml parser)
default:
$attributes[$attr_key] = $value;
}
}
return $attributes;
}
protected function attributeToPhp($attr)
{
if (is_array($attr))
{
if ($attr[0] == 'constant')
{
$attr = 'Doctrine::' . $attr[1];
}
elseif ($attr[0] == 'php')
{
$attr = $attr[1];
}
}
elseif (is_string($attr))
{
$attr = sprintf("'%s'", $attr);
}
else
{
$attr = var_export($attr, 1);
}
return $attr;
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id$
*/
class sfDoctrineConnectionListener extends Doctrine_EventListener
{
public function __construct($connection, $encoding)
{
$this->connection = $connection;
$this->encoding = $encoding;
}
public function postConnect(Doctrine_Event $event)
{
$this->connection->setCharset($this->encoding);
$this->connection->setDateFormat();
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package sfDoctrinePlugin
* @subpackage sfDoctrineData
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineData.class.php 4493 2007-06-30 00:47:02Z Jonathan.Wage $
*/
class sfDoctrineData extends sfData
{
/**
* connectionName
*
* @var mixed
* @access protected
*/
protected $connectionName = null;
/**
* loadData
*
* @param mixed $directory_or_file
* @param mixed $connectionName
* @access public
* @return void
*/
public function loadData($directory_or_file = null, $connectionName = null)
{
$this->connectionName = $connectionName;
$fixture_files = $this->getFiles($directory_or_file);
// wrap all database operations in a single transaction
$con = sfDoctrine::connection($connectionName);
try
{
$con->beginTransaction();
$this->doLoadData($fixture_files);
$con->commit();
}
catch (Exception $e)
{
$con->rollback();
throw $e;
}
}
/**
* loadDataFromArray
*
* @param mixed $data
* @access public
* @return void
*/
public function loadDataFromArray($data)
{
$pendingRelations = array();
if ($data === null)
{
// no data
return;
}
// only for pake_echo_action
require_once(sfConfig::get('sf_symfony_lib_dir').'/vendor/pake/pakeFunction.php');
foreach ($data as $class => $entries)
{
pake_echo_action('Filling', sprintf('class "%s"', $class)."\t");
// fetch a table object
$table = sfDoctrine::getTable($class, $this->connectionName);
$colNames = array_keys($table->getColumns());
$tableName = $table->getTableName();
// relation fields
$relations = $table->getRelations();
//echo "Class $class: ".implode(', ', array_keys($relations))."\n";
if ($this->deleteCurrentData)
{
$q = new Doctrine_Query();
$q->delete()->from($class);
$q->execute();
}
// iterate through entries for this class
// might have been empty just for force a table to be emptied on import
if (is_array($entries))
{
foreach ($entries as $key => $columnAssignments)
{
// create a new entry in the database
$obj = $table->create();
$now = date("Y-m-d H:i:s", time());
if($obj->getTable()->hasColumn('created_at'))
{
$obj->set('created_at', $now);
}
if (!is_array($columnAssignments))
{
throw new Exception('You must give a name for each fixture data entry');
}
foreach ($columnAssignments as $name => $value)
{
$isRelation = isset($relations[$name]);
// foreign key?
if ($isRelation)
{
$rel = $relations[$name];
// $relatedTable = $rel->getTable()->getTableName();
$localKey = $rel->getLocal();
$foreignKey = $rel->getForeign();
$pendingRelations[] = array($obj, $localKey, $foreignKey, $value);
}
else
{
// first check that the column exists
if (!in_array($name, $colNames))
{
$error = 'Column "%s" does not exist for class "%s"';
$error = sprintf($error, $name, $class);
throw new sfException($error);
}
$obj->rawSet($name, $value);
}
}
$obj->save();
// For progress meter
echo '.';
// save the id for future reference
$pk = $obj->obtainIdentifier();
if (isset($this->object_references[$key]))
{
throw new sfException(sprintf('The key "%s" is not unique', $key));
}
$this->object_references[$key] = $pk;
}
}
echo "\n";
}
// now we take care of the pending relations
foreach ($pendingRelations as $pending)
{
list($obj, $localKey, $foreignKey, $key) = $pending;
if (!isset($this->object_references[$key]))
{
$error = 'No object with key "%s" is defined in your data file';
$error = sprintf($error, $key);
throw new sfException($error);
}
$foreignId = $this->object_references[$key][$foreignKey];
$obj->rawSet($localKey, $foreignId);
$obj->save();
}
}
/**
* loadMapBuilder
*
* @param mixed $class
* @access protected
* @return void
*/
protected function loadMapBuilder($class)
{
$class_map_builder = $class.'MapBuilder';
if (!isset($this->maps[$class]))
{
if (!$classPath = sfCore::getClassPath($class_map_builder))
{
throw new sfException(sprintf('Unable to find path for class "%s".', $class_map_builder));
}
require_once($classPath);
$this->maps[$class] = new $class_map_builder();
$this->maps[$class]->doBuild();
}
}
/**
* dumpData
*
* @param mixed $directory_or_file
* @param string $tables
* @param string $connectionName
* @access public
* @return void
*/
public function dumpData($directory_or_file = null, $tables = 'all', $connectionName = 'propel')
{
$sameFile = true;
if (is_dir($directory_or_file))
{
// multi files
$sameFile = false;
}
else
{
// same file
// delete file
}
$manager = Doctrine_Manager::getInstance();
$con = $manager->getCurrentConnection();
// get tables
if ('all' === $tables || null === $tables)
{
$modelDirectories = array();
$modelDirectories[] = sfConfig::get('sf_model_lib_dir').'/doctrine';
$directories = sfFinder::type('dir')->maxdepth(0)->in(sfConfig::get('sf_model_lib_dir').'/doctrine');
foreach($directories AS $directory)
{
if( strstr($directory, 'generated') )
{
continue;
}
$modelDirectories[] = $directory;
}
$tables = array();
foreach($modelDirectories AS $directory)
{
$dirTables = sfFinder::type('file')->name('/(?<!Table)\.class.php$/')->maxdepth(0)->in($directory);
foreach ($dirTables AS $key => $table)
{
$table = basename($table, '.class.php');
$tables[] = $table;
}
}
}
else if (!is_array($tables))
{
$tables = array($tables);
}
$dumpData = array();
foreach ($tables as $modelName)
{
$table = sfDoctrine::getTable($modelName, $this->connectionName);
// get table name
$tableName = $table->getTableName();
$relations = $table->getRelations();
// get columns
$columns = $con->fetchAll('DESCRIBE '.$tableName);
// get records
//$records = $con->fetchAll('SELECT * FROM '.$tableName);
$query = new Doctrine_Query();
$query->from($modelName);
$records = $query->execute();
$dumpData[$modelName] = array();
foreach($records AS $record)
{
$pk = $modelName;
$values = array();
foreach($columns AS $column)
{
$col = strtolower($column['Field']);
try {
$initialValue = $record[$col];
} catch(Exception $e) {
continue;
}
if( !$initialValue )
{
continue;
}
if ($column['Key'] == 'PRI')
{
$pk .= '_'.$initialValue;
}
else
{
$isForeignKey = false;
foreach($relations AS $relation)
{
if( $relation->getLocal() == $col )
{
$isForeignKey = true;
break;
}
}
if( $isForeignKey )
{
$array = $relation->toArray();
$values[$relation->getAlias()] = $array['class'].'_'.$initialValue;
} else {
$value = $initialValue;
// Needed to maintain bool values
if( is_bool($value) )
{
$value = $value ? 1:0;
}
$values[$col] = $value;
}
}
}
$dumpData[$modelName][$pk] = $values;
}
}
// save to file(s)
if ($sameFile)
{
$yaml = Spyc::YAMLDump($dumpData);
file_put_contents($directory_or_file, $yaml);
}
else
{
foreach ($dumpData as $table => $data)
{
$yaml = Spyc::YAMLDump($data);
file_put_contents($directory_or_file."/$table.yml", $yaml);
}
}
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineDataRetriever.class.php 3437 2007-02-10 09:04:27Z chtito $
*/
class sfDoctrineDataRetriever
{
static public function retrieveObjects($class, $peer_method = 'findAll')
{
if (!$peer_method)
$peer_method = 'findAll';
$table = sfDoctrine::getTable($class);
return call_user_func(array($table, $peer_method));
}
}
\ No newline at end of file
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004-2006 Sean Kerr.
* (c) 2006-2007 Olivier Verdier <olivier.verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfDoctrineDatabase provides connectivity for the Doctrine.
*
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Maarten den Braber <mdb@twister.cx>
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @author Dan Porter
* @version SVN: $Id: sfDoctrineDatabase.class.php 4394 2007-06-25 17:59:38Z subzero2000 $
*/
class sfDoctrineDatabase extends sfDatabase
{
protected
$doctrineConnection = null;
public function initialize($parameters = array(), $name = null)
{
parent::initialize($parameters);
// if a default connection is defined we only open that one
if ($defaultDatabase = sfConfig::get('sf_default_database'))
{
if ($name != $defaultDatabase)
{
return;
}
}
// load doctrine config
require(sfConfigCache::getInstance()->checkConfig('config/doctrine.yml'));
$db_attributes = $default_attributes;
if (isset($attributes[$name]))
{
$db_attributes = array_merge($default_attributes, $attributes[$name]);
}
$this->setParameter('attributes', $db_attributes);
$this->setParameter('name', $name);
// take care of the component binding
// suppress errors from include_once
// because config/schemas.yml is optional.
@include_once(sfConfigCache::getInstance()->checkConfig('config/schemas.yml', true));
// opening the doctrine connection
// determine how to get our parameters
$method = $this->getParameter('method', 'dsn');
// get parameters
switch ($method)
{
case 'dsn':
$dsn = $this->getParameter('dsn');
if ($dsn == null)
{
// missing required dsn parameter
$error = 'Database configuration specifies method "dsn", but is missing dsn parameter';
throw new sfDatabaseException($error);
}
break;
}
try
{
// Make sure we pass non-PEAR style DSNs as an array
if ( ! strpos($dsn, '://'))
{
$dsn = array($dsn, $this->getParameter('username'), $this->getParameter('password'));
}
$this->doctrineConnection = Doctrine_Manager::connection($dsn, $name);
// figure out the encoding
$encoding = $this->getParameter('encoding', 'UTF8');
// set up the connection parameters from the doctrine.yml config file
foreach($this->getParameter('attributes') as $k => $v)
{
$this->doctrineConnection->setAttribute(constant('Doctrine::'.$k), $v);
}
// we add the listener that sets up encoding and date formats
$eventListener = new sfDoctrineConnectionListener($this->doctrineConnection, $encoding);
$this->doctrineConnection->addListener($eventListener);
// add the query logger
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
{
$this->doctrineConnection->addListener(new sfDoctrineQueryLogger());
}
}
catch (PDOException $e)
{
throw new sfDatabaseException($e->getMessage());
}
}
/**
* Connect to the database.
* Stores the PDO connection in $connection
*
*/
public function connect ()
{
$dbh = $this->doctrineConnection->getDbh();
$dbh->connect();
$this->connection = $dbh->getDbh();
}
/**
* Execute the shutdown procedure.
*
* @return void
*
* @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database.
*/
public function shutdown ()
{
if ($this->connection !== null)
{
@$this->connection = null;
}
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Jan Schaefer <jschaefe@sinntax.de>
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineEventListener.class.php 4416 2007-06-26 21:54:15Z subzero2000 $
*/
class sfDoctrineEventListener extends Doctrine_EventListener
{
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineException.class.php 2675 2006-11-14 07:00:59Z chtito $
*/
class sfDoctrineException extends sfException
{
/**
* Class constructor.
*
* @param string The error message.
* @param int The error code.
*/
public function __construct ($message = null, $code = 0)
{
$this->setName('sfDoctrineException');
parent::__construct($message, $code);
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Maarten den Braber <mdb@twister.cx>
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @author Ian P. Christian <pookey@pookey.co.uk>
* @version SVN: $Id: sfDoctrinePager.class.php 4401 2007-06-26 05:02:41Z hansbrix $
*/
class sfDoctrinePager extends sfPager implements Serializable
{
protected
$query;
public function __construct($class, $defaultMaxPerPage = 10)
{
parent::__construct($class, $defaultMaxPerPage);
$this->setQuery(Doctrine_Query::create()->from($class));
}
public function serialize()
{
$vars = get_object_vars($this);
unset($vars['query']);
return serialize($vars);
}
public function unserialize($serialized)
{
$array = unserialize($serialized);
foreach($array as $name => $values) {
$this->$name = $values;
}
}
public function init()
{
$count = $this->getQuery()->offset(0)->limit(0)->count();
$this->setNbResults($count);
$p = $this->getQuery();
$p->offset(0);
$p->limit(0);
if ($this->getPage() == 0 || $this->getMaxPerPage() == 0)
{
$this->setLastPage(0);
}
else
{
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();
$p->offset($offset);
$p->limit($this->getMaxPerPage());
}
}
public function getQuery()
{
return $this->query;
}
public function setQuery($query)
{
$this->query = $query;
}
protected function retrieveObject($offset)
{
$cForRetrieve = clone $this->getQuery();
$cForRetrieve->offset($offset - 1);
$cForRetrieve->limit(1);
$results = $cForRetrieve->execute();
return $results[0];
}
public function getResults($fetchtype = null)
{
$p = $this->getQuery();
if ($fetchtype == 'array')
return $p->execute(array(), Doctrine::FETCH_ARRAY);
return $p->execute();
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineQueryLogger.class.php 4728 2007-07-27 10:42:49Z mahono $
*/
class sfDoctrineQueryLogger extends Doctrine_EventListener
{
protected $connection = null;
protected $encoding = 'UTF8';
public function preExecute(Doctrine_Event $event)
{
$this->sfLogQuery('{sfDoctrine Execute} executeQuery : ', $event);
}
public function postExecute(Doctrine_Event $event)
{
$this->sfAddTime();
}
public function postPrepare(Doctrine_Event $event)
{
$this->sfAddTime();
}
public function preStmtExecute(Doctrine_Event $event)
{
$this->sfLogQuery('{sfDoctrine Statement} executeQuery : ', $event);
}
public function postStmtExecute(Doctrine_Event $event)
{
$this->sfAddTime();
}
public function preQuery(Doctrine_Event $event)
{
$this->sfLogQuery('{sfDoctrine Query} executeQuery : ', $event);
}
public function postQuery(Doctrine_Event $event)
{
$this->sfAddTime();
}
protected function sfLogQuery($message, $event)
{
$message .= $event->getQuery();
if ($params = $event->getParams())
{
$message .= ' - ('.implode(', ', $params) . ' )';
}
sfContext::getInstance()->getLogger()->log($message);
$sqlTimer = sfTimerManager::getTimer('Database (Doctrine)');
}
protected function sfAddTime()
{
sfTimerManager::getTimer('Database (Doctrine)')->addTime();
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineRecord.class.php 5050 2007-09-12 16:13:53Z Jonathan.Wage $
*/
class sfDoctrineRecord extends Doctrine_Record
{
public function __toString()
{
// if the current object doesn't exist we return nothing
if (!$this->exists())
{
return '-';
}
// we try to guess a column which would give a good description of the object
foreach (array('name', 'title', 'description', 'id') as $descriptionColumn)
{
if ($this->getTable()->hasColumn($descriptionColumn))
{
return $this->get($descriptionColumn);
}
}
return sprintf('No description for object of class "%s"', $this->getTable()->getComponentName());
}
// FIXME: All code should be updated to use the new identifier() method directly
public function obtainIdentifier()
{
return $this->identifier();
}
public function set($name, $value, $load = true)
{
// ucfirst() is used instead of camelize() to be compatible with older version of Doctrine
$filterMethod = 'filterSet'.ucfirst($name);
if (method_exists($this, $filterMethod))
{
$value = $this->$filterMethod($value);
}
$setterMethod = 'set'.sfInflector::camelize($name);
return method_exists($this, $setterMethod) ? $this->$setterMethod($value) : parent::set($name, $value, $load);
}
public function get($name, $load = true)
{
$getterMethod = 'get'.sfInflector::camelize($name);
$value = method_exists($this, $getterMethod) ? $this->$getterMethod() : parent::get($name, $load);
// ucfirst() is used instead of camelize() to be compatible with older version of Doctrine
$filterMethod = 'filterGet'.ucfirst($name);
if (method_exists($this, $filterMethod))
{
$value = $this->$filterMethod($value);
}
return $value;
}
function rawSet($name, $value, $load = true)
{
return parent::set($name, $value, $load);
}
function rawGet($name)
{
return parent::rawGet($name);
}
public function __call($m, $a)
{
try {
$verb = substr($m, 0, 3);
if ($verb == 'set' || $verb == 'get')
{
$camelColumn = substr($m, 3);
if (in_array($camelColumn, array_keys($this->getTable()->getRelations())))
{
// the column is actually a class name
$column = $camelColumn;
}
else
{
// the relation was not found
$column = sfInflector::underscore($camelColumn);
}
if ($verb == 'get')
{
return $this->get($column);
}
else // $verb must be 'set'...
{
return $this->set($column, $a[0]);
}
}
} catch(Exception $e) {
return parent::__call($m, $a);
}
}
// added for compatibility with the _get_options_from_objects helper
public function getPrimaryKey()
{
return $this->obtainIdentifier();
}
// to check for the existence of a record
public function has($name)
{
return $this->get($name)->exists();
}
// Hook to update created_at and updated_at fields on record insert
public function preInsert($event)
{
// Set created_at and update_at to now, if they exist in this record
$now = date("Y-m-d H:i:s", time());
if ($this->getTable()->hasColumn('created_at'))
{
$this->rawSet('created_at', $now);
}
if ($this->getTable()->hasColumn('updated_at'))
{
$this->rawSet('updated_at', $now);
}
}
// Hook to update updated_at field on record update
public function preUpdate($event)
{
// Set update_at to now, if it exists in this record
$now = date("Y-m-d H:i:s", time());
if ($this->getTable()->hasColumn('updated_at'))
{
$this->rawSet('updated_at', $now);
}
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineRecordI18n.class.php 4498 2007-06-30 20:18:01Z mahono $
*/
class sfDoctrineRecordI18n extends sfDoctrineRecord
{
protected
$cultureName = '',
$i18nColumns = null,
$i18nTable = '',
$culture = null;
public function setCulture($newCulture)
{
$this->culture = $newCulture;
}
public function getCulture()
{
// lazily set the culture to the current user's one
if (!$this->culture)
{
$this->culture = sfContext::getInstance()->getUser()->getCulture();
}
return $this->culture;
}
public function getI18nTable()
{
return $this->getTable()->getConnection()->getTable($this->i18nTable);
}
public function getCurrentI18nObject($create = false)
{
return $this->getI18nObjectForCulture($this->getCulture(), $create);
}
public function getI18nObjectForCulture($culture, $create)
{
if (empty($this->i18nTable))
{
$this->setUp();
}
$coll = parent::get($this->i18nTable);
if (isset($coll[$culture]))
{
return $coll[$culture];
}
// the i18n object does not exist
if ($create)
{
$obj = $this->getI18nTable()->create();
$obj->set($this->cultureName, $culture);
$coll->add($obj);
return $obj;
}
return null; // not found and not created
}
protected function hasI18nTable($i18nTableName, $cultureName)
{
$this->i18nTable = $i18nTableName;
$i18nTable = $this->getI18nTable();
$i18nTable->setAttribute(Doctrine::ATTR_COLL_KEY, $cultureName);
$columns = $i18nTable->getColumns();
$this->cultureName = $cultureName;
unset($columns[$cultureName]);
$pks = $i18nTable->getPrimaryKeys();
foreach($pks as $pk)
{
unset($columns[$pk]);
}
$this->i18nColumns = array_keys($columns);
}
// we need this function in order to be sure that setUp has been called
protected function getI18nColumns()
{
if (is_null($this->i18nColumns))
{
$this->setUp();
}
return $this->i18nColumns;
}
public function get($name, $load = true)
{
// check if $name is i18n
if (in_array($name, $this->getI18nColumns()))
{
if ($obj = $this->getCurrentI18nObject())
{
return $obj->get($name, $load);
}
else
{
return null;
}
}
return parent::get($name, $load);
}
public function set($name, $value, $load = true)
{
if (in_array($name, $this->getI18nColumns()))
{
$obj = $this->getCurrentI18nObject(true);
$obj->set($name, $value, $load);
}
else
{
parent::set($name, $value, $load);
}
}
public function contains($name)
{
$i18n = $this->getCurrentI18nObject();
return $i18n ? $i18n->contains($name) : parent::contains($name);
}
public function getData()
{
$i18n = $this->getCurrentI18nObject();
$i18nData = $i18n ? $i18n->getData() : array();
$data = parent::getData();
return array_merge($data, $i18nData);
}
/**
* @return integer the number of columns in this record
*/
public function count()
{
$data = $this->getData();
return count($data);
}
public function toArray()
{
$array = parent::toArray();
$i18n = $this->getCurrentI18nObject();
$i18nArray = $i18n ? $i18n->toArray() : array();
return array_merge($array, $i18nArray);
}
}
\ No newline at end of file
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* sfDoctrineSchemasConfigHandler parses the config/schemas.yml config file
*
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Stephen Leavitt <stephen.leavitt@gmail.com>
* @version SVN: $Id$
*/
class sfDoctrineSchemasConfigHandler extends sfYamlConfigHandler
{
public function execute( $configFiles )
{
$this->initialize();
$mappings = $this->parseYamls( $configFiles );
$data = array();
$data[] = '$manager = Doctrine_Manager::getInstance();'."\n";
foreach ( $mappings as $mapping => $schemas )
{
foreach ( $schemas as $schema )
{
$path = sfConfig::get( 'sf_config_dir' ) . '/doctrine/' . $schema . '.yml';
$components = array_keys( sfYaml::load( $path ) );
foreach ( $components as $component )
{
$data[] = "\$manager->bindComponent('{$component}', '{$mapping}');";
}
}
}
// compile data
$retval = sprintf("<?php\n".
"// auto-generated by sfDoctrineSchemasConfigHandler\n".
"// date: %s\n%s\n",
date('Y-m-d H:i:s'), implode("\n", $data));
return $retval;
}
}
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfDoctrineUniqueValidator validates that the value does not already exists
*
* <b>Required parameters:</b>
*
* # <b>class</b> - [none] - Doctrine class name.
* # <b>column</b> - [none] - Doctrine column name.
*
* <b>Optional parameters:</b>
*
* # <b>unique_error</b> - [Uniqueness error] - An error message to use when
* the value for this column already
* exists in the database.
*
* @package symfony.plugins
* @subpackage sfDoctrine
* @author ?
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineUniqueValidator.php 3428 2007-02-09 14:46:36Z chtito $
*/
class sfDoctrineUniqueValidator extends sfValidator
{
public function execute (&$value, &$error)
{
$className = $this->getParameter('class');
$columnName = $className.'.'.$this->getParameter('column');
$primaryKeys = sfDoctrine::getTable($className)->getPrimaryKeys();
foreach($primaryKeys as $primaryKey)
{
if(is_null($primaryKeyValue = $this->getContext()
->getRequest()
->getParameter($primaryKey)));
break;
}
$query = new Doctrine_Query();
$query->from($className);
if($primaryKeyValue === null)
{
$query->where($columnName.' = ?');
$res = $query->execute(array($value));
}
else
{
$query->where($columnName.' = ? AND '.$primaryKey.' != ?');
$res = $query->execute(array($value, $primaryKeyValue));
}
if(sizeof($res))
{
$error = $this->getParameterHolder()->get('unique_error');
return false;
}
return true;
}
/**
* Initialize this validator.
*
* @param sfContext The current application context.
* @param array An associative array of initialization parameters.
*
* @return bool true, if initialization completes successfully, otherwise false.
*/
public function initialize ($context, $parameters = null)
{
// initialize parent
parent::initialize($context);
// set defaults
$this->setParameter('unique_error', 'Uniqueness error');
$this->getParameterHolder()->add($parameters);
// check parameters
if (!$this->getParameter('class'))
{
throw new sfValidatorException('The "class" parameter is mandatory for the sfDoctrineUniqueValidator validator.');
}
if (!$this->getParameter('column'))
{
throw new sfValidatorException('The "column" parameter is mandatory for the sfDoctrineUniqueValidator validator.');
}
return true;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" packagerversion="1.4.1" version="2.0" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<name>sfDoctrinePlugin</name>
<channel>pear.symfony-project.com</channel>
<summary>This plugin allows symfony users to use doctrine instead of propel.</summary>
<description>This plugin fully integrates doctrine into symfony. You can use *all* the features in symfony with propel replaced by doctrine.</description>
<lead>
<name>Olivier Verdier</name>
<user>chtito</user>
<email>olivier.verdier@gmail.com</email>
<active>yes</active>
</lead>
<date>2007-05-05</date>
<version>
<release>0.4.2</release>
<api>0.1.0</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.symfony-project.com/license">MIT license</license>
<notes>-</notes>
<contents>
<dir name="addon"><file name="sfDoctrineDatabaseSchema.class.php" role="data"/></dir><dir name="config"><file name="autoload.yml" role="data"/><file name="config_handlers.yml" role="data"/><file name="doctrine.yml" role="data"/></dir><dir name="data"><dir name="sfDoctrineAdmin"><dir name="default"><dir name="skeleton"><dir name="actions"><file name="actions.class.php" role="data"/></dir><dir name="config"><file name="generator.yml" role="data"/></dir><dir name="templates"/><dir name="validate"/></dir><dir name="template"><dir name="actions"><file name="actions.class.php" role="data"/></dir><dir name="templates"><file name="_edit_actions.php" role="data"/><file name="_edit_footer.php" role="data"/><file name="_edit_header.php" role="data"/><file name="_filters.php" role="data"/><file name="_list_actions.php" role="data"/><file name="_list_footer.php" role="data"/><file name="_list_header.php" role="data"/><file name="_list_td_actions.php" role="data"/><file name="_list_td_stacked.php" role="data"/><file name="_list_td_tabular.php" role="data"/><file name="_list_th_stacked.php" role="data"/><file name="_list_th_tabular.php" role="data"/><file name="editSuccess.php" role="data"/><file name="listSuccess.php" role="data"/></dir></dir></dir></dir><dir name="tasks"><file name="sfPakeDoctrine.php" role="data"/></dir></dir><dir name="lib"><file name="sfDoctrine.class.php" role="data"/><file name="sfDoctrineAdminGenerator.class.php" role="data"/><file name="sfDoctrineConfigHandler.class.php" role="data"/><file name="sfDoctrineDatabase.class.php" role="data"/><file name="sfDoctrinePager.class.php" role="data"/><file name="sfDoctrineUniqueValidator.php" role="data"/><file name="sfPropelCompatRecord.class.php" role="data"/></dir><file name="LICENSE" role="data"/><file name="README" role="data"/><dir name="/"><dir name="addon"><file name="sfDoctrineClassSchema.class.php" role="data"/><file name="sfDoctrineColumnSchema.class.php" role="data"/><file name="sfDoctrineDatabaseSchema.class.php" role="data"/><file name="sfDoctrineRelationSchema.class.php" role="data"/><file name="sfDoctrineSchemaDoctrineLoader.class.php" role="data"/><file name="sfDoctrineSchemaPropelLoader.class.php" role="data"/><file name="sfDoctrineTableSchema.class.php" role="data"/></dir><dir name="config"><file name="autoload.yml" role="data"/><file name="config_handlers.yml" role="data"/><file name="doctrine.yml" role="data"/><file name="settings.yml" role="data"/></dir><dir name="data"><dir name="generator"><dir name="sfDoctrineAdmin"><dir name="crud"><dir name="skeleton"><dir name="actions"><file name="actions.class.php" role="data"/></dir><dir name="config"><file name="generator.yml" role="data"/></dir><dir name="lib"/><dir name="templates"/><dir name="validate"/></dir><dir name="template"><dir name="actions"><file name="actions.class.php" role="data"/></dir><dir name="templates"><file name="editSuccess.php" role="data"/><file name="listSuccess.php" role="data"/><file name="showSuccess.php" role="data"/></dir></dir></dir><dir name="default"><dir name="skeleton"><dir name="actions"><file name="actions.class.php" role="data"/></dir><dir name="config"><file name="generator.yml" role="data"/></dir><dir name="templates"/><dir name="validate"/></dir><dir name="template"><dir name="actions"><file name="actions.class.php" role="data"/></dir><dir name="templates"><file name="_edit_actions.php" role="data"/><file name="_edit_footer.php" role="data"/><file name="_edit_form.php" role="data"/><file name="_edit_header.php" role="data"/><file name="_edit_messages.php" role="data"/><file name="_filters.php" role="data"/><file name="_list.php" role="data"/><file name="_list_actions.php" role="data"/><file name="_list_footer.php" role="data"/><file name="_list_header.php" role="data"/><file name="_list_messages.php" role="data"/><file name="_list_td_actions.php" role="data"/><file name="_list_td_stacked.php" role="data"/><file name="_list_td_tabular.php" role="data"/><file name="_list_th_stacked.php" role="data"/><file name="_list_th_tabular.php" role="data"/><file name="editSuccess.php" role="data"/><file name="listSuccess.php" role="data"/></dir></dir></dir></dir></dir><dir name="tasks"><file name="sfPakeDoctrine.php" role="data"/></dir></dir><dir name="lib"><dir name="helper"><file name="ObjectDoctrineAdminHelper.php" role="data"/></dir><file name="sfDoctrine.class.php" role="data"/><file name="sfDoctrineAdminGenerator.class.php" role="data"/><file name="sfDoctrineConfigHandler.class.php" role="data"/><file name="sfDoctrineConnectionListener.class.php" role="data"/><file name="sfDoctrineData.class.php" role="data"/><file name="sfDoctrineDatabase.class.php" role="data"/><file name="sfDoctrineDataRetriever.class.php" role="data"/><file name="sfDoctrineEventListener.class.php" role="data"/><file name="sfDoctrineException.class.php" role="data"/><file name="sfDoctrinePager.class.php" role="data"/><file name="sfDoctrineQueryLogger.class.php" role="data"/><file name="sfDoctrineRecord.class.php" role="data"/><file name="sfDoctrineRecordI18n.class.php" role="data"/><file name="sfDoctrineSchemasConfigHandler.class.php" role="data"/><file name="sfDoctrineUniqueValidator.php" role="data"/></dir><file name="LICENSE" role="data"/><file name="README" role="data"/><dir name="test"><dir name="bootstrap"><file name="config.php" role="data"/><file name="functional.php" role="data"/><file name="unit.php" role="data"/></dir><file name="coverage.php" role="data"/><dir name="functional"/><dir name="other"/><file name="prove.php" role="data"/><file name="tests.php" role="data"/><dir name="unit"><dir name="addon"><dir name="fixtures"><file name="doctrineTestSchema.yml" role="data"/><file name="propelTestSchema.xml" role="data"/><file name="propelTestSchema.yml" role="data"/></dir><file name="sfDoctrineClassTest.php" role="data"/><file name="sfDoctrineColumnTest.php" role="data"/><file name="sfDoctrineRelationTest.php" role="data"/><file name="sfDoctrineSchemaDoctrineLoaderTest.php" role="data"/><file name="sfDoctrineSchemaPropelLoaderTest.php" role="data"/><file name="sfDoctrineTableTest.php" role="data"/></dir><dir name="lib"/></dir></dir><file name="TODO.txt" role="data"/></dir></contents>
<dependencies>
<required>
<php>
<min>5.1.0</min>
</php>
<pearinstaller>
<min>1.4.1</min>
</pearinstaller>
<package>
<name>symfony</name>
<channel>pear.symfony-project.com</channel>
<min>1.0.0</min>
<max>1.1.0</max>
<exclude>1.1.0</exclude>
</package>
</required>
</dependencies>
<phprelease>
</phprelease>
<changelog>
</changelog>
</package>
<?php
// comment or remove the following message
die('Please define the path to the symfony library in '.realpath(__FILE__)."\n");
$sf_symfony_lib_dir = '';
$sf_symfony_data_dir = '';
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Pavel Kunc
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: unit.php 2690 2006-11-15 18:35:07Z chtito $
*/
$sfDoctrine_dir = realpath(dirname(__FILE__).'/../..');
define('SF_ROOT_DIR', realpath($sfDoctrine_dir.'/../..'));
// symfony directories
$project_config = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php';
if (file_exists($project_config)) // if we are in a project directory
require $project_config;
else // the plugin is installed globally
require 'config.php';
require_once $sf_symfony_lib_dir.'/../test/bootstrap/unit.php';
class sfDoctrineAutoLoader extends testAutoloader
{
public static function sfDoctrineInitialize()
{
//FIXME: loading all the sfDoctrine directory is probably not needed
$files = pakeFinder::type('file')->name('*.php')->ignore_version_control()->in(realpath(dirname(__FILE__).'/../..'));
foreach ($files as $file)
{
preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
foreach ($classes[1] as $class)
{
self::$class_paths[$class] = $file;
}
}
}
}
sfDoctrineAutoLoader::initialize();
sfDoctrineAutoLoader::sfDoctrineInitialize();
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Pavel Kunc
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: coverage.php 2690 2006-11-15 18:35:07Z chtito $
*/
$testsDir = realpath(dirname(__FILE__));
define('SF_ROOT_DIR', realpath($testsDir.'/../../../'));
// symfony directories
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
require_once($sf_symfony_lib_dir.'/vendor/lime/lime.php');
$h = new lime_harness(new lime_output_color());
$h->base_dir = dirname(__FILE__);
// unit tests
$h->register_glob($h->base_dir.'/unit/*/*Test.php');
// functional tests
$h->register_glob($h->base_dir.'/functional/*Test.php');
$c = new lime_coverage($h);
$c->extension = '.class.php';
$c->verbose = false;
$c->base_dir = realpath(dirname(__FILE__).'/../lib');
$c->register_glob($c->base_dir.'/*/*.php');
$c->run();
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Pavel Kunc
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: prove.php 2874 2006-11-29 16:48:01Z chtito $
*/
$testsDir = realpath(dirname(__FILE__));
require_once($testsDir.'/bootstrap/unit.php');
$h = new lime_harness(new lime_output_color());
$h->base_dir = $testsDir;
// cache autoload files
testAutoloader::initialize(true);
// unit tests
$h->register_glob($h->base_dir.'/unit/*/*Test.php');
// functional tests
//$h->register_glob($h->base_dir.'/functional/*Test.php');
//$h->register_glob($h->base_dir.'/functional/*/*Test.php');
// other tests
//$h->register_glob($h->base_dir.'/other/*Test.php');
$h->run();
testAutoloader::removeCache();
---
TestClass:
tableName: testTable
columns:
name: string(64)
description: string(8)
active: boolean
User:
tableName: user
columns:
name:
Group:
tableName: group
columns:
name:
User2Group:
tableName: user2group
columns:
user_id: {foreignClass: User, localName: Groups, counterpart: group_id, cascadeDelete: true}
group_id: {foreignClass: Group, localName: Users, counterpart: user_id, cascadeDelete: true}
Parent:
tableName: parent_table
columns:
dummy:
ColAggregation:
inheritance: {extends: Parent, keyField: class_key, keyValue: 1}
columns:
last_active: timestamp
SeparateTable:
tableName: separate_table
inheritance: {extends: Parent}
columns:
none:
Review:
tableName: reviews
columns:
author_id:
type: integer
size: 11
foreignReference: id
localName: Reviews
foreignName: author
foreignClass: User
counterpart: book_id
book_id:
type: integer
size: 11
foreignReference: id
localName: Reviews
foreignClass: Book
counterpart: author_id
Book:
tableName: book
i18n: {class: BookI18n, cultureField: culture}
columns:
author_id:
localName: Books
foreignName: author
foreignClass: User
publication_date: date
lastviewed: timestamp
price: double(10)
edition: integer(11)
type: enum(5)
dimensions:
type: array
size: 100
BookI18n:
tableName: book_i18n
columns:
title:
Yin:
tableName: yin
columns:
name:
yang_id: {foreignClass: Yang, unique: true}
Yang:
tableName: yang
columns:
name:
#Tone:
# inheritance: {extends: Ttwo}
#Ttwo:
# inheritance: {extends: Tone}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<database name="doctrineTestSchema" defaultIdMethod="native">
<table name="testTable" phpName="TestTable">
<column name="name" type="longvarchar" />
<column name="description" type="longvarchar" />
<column name="id" type="integer" primaryKey="true" autoIncrement="true" />
<foreign-key foreignTable="dummy">
<reference foreign="id" local="dummy_id"/>
</foreign-key>
</table>
<table name="dummy" phpName="DummyPHP">
<column name="foo" type="integer"/>
</table>
</database>
\ No newline at end of file
---
doctrineTestSchema:
testTable:
_attributes:
idMethod: native
id:
type: TINYINT
required: true
autoIncrement: true
primaryKey: true
name:
type: VARCHAR
size: 64
required: true
default:
description:
type: VARCHAR
size: 8
required: true
default:
dummy_id:
foreignTable: dummy
onDelete: cascade
dummy:
_attributes: {phpName: DummyPHP}
foo: integer(10)
\ No newline at end of file
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Pavel Kunc
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineClassTest.php 3455 2007-02-14 16:17:48Z chtito $
*/
//We need bootStrap
require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
//TODO: add planned tests
$t = new lime_test(null,new lime_output_color());
$name = 'TestClass';
$class = new sfDoctrineClassSchema($name);
// ->__construct()
$t->diag('->__constuct()');
$t->is($class->getPhpName(), $name, '->__construct() takes first parameter as Class name');
// ->setPhpName()
$t->diag('->setPhpName()');
$newName = 'NewTestClass';
$class->setPhpName($newName);
$t->is($class->getPhpName(), $newName, '->setPhpName() sets new Class name');
// ->getColumns()
$t->diag('->getColumns()');
$t->is($class->getColumns(), array(),'->getColumns() returns array');
// ->isTable()
$t->diag('->isTable()');
$t->is($class->hasTable(), false, '->isTable() class is never table');
$t->diag('setting up a foreign relation');
$colName = 'colName';
$column = new sfDoctrineColumnSchema($colName, array('foreignClass'=>'otherClass'));
$class->addColumn($column);
$rel = $class->getRelation($colName);
$t->is($rel->get('localName'), $class->getPhpName().'s', 'default local name: plural of class name');
$t->is($rel->get('foreignName'), 'otherClass', 'default foreignName set to the foreign class name');
$t->diag('setting up options');
$class = new sfDoctrineClassSchema($name, array('options' => array('foo'=> 'bar')));
$classPhp = $class->asPhp();
$t->like($classPhp[0]['source'], '@\$this->option\(\'foo\', \'bar\'\)@', 'right output of the class options');
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Pavel Kunc
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineColumnTest.php 3438 2007-02-10 15:31:31Z chtito $
*/
//We need bootStrap
require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
//TODO: add planned tests
$t = new lime_test(null, new lime_output_color());
$colName = 'TestColumn';
$column = new sfDoctrineColumnSchema($colName);
// ->__construct(), special case without variable parameters
$t->diag('->__constuct()');
$t->is($column->getName(), $colName, '->__construct() takes first parameter as Column name');
$t->isa_ok($column->getColumnInfo(), 'sfParameterHolder', '->__construct() sets column infor to sfParameterHolder');
//Construct sets default values, nothing passed
$props = $column->getProperties();
$t->is($props['type'],'string',"->__construct() default type is 'string'");
$t->is($props['name'],$colName,"->__construct() sets property name to column name");
$column = new sfDoctrineColumnSchema($colName, array('foreignClass'=>'other'));
$props = $column->getProperties();
$t->is($props['type'], 'integer', 'default foreign key type is integer');
$t->diag('constraints');
$column = new sfDoctrineColumnSchema($colName, array('enum'=>true, 'noconstraint'=>true));
$props = $column->getProperties();
$t->is($props['enum'], true, 'constraints are stored properly');
$t->ok(!isset($props['notaconstraint']), 'false constraints are not stored');
$t->diag('short syntax');
$type = 'string';
$size = 10;
$shortTypeSize = "$type($size)";
$column = new sfDoctrineColumnSchema($colName, array('type'=>$shortTypeSize));
$t->is($column->getProperty('size'), $size, 'short array syntax for size');
$t->is($column->getProperty('type'), $type, 'short array syntax for type');
$column = new sfDoctrineColumnSchema($colName, $shortTypeSize);
$t->is($column->getProperty('size'), $size, 'short string syntax for size');
$t->is($column->getProperty('type'), $type, 'short string syntax for type');
$column = new sfDoctrineColumnSchema($column, 'boolean');
$t->is($column->getProperty('type'), 'boolean', 'short string syntax without size');
$t->diag('PHP output');
$type = 'integer';
$size = 456;
$constraint = 'primary';
$colSetup = array('type'=>$type, 'size'=>$size, 'columnName' => 'test_column', $constraint=>true);
$column = new sfDoctrineColumnSchema($colName, $colSetup);
$t->is($column->asPhp(), "\$this->hasColumn('test_column as $colName', '$type', $size, array ( '$constraint' => true,));", 'php output');
$t->diag('Doctrine YML output');
$t->is_deeply($column->asDoctrineYml(), $colSetup, 'Doctrine array output');
$colEnum = new sfDoctrineColumnSchema($colName, array('type'=>array('a a', 'b')));
#$t->like($colEnum->asPhp(), "|this->setEnumValues\('TestColumn', .*0=>'a',.*1=>'b'.*\);|", 'enum types are declared');
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Pavel Kunc
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineSchemaDoctrineLoaderTest.php 3455 2007-02-14 16:17:48Z chtito $
*/
//We need bootStrap
require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
$t = new lime_test(null,new lime_output_color());
class sfDoctrineSchemaDoctrineLoaderTestProxy extends sfDoctrineSchemaDoctrineLoader {
/**
* Lime test object
*
* @var lime_test
*/
private $t = null;
/**
* Constructor
*
* @param lime_test $testObject
*/
function __construct($testObject) {
$this->t = $testObject;
}
/**
* Test launcher
*
* @param string $schema Path to schema file
*/
function launchTests($schema) {
$this->t->diag('->load()');
$this->load($schema);
$this->process();
$yml = $this->asDoctrineYml();
$this->t->diag('->getClasses()');
$classes = $this->getClasses();
$nbClasses = 12;
$this->t->is(count($classes), $nbClasses,"->getClasses() should return $nbClasses classes from fixture.");
$this->t->diag('->getClass()');
$class = $this->getClass('TestClass');
#$this->t->ok($class->isTable(),"->getClass() should return return class instance.");
$this->t->diag('->parentTable()');
$table = $this->parentTable($class);
$this->t->is(get_class($table), 'sfDoctrineTableSchema', "->parentTable() should return table instance.");
$this->t->is($this->getClass('ColAggregation')->getTableName(), 'parent_table', 'inheritance gets the right parent table');
#$this->t->ok($this->getClass('SeparateTable')->isTable(), '"SeparateTable" is a table-class');
$this->t->is($this->getClass('BookI18n')->getColumn('culture')->getProperty('type'), 'string', 'culture field is defined (as a string)');
$rel = $this->getClass('BookI18n')->getRelation('id');
$this->t->is($rel->get('localName'), 'BookI18n', 'i18n relation name is not a plural');
$this->t->is($this->getClass('ColAggregation')->getTable()->getColumn('class_key')->getProperty('type'), 'integer', 'inheritance field is defined (as an integer)');
$c = $this->getClass('SeparateTable');
$SeparateTablePhp = $c->asPhp();
$SeparateTableSource = $SeparateTablePhp[0]['source'];
$this->t->like($SeparateTableSource, '/extends Parent/', 'The class "SeparateTable" extends Parent without having any class key field');
$this->t->like($SeparateTableSource, '@setTableName\(\'separate_table\'\)@', 'class "SeparateTable" has both a table and inheritance');
$this->t->like($SeparateTableSource, '@parent::setTableDefinition\(\);@', 'class "SeparateTable" calls parent::setTableDefinition');
$colAggregationPhp = $this->getClass('ColAggregation')->asPhp();
$this->t->like($colAggregationPhp[0]['source'], "@setInheritanceMap\(array\('class_key'=>1\)\)@", 'setInheritanceMap is properly set');
$this->t->diag('relationships');
$yangPhp = $this->getClass('Yin')->asPhp();
$this->t->like($yangPhp[0]['source'], "#hasOne\('Yang as Yang', 'Yin.yang_id', 'id'\)#", 'one to one relationships is properly declared');
$userPhp = $this->getClass('User')->asPhp();
$this->t->like($userPhp[0]['source'], "#hasMany\('Book as Books', 'Book.author_id'\)#", 'hasMany is properly declared');
$this->t->like($userPhp[0]['source'], "#hasMany\('Group as Groups', 'User2Group.group_id'\)#", 'has many to many properly declared');
$userGroupPhp = $this->getClass('User2Group')->asPhp();
$this->t->like($userGroupPhp[0]['source'], "#ownsOne\('User as User', 'User2Group.group_id', 'id'\)#", 'has many to many with cascade properly defined');
}
}
//Load doctrine schema from fixtures and run tests
$schemaFixture = dirname(__FILE__)."/fixtures/doctrineTestSchema.yml";
$schema = new sfDoctrineSchemaDoctrineLoaderTestProxy($t);
$schema->launchTests($schemaFixture);
\ No newline at end of file
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Pavel Kunc
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineSchemaPropelLoaderTest.php 3455 2007-02-14 16:17:48Z chtito $
*/
//We need bootStrap
require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
$t = new lime_test(null, new lime_output_color());
class sfDoctrineSchemaPropelLoaderTestProxy extends sfDoctrineSchemaPropelLoader {
/**
* Lime test object
*
* @var lime_test
*/
private $t = null;
/**
* Constructor
*
* @param lime_test $testObject
*/
function __construct($testObject) {
$this->t = $testObject;
}
/**
* Test launcher
*
* @param string $schema Path to schema file
*/
function launchTests($schema) {
$this->t->diag('->load()');
$this->load($schema);
$this->t->diag('->getTables()');
$tables = $this->tables;
$this->t->is(count($tables),2,"->getTables() should return 2 table from fixture.");
$this->t->ok(in_array('testTable', array_keys($tables)), "->getTables() should return 'testTable' from fixture.");
$this->t->diag('->classes');
$this->t->is(count($this->classes),2,"->classes should have 2 class from fixture");
$this->t->ok($this->getClass('TestTable'),"->classes should have 'TestTable' from fixture.");
$this->t->ok($this->getClass('TestTable')->getColumn('dummy_id')->hasRelation(), 'foreign relation is properly imported');
#$this->t->diag('->asDoctrineYml()');
#$yml = $this->asDoctrineYml();
#$this->t->cmp_ok(strlen($yml['source']), '>', 0, "->asDoctrineYml() doctrine YAML shoudl not be empty.");
$this->t->diag('->findClassByTableName()');
$this->t->is($this->findClassByTableName('testTable')->getPhpName(),'TestTable', "->findClassByTableName() returns 'TestTable' class for 'testTable' table.");
$yml = $this->asDoctrineYml();
$yml = $yml['source'];
$this->t->like($yml, '@cascadeDelete: 1@', 'onDelete is generated');
}
}
//Load Propel schema from fixtures and run tests
$schemaFixture = dirname(__FILE__)."/fixtures/propelTestSchema.xml";
$schema = new sfDoctrineSchemaPropelLoaderTestProxy($t);
$schema->launchTests($schemaFixture);
$schemaFixture = dirname(__FILE__)."/fixtures/propelTestSchema.yml";
$schema = new sfDoctrineSchemaPropelLoaderTestProxy($t);
$schema->launchTests($schemaFixture);
\ No newline at end of file
<?php
/*
* This file is part of the sfDoctrine package.
* (c) 2006 Olivier Verdier <Olivier.Verdier@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package symfony.plugins
* @subpackage sfDoctrine
* @author Pavel Kunc
* @author Olivier Verdier <Olivier.Verdier@gmail.com>
* @version SVN: $Id: sfDoctrineTableTest.php 3455 2007-02-14 16:17:48Z chtito $
*/
//We need bootStrap
require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
//TODO: add planned tests
$t = new lime_test(null,new lime_output_color());
$tableName = 'test';
$package = 'package';
$table = new sfDoctrineTableSchema($tableName,$package);
// ->__construct()
$t->diag('->construct()');
$t->is($table->getName(), $tableName, '->__construct() takes first parameter as Table name');
$t->is($table->getPackage(), $package, '->__construct() takes second parameter as package name');
// ->setName()
$t->diag('->setName()');
$tableName = 'myTest';
$table->setName($tableName);
$t->is($table->getName(), $tableName, '->setName() sets new table name');
// ->addClass()
//TODO: need test
// ->addPropelXmlClasses()
//TODO: need test
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