DQL now supports UPDATE queries
This commit is contained in:
parent
50226697e0
commit
84bbfccbe2
@ -314,7 +314,7 @@ final class Doctrine {
|
||||
* @param string $directory
|
||||
*/
|
||||
public static function import($directory) {
|
||||
|
||||
Doctrine_Import::import();
|
||||
}
|
||||
/**
|
||||
* export
|
||||
@ -323,7 +323,7 @@ final class Doctrine {
|
||||
* @param string $directory
|
||||
*/
|
||||
public static function export($directory) {
|
||||
|
||||
Doctrine_Export::export();
|
||||
}
|
||||
/**
|
||||
* compile
|
||||
|
@ -276,7 +276,10 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module implemen
|
||||
$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->execute($params);
|
||||
|
@ -585,8 +585,11 @@ class Doctrine_Export extends Doctrine_Connection_Module {
|
||||
}
|
||||
/**
|
||||
* export
|
||||
* method for exporting Doctrine_Record classes to a schema
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function export() {
|
||||
public static function export() {
|
||||
$parent = new ReflectionClass('Doctrine_Record');
|
||||
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
|
||||
$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
|
||||
*/
|
||||
protected $parts = array(
|
||||
"select" => array(),
|
||||
"from" => array(),
|
||||
"join" => array(),
|
||||
"where" => array(),
|
||||
"groupby" => array(),
|
||||
"having" => array(),
|
||||
"orderby" => array(),
|
||||
"limit" => false,
|
||||
"offset" => false,
|
||||
'select' => array(),
|
||||
'from' => array(),
|
||||
'set' => array(),
|
||||
'join' => array(),
|
||||
'where' => array(),
|
||||
'groupby' => array(),
|
||||
'having' => array(),
|
||||
'orderby' => array(),
|
||||
'limit' => false,
|
||||
'offset' => false,
|
||||
);
|
||||
/**
|
||||
* constructor
|
||||
|
@ -137,8 +137,13 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
|
||||
}
|
||||
/**
|
||||
* connection
|
||||
*
|
||||
* if the adapter parameter is set this method acts as
|
||||
* 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 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
|
||||
@ -150,7 +155,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
|
||||
} else {
|
||||
return Doctrine_Manager::getInstance()->openConnection($adapter, $name);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* openConnection
|
||||
* opens a new connection and saves it to Doctrine_Manager->connections
|
||||
|
@ -320,7 +320,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
||||
|
||||
|
||||
|
||||
$method = "parse".ucwords($name);
|
||||
$method = 'parse' . ucwords($name);
|
||||
|
||||
switch($name) {
|
||||
case 'select':
|
||||
@ -399,48 +399,19 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
||||
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 $value query part value
|
||||
* @return boolean
|
||||
* @param $name name of the field
|
||||
* @param $value field value
|
||||
* @return Doctrine_Query
|
||||
*/
|
||||
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->parse($name, $value);
|
||||
$this->parts['set'][] = $class->parse($name . ' = ' . $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @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();
|
||||
|
||||
if( ! empty($string))
|
||||
@ -745,8 +720,8 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
||||
$this->clear();
|
||||
|
||||
$query = trim($query);
|
||||
$query = str_replace("\n"," ",$query);
|
||||
$query = str_replace("\r"," ",$query);
|
||||
$query = str_replace("\n", ' ', $query);
|
||||
$query = str_replace("\r", ' ', $query);
|
||||
|
||||
$parts = $this->splitQuery($query);
|
||||
|
||||
@ -756,7 +731,6 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
||||
case 'DELETE':
|
||||
$this->type = self::DELETE;
|
||||
break;
|
||||
|
||||
case 'SELECT':
|
||||
$this->type = self::SELECT;
|
||||
$this->parseSelect($part);
|
||||
@ -766,27 +740,31 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
||||
$k = 'FROM';
|
||||
|
||||
case 'FROM':
|
||||
case 'SET':
|
||||
$class = 'Doctrine_Query_'.ucwords(strtolower($k));
|
||||
$class = 'Doctrine_Query_' . ucwords(strtolower($k));
|
||||
$parser = new $class($this);
|
||||
$parser->parse($part);
|
||||
break;
|
||||
case 'SET':
|
||||
$class = 'Doctrine_Query_' . ucwords(strtolower($k));
|
||||
$parser = new $class($this);
|
||||
$this->parts['set'][] = $parser->parse($part);
|
||||
break;
|
||||
case 'GROUP':
|
||||
case 'ORDER':
|
||||
$k .= "by";
|
||||
$k .= 'by';
|
||||
case 'WHERE':
|
||||
case 'HAVING':
|
||||
$class = "Doctrine_Query_".ucwords(strtolower($k));
|
||||
$class = 'Doctrine_Query_' . ucwords(strtolower($k));
|
||||
$parser = new $class($this);
|
||||
|
||||
$name = strtolower($k);
|
||||
$this->parts[$name][] = $parser->parse($part);
|
||||
break;
|
||||
case 'LIMIT':
|
||||
$this->parts["limit"] = trim($part);
|
||||
$this->parts['limit'] = trim($part);
|
||||
break;
|
||||
case 'OFFSET':
|
||||
$this->parts["offset"] = trim($part);
|
||||
$this->parts['offset'] = trim($part);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -21,17 +21,33 @@
|
||||
/**
|
||||
* Doctrine_Query
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
class Doctrine_Query_Set extends Doctrine_Query_Part {
|
||||
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);
|
||||
$operator = trim(substr($where, strlen($r), -strlen($value)));
|
||||
|
||||
$reference = implode(".",$a);
|
||||
$reference = implode('.', $a);
|
||||
$count = count($a);
|
||||
|
||||
|
||||
@ -51,7 +51,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
|
||||
$values = Doctrine_Query::sqlExplode($value, ',');
|
||||
|
||||
$field = array_pop($a);
|
||||
$reference = implode(".",$a);
|
||||
$reference = implode('.', $a);
|
||||
$table = $this->query->load($reference, false);
|
||||
array_pop($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() . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* getOperator
|
||||
*
|
||||
* @param string $func
|
||||
* @return string
|
||||
*/
|
||||
public function getOperator($func) {
|
||||
switch($func) {
|
||||
case 'contains':
|
||||
@ -206,7 +211,7 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
|
||||
}
|
||||
|
||||
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 actsAsTree($treeImplName, $args) {
|
||||
|
||||
}
|
||||
/**
|
||||
* addListener
|
||||
|
Loading…
Reference in New Issue
Block a user