Added Doctrine::tableize(), Doctrine::classify() and DataDict::isValidClassname() for proper table naming. Fixed Table() to accept these (more need to be changed such as rawsql).Also fixed unbind method (missing argument)
This commit is contained in:
parent
b887ca946f
commit
392683e10d
18
Doctrine.php
18
Doctrine.php
@ -420,5 +420,23 @@ class DQLException extends Exception { }
|
||||
require_once($class);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* returns table name from class name
|
||||
*
|
||||
* @param string $classname
|
||||
* @return string
|
||||
*/
|
||||
public static function tableize($classname) {
|
||||
return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $classname));
|
||||
}
|
||||
/**
|
||||
* returns class name from table name
|
||||
*
|
||||
* @param string $tablename
|
||||
* @return string
|
||||
*/
|
||||
public static function classify($tablename) {
|
||||
return preg_replace('~(_?)(_)([\w])~e', '"$1".strtoupper($3)', ucfirst($tablename));
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -109,5 +109,16 @@ class Doctrine_DataDict {
|
||||
throw new Doctrine_Exception("Unknown column type $type");
|
||||
endswitch;
|
||||
}
|
||||
/**
|
||||
* checks for valid class name (uses camel case and underscores)
|
||||
*
|
||||
* @param string $classname
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isValidClassname($classname) {
|
||||
if(preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $classname))
|
||||
throw new Doctrine_Exception("Class name is not valid. use camel case and underscores (i.e My_PerfectClass).");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -154,7 +154,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
|
||||
$class = $method->getDeclaringClass();
|
||||
|
||||
if( ! isset($this->tableName))
|
||||
$this->tableName = strtolower($class->getName());
|
||||
$this->tableName = Doctrine::tableize($class->getName());
|
||||
|
||||
switch(count($this->primaryKeys)):
|
||||
case 0:
|
||||
@ -205,7 +205,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
|
||||
}
|
||||
endswitch;
|
||||
|
||||
if($this->getAttribute(Doctrine::ATTR_CREATE_TABLES)) {
|
||||
if(Doctrine_DataDict::isValidClassname($class->getName()) && $this->getAttribute(Doctrine::ATTR_CREATE_TABLES)) {
|
||||
$dict = new Doctrine_DataDict($this->getSession()->getDBH());
|
||||
$dict->createTable($this->tableName, $this->columns);
|
||||
}
|
||||
@ -456,7 +456,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
|
||||
* @param $name
|
||||
* @return boolean
|
||||
*/
|
||||
final public function unbind() {
|
||||
final public function unbind($name) {
|
||||
if( ! isset($this->bound[$name]))
|
||||
return false;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user