Commit 392683e1 authored by amadeus's avatar amadeus

Added Doctrine::tableize(), Doctrine::classify() and...

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)
parent b887ca94
<?php
/*
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
......@@ -33,7 +33,7 @@ final class Doctrine {
/**
* ERROR MODE CONSTANTS
*/
/**
* NO PRIMARY KEY COLUMN ERROR
* no primary key column found error code
......@@ -47,7 +47,7 @@ final class Doctrine {
const ERR_REFRESH = 1;
/**
* FIND ERROR
* this code used when for example Doctrine_Table::find() is called and
* this code used when for example Doctrine_Table::find() is called and
* a Data Access Object is not found
*/
const ERR_FIND = 2;
......@@ -145,7 +145,7 @@ final class Doctrine {
* collection key attribute
*/
const ATTR_COLL_KEY = 15;
/**
/**
* collection limit attribute
*/
const ATTR_COLL_LIMIT = 16;
......@@ -155,7 +155,7 @@ final class Doctrine {
/**
* CACHE CONSTANTS
*/
/**
* sqlite cache constant
*/
......@@ -164,9 +164,9 @@ final class Doctrine {
* constant for disabling the caching
*/
const CACHE_NONE = 1;
/**
* FETCHMODE CONSTANTS
*/
......@@ -219,7 +219,7 @@ final class Doctrine {
/**
* LOCKMODE CONSTANTS
*/
/**
* mode for optimistic locking
*/
......@@ -228,11 +228,11 @@ final class Doctrine {
* mode for pessimistic locking
*/
const LOCK_PESSIMISTIC = 1;
/**
* PRIMARY KEY TYPE CONSTANTS
*/
/**
* auto-incremented/(sequence updated) primary key
*/
......@@ -241,7 +241,7 @@ final class Doctrine {
* unique key
*/
const UNIQUE_KEY = 1;
/**
* constructor
*/
......@@ -314,7 +314,7 @@ final class Doctrine {
/**
* method for making a single file of most used doctrine components
*
* including the compiled file instead of multiple files (in worst
* including the compiled file instead of multiple files (in worst
* cases dozens of files) can improve performance by order of magnitude
*
* @throws Doctrine_Exception
......@@ -354,14 +354,14 @@ final class Doctrine {
"Association",
"DB",
"DBStatement");
$ret = array();
foreach($classes as $class) {
if($class !== 'Doctrine')
$class = 'Doctrine_'.$class;
$file = self::$path.DIRECTORY_SEPARATOR.str_replace("_",DIRECTORY_SEPARATOR,$class).".php";
if( ! file_exists($file))
......@@ -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));
}
}
?>
......@@ -6,7 +6,7 @@ class Doctrine_DataDict {
public function __construct(PDO $dbh) {
$file = Doctrine::getPath().DIRECTORY_SEPARATOR."Doctrine".DIRECTORY_SEPARATOR."adodb-hack".DIRECTORY_SEPARATOR."adodb.inc.php";
if( ! file_exists($file))
if( ! file_exists($file))
throw new Doctrine_Exception("Couldn't include datadict. File $file does not exist");
require_once($file);
......@@ -89,7 +89,7 @@ class Doctrine_DataDict {
case "boolean":
return "L";
break;
case "enum":
case "enum":
case "integer":
if(empty($length))
return "I8";
......@@ -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;
......@@ -807,7 +807,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
}
/**
* count
*
*
* @return integer
*/
public function count() {
......
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