DQL now supports UPDATE queries
This commit is contained in:
parent
50226697e0
commit
84bbfccbe2
@ -314,7 +314,7 @@ final class Doctrine {
|
|||||||
* @param string $directory
|
* @param string $directory
|
||||||
*/
|
*/
|
||||||
public static function import($directory) {
|
public static function import($directory) {
|
||||||
|
Doctrine_Import::import();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* export
|
* export
|
||||||
@ -323,7 +323,7 @@ final class Doctrine {
|
|||||||
* @param string $directory
|
* @param string $directory
|
||||||
*/
|
*/
|
||||||
public static function export($directory) {
|
public static function export($directory) {
|
||||||
|
Doctrine_Export::export();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* compile
|
* compile
|
||||||
|
@ -276,7 +276,10 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module implemen
|
|||||||
$params = array_merge($params, $id);
|
$params = array_merge($params, $id);
|
||||||
|
|
||||||
|
|
||||||
$sql = "UPDATE ".$record->getTable()->getTableName()." SET ".implode(", ",$set)." WHERE ".implode(" = ? AND ",$record->getTable()->getPrimaryKeys())." = ?";
|
$sql = 'UPDATE ' . $record->getTable()->getTableName()
|
||||||
|
. ' SET ' . implode(', ', $set)
|
||||||
|
. ' WHERE ' . implode(' = ? AND ', $record->getTable()->getPrimaryKeys())
|
||||||
|
. ' = ?';
|
||||||
|
|
||||||
$stmt = $this->conn->getDBH()->prepare($sql);
|
$stmt = $this->conn->getDBH()->prepare($sql);
|
||||||
$stmt->execute($params);
|
$stmt->execute($params);
|
||||||
|
@ -585,8 +585,11 @@ class Doctrine_Export extends Doctrine_Connection_Module {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* export
|
* export
|
||||||
|
* method for exporting Doctrine_Record classes to a schema
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function export() {
|
public static function export() {
|
||||||
$parent = new ReflectionClass('Doctrine_Record');
|
$parent = new ReflectionClass('Doctrine_Record');
|
||||||
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
|
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
|
||||||
$old = $conn->getAttribute(Doctrine::ATTR_CREATE_TABLES);
|
$old = $conn->getAttribute(Doctrine::ATTR_CREATE_TABLES);
|
||||||
|
@ -96,15 +96,16 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
|
|||||||
* @var array $parts SQL query string parts
|
* @var array $parts SQL query string parts
|
||||||
*/
|
*/
|
||||||
protected $parts = array(
|
protected $parts = array(
|
||||||
"select" => array(),
|
'select' => array(),
|
||||||
"from" => array(),
|
'from' => array(),
|
||||||
"join" => array(),
|
'set' => array(),
|
||||||
"where" => array(),
|
'join' => array(),
|
||||||
"groupby" => array(),
|
'where' => array(),
|
||||||
"having" => array(),
|
'groupby' => array(),
|
||||||
"orderby" => array(),
|
'having' => array(),
|
||||||
"limit" => false,
|
'orderby' => array(),
|
||||||
"offset" => false,
|
'limit' => false,
|
||||||
|
'offset' => false,
|
||||||
);
|
);
|
||||||
/**
|
/**
|
||||||
* constructor
|
* constructor
|
||||||
|
@ -137,8 +137,13 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* connection
|
* connection
|
||||||
|
*
|
||||||
|
* if the adapter parameter is set this method acts as
|
||||||
* a short cut for Doctrine_Manager::getInstance()->openConnection($adapter, $name);
|
* a short cut for Doctrine_Manager::getInstance()->openConnection($adapter, $name);
|
||||||
*
|
*
|
||||||
|
* if the adapter paramater is not set this method acts as
|
||||||
|
* a short cut for Doctrine_Manager::getInstance()->getCurrentConnection()
|
||||||
|
*
|
||||||
* @param PDO|Doctrine_Adapter_Interface $adapter database driver
|
* @param PDO|Doctrine_Adapter_Interface $adapter database driver
|
||||||
* @param string $name name of the connection, if empty numeric key is used
|
* @param string $name name of the connection, if empty numeric key is used
|
||||||
* @throws Doctrine_Manager_Exception if trying to bind a connection with an existing name
|
* @throws Doctrine_Manager_Exception if trying to bind a connection with an existing name
|
||||||
|
@ -320,7 +320,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
$method = "parse".ucwords($name);
|
$method = 'parse' . ucwords($name);
|
||||||
|
|
||||||
switch($name) {
|
switch($name) {
|
||||||
case 'select':
|
case 'select':
|
||||||
@ -399,48 +399,19 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
return $this->parts[$name];
|
return $this->parts[$name];
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* sets a query part
|
* set
|
||||||
|
* sets a query SET part
|
||||||
|
* this method should only be used with UPDATE queries
|
||||||
*
|
*
|
||||||
* @param $name query part name
|
* @param $name name of the field
|
||||||
* @param $value query part value
|
* @param $value field value
|
||||||
* @return boolean
|
* @return Doctrine_Query
|
||||||
*/
|
*/
|
||||||
public function set($name, $value) {
|
public function set($name, $value) {
|
||||||
/**
|
|
||||||
|
|
||||||
if(isset($this->parts[$name])) {
|
|
||||||
$method = "parse".ucwords($name);
|
|
||||||
switch($name):
|
|
||||||
case "where":
|
|
||||||
case "having":
|
|
||||||
$this->parts[$name] = array($this->$method($value));
|
|
||||||
break;
|
|
||||||
case "limit":
|
|
||||||
case "offset":
|
|
||||||
if($value == null)
|
|
||||||
$value = false;
|
|
||||||
|
|
||||||
$this->parts[$name] = $value;
|
|
||||||
break;
|
|
||||||
case "from":
|
|
||||||
$this->parts['select'] = array();
|
|
||||||
$this->parts['join'] = array();
|
|
||||||
$this->joins = array();
|
|
||||||
$this->tables = array();
|
|
||||||
$this->fetchModes = array();
|
|
||||||
$this->tableIndexes = array();
|
|
||||||
$this->tableAliases = array();
|
|
||||||
default:
|
|
||||||
$this->parts[$name] = array();
|
|
||||||
$this->$method($value);
|
|
||||||
endswitch;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
*/
|
|
||||||
$class = new Doctrine_Query_Set($this);
|
$class = new Doctrine_Query_Set($this);
|
||||||
$class->parse($name, $value);
|
$this->parts['set'][] = $class->parse($name . ' = ' . $value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @return boolean
|
* @return boolean
|
||||||
@ -512,6 +483,10 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( ! empty($this->parts['set'])) {
|
||||||
|
$q .= ' SET ' . implode(', ', $this->parts['set']);
|
||||||
|
}
|
||||||
|
|
||||||
$string = $this->applyInheritance();
|
$string = $this->applyInheritance();
|
||||||
|
|
||||||
if( ! empty($string))
|
if( ! empty($string))
|
||||||
@ -745,8 +720,8 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
$this->clear();
|
$this->clear();
|
||||||
|
|
||||||
$query = trim($query);
|
$query = trim($query);
|
||||||
$query = str_replace("\n"," ",$query);
|
$query = str_replace("\n", ' ', $query);
|
||||||
$query = str_replace("\r"," ",$query);
|
$query = str_replace("\r", ' ', $query);
|
||||||
|
|
||||||
$parts = $this->splitQuery($query);
|
$parts = $this->splitQuery($query);
|
||||||
|
|
||||||
@ -756,7 +731,6 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
case 'DELETE':
|
case 'DELETE':
|
||||||
$this->type = self::DELETE;
|
$this->type = self::DELETE;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'SELECT':
|
case 'SELECT':
|
||||||
$this->type = self::SELECT;
|
$this->type = self::SELECT;
|
||||||
$this->parseSelect($part);
|
$this->parseSelect($part);
|
||||||
@ -766,27 +740,31 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
$k = 'FROM';
|
$k = 'FROM';
|
||||||
|
|
||||||
case 'FROM':
|
case 'FROM':
|
||||||
case 'SET':
|
$class = 'Doctrine_Query_' . ucwords(strtolower($k));
|
||||||
$class = 'Doctrine_Query_'.ucwords(strtolower($k));
|
|
||||||
$parser = new $class($this);
|
$parser = new $class($this);
|
||||||
$parser->parse($part);
|
$parser->parse($part);
|
||||||
break;
|
break;
|
||||||
|
case 'SET':
|
||||||
|
$class = 'Doctrine_Query_' . ucwords(strtolower($k));
|
||||||
|
$parser = new $class($this);
|
||||||
|
$this->parts['set'][] = $parser->parse($part);
|
||||||
|
break;
|
||||||
case 'GROUP':
|
case 'GROUP':
|
||||||
case 'ORDER':
|
case 'ORDER':
|
||||||
$k .= "by";
|
$k .= 'by';
|
||||||
case 'WHERE':
|
case 'WHERE':
|
||||||
case 'HAVING':
|
case 'HAVING':
|
||||||
$class = "Doctrine_Query_".ucwords(strtolower($k));
|
$class = 'Doctrine_Query_' . ucwords(strtolower($k));
|
||||||
$parser = new $class($this);
|
$parser = new $class($this);
|
||||||
|
|
||||||
$name = strtolower($k);
|
$name = strtolower($k);
|
||||||
$this->parts[$name][] = $parser->parse($part);
|
$this->parts[$name][] = $parser->parse($part);
|
||||||
break;
|
break;
|
||||||
case 'LIMIT':
|
case 'LIMIT':
|
||||||
$this->parts["limit"] = trim($part);
|
$this->parts['limit'] = trim($part);
|
||||||
break;
|
break;
|
||||||
case 'OFFSET':
|
case 'OFFSET':
|
||||||
$this->parts["offset"] = trim($part);
|
$this->parts['offset'] = trim($part);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,23 @@
|
|||||||
*/
|
*/
|
||||||
class Doctrine_Query_Set extends Doctrine_Query_Part {
|
class Doctrine_Query_Set extends Doctrine_Query_Part {
|
||||||
public function parse($dql) {
|
public function parse($dql) {
|
||||||
$e = Doctrine_Query::sqlExplode($dql, '=');
|
$parts = Doctrine_Query::sqlExplode($dql, ',');
|
||||||
|
|
||||||
|
$result = array();
|
||||||
|
foreach($parts as $part) {
|
||||||
|
$set = Doctrine_Query::sqlExplode($part, '=');
|
||||||
|
|
||||||
|
$e = explode('.', trim($set[0]));
|
||||||
|
$field = array_pop($e);
|
||||||
|
|
||||||
|
$reference = implode('.', $e);
|
||||||
|
|
||||||
|
$alias = $this->query->getTableAlias($reference);
|
||||||
|
|
||||||
|
$result[] = $alias . '.' . $field . ' = ' . $set[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(', ', $result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -37,7 +37,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
|
|||||||
$value = implode('', $slice);
|
$value = implode('', $slice);
|
||||||
$operator = trim(substr($where, strlen($r), -strlen($value)));
|
$operator = trim(substr($where, strlen($r), -strlen($value)));
|
||||||
|
|
||||||
$reference = implode(".",$a);
|
$reference = implode('.', $a);
|
||||||
$count = count($a);
|
$count = count($a);
|
||||||
|
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
|
|||||||
$values = Doctrine_Query::sqlExplode($value, ',');
|
$values = Doctrine_Query::sqlExplode($value, ',');
|
||||||
|
|
||||||
$field = array_pop($a);
|
$field = array_pop($a);
|
||||||
$reference = implode(".",$a);
|
$reference = implode('.', $a);
|
||||||
$table = $this->query->load($reference, false);
|
$table = $this->query->load($reference, false);
|
||||||
array_pop($a);
|
array_pop($a);
|
||||||
$reference2 = implode('.', $a);
|
$reference2 = implode('.', $a);
|
||||||
@ -189,7 +189,12 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
|
|||||||
|
|
||||||
return $operator . ' ('.$this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
|
return $operator . ' ('.$this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* getOperator
|
||||||
|
*
|
||||||
|
* @param string $func
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getOperator($func) {
|
public function getOperator($func) {
|
||||||
switch($func) {
|
switch($func) {
|
||||||
case 'contains':
|
case 'contains':
|
||||||
@ -206,7 +211,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function __toString() {
|
public function __toString() {
|
||||||
return ( ! empty($this->parts))?implode(" AND ", $this->parts):'';
|
return ( ! empty($this->parts))?implode(' AND ', $this->parts):'';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1369,6 +1369,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
|||||||
}
|
}
|
||||||
public function hasIndex($name ) {
|
public function hasIndex($name ) {
|
||||||
|
|
||||||
|
}
|
||||||
|
public function actsAsTree($treeImplName, $args) {
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* addListener
|
* addListener
|
||||||
|
Loading…
Reference in New Issue
Block a user