Skip to content

Instantly share code, notes, and snippets.

@postalservice14
Created July 26, 2012 19:41
Show Gist options
  • Save postalservice14/3184074 to your computer and use it in GitHub Desktop.
Save postalservice14/3184074 to your computer and use it in GitHub Desktop.
ZF2-418
Index: library/Zend/Db/TableGateway/AbstractTableGateway.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
<+><?php\n/**\n * Zend Framework (http://framework.zend.com/)\n *\n * @link http://github.com/zendframework/zf2 for the canonical source repository\n * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)\n * @license http://framework.zend.com/license/new-bsd New BSD License\n * @package Zend_Db\n */\n\nnamespace Zend\\Db\\TableGateway;\n\nuse Zend\\Db\\Adapter\\Adapter;\nuse Zend\\Db\\ResultSet\\ResultSet;\nuse Zend\\Db\\ResultSet\\ResultSetInterface;\nuse Zend\\Db\\Sql\\Delete;\nuse Zend\\Db\\Sql\\Insert;\nuse Zend\\Db\\Sql\\Select;\nuse Zend\\Db\\Sql\\Sql;\nuse Zend\\Db\\Sql\\TableIdentifier;\nuse Zend\\Db\\Sql\\Update;\n\n/**\n * @category Zend\n * @package Zend_Db\n * @subpackage TableGateway\n *\n * @property Adapter $adapter\n * @property int $lastInsertValue\n * @property string $table\n */\nabstract class AbstractTableGateway implements TableGatewayInterface\n{\n\n /**\n * @var bool\n */\n protected $isInitialized = false;\n\n /**\n * @var Adapter\n */\n protected $adapter = null;\n\n /**\n * @var string\n */\n protected $table = null;\n\n /**\n * @var array\n */\n protected $columns = array();\n\n /**\n * @var Feature\\FeatureSet\n */\n protected $featureSet = null;\n\n /**\n * @var ResultSetInterface\n */\n protected $resultSetPrototype = null;\n\n /**\n * @var Sql\\Sql\n */\n protected $sql = null;\n\n /**\n *\n * @var integer\n */\n protected $lastInsertValue = null;\n\n /**\n * @return bool\n */\n public function isInitialized()\n {\n return $this->isInitialized;\n }\n\n /**\n * Initialize\n *\n * @return null\n */\n public function initialize()\n {\n if ($this->isInitialized) {\n return;\n }\n\n if (!$this->featureSet instanceof Feature\\FeatureSet) {\n $this->featureSet = new Feature\\FeatureSet;\n }\n\n $this->featureSet->setTableGateway($this);\n $this->featureSet->apply('preInitialize', array());\n\n if (!$this->adapter instanceof Adapter) {\n throw new Exception\\RuntimeException('This table does not have an Adapter setup');\n }\n\n if (!is_string($this->table) && !$this->table instanceof TableIdentifier) {\n throw new Exception\\RuntimeException('This table object does not have a valid table set.');\n }\n\n if (!$this->resultSetPrototype instanceof ResultSetInterface) {\n $this->resultSetPrototype = new ResultSet;\n }\n\n if (!$this->sql instanceof Sql) {\n $this->sql = new Sql($this->adapter, $this->table);\n }\n\n $this->featureSet->apply('postInitialize', array());\n\n $this->isInitialized = true;\n }\n\n /**\n * Get table name\n *\n * @return string\n */\n public function getTable()\n {\n return $this->table;\n }\n\n /**\n * Get adapter\n *\n * @return Adapter\n */\n public function getAdapter()\n {\n return $this->adapter;\n }\n\n /**\n * @return array\n */\n public function getColumns()\n {\n return $this->columns;\n }\n\n /**\n * @return Feature\\FeatureSet\n */\n public function getFeatureSet()\n {\n return $this->featureSet;\n }\n\n /**\n * Get select result prototype\n *\n * @return ResultSet\n */\n public function getResultSetPrototype()\n {\n return $this->resultSetPrototype;\n }\n\n /**\n * @return Sql\n */\n public function getSql()\n {\n return $this->sql;\n }\n\n /**\n * Select\n *\n * @param string|array|\\Closure $where\n * @return ResultSet\n */\n public function select($where = null)\n {\n if (!$this->isInitialized) {\n $this->initialize();\n }\n\n $select = $this->sql->select();\n\n if ($where instanceof \\Closure) {\n $where($select);\n } elseif ($where !== null) {\n $select->where($where);\n }\n\n return $this->selectWith($select);\n }\n\n /**\n * @param Sql\\Select $select\n * @return null|ResultSetInterface\n * @throws \\RuntimeException\n */\n public function selectWith(Select $select)\n {\n if (!$this->isInitialized) {\n $this->initialize();\n }\n return $this->executeSelect($select);\n }\n\n /**\n * @param Select $select\n * @return ResultSet\n * @throws \\RuntimeException\n */\n protected function executeSelect(Select $select)\n {\n $selectState = $select->getRawState();\n if ($selectState['table'] != $this->table) {\n throw new \\RuntimeException('The table name of the provided select object must match that of the table');\n }\n\n if ($selectState['columns'] == array(Select::SQL_STAR)\n && $this->columns !== array()) {\n $select->columns($this->columns);\n }\n\n // apply preSelect features\n $this->featureSet->apply('preSelect', array($select));\n\n // prepare and execute\n $statement = $this->sql->prepareStatementForSqlObject($select);\n $result = $statement->execute();\n\n // build result set\n $resultSet = clone $this->resultSetPrototype;\n $resultSet->initialize($result);\n\n // apply postSelect features\n $this->featureSet->apply('postSelect', array($statement, $result, $resultSet));\n\n return $resultSet;\n }\n\n /**\n * Insert\n *\n * @param array $set\n * @return int\n */\n public function insert($set)\n {\n if (!$this->isInitialized) {\n $this->initialize();\n }\n $insert = $this->sql->insert();\n $insert->values($set);\n return $this->executeInsert($insert);\n }\n\n /**\n * @param Insert $insert\n * @return mixed\n */\n public function insertWith(Insert $insert)\n {\n if (!$this->isInitialized) {\n $this->initialize();\n }\n return $this->executeInsert($insert);\n }\n\n /**\n * @todo add $columns support\n *\n * @param Insert $insert\n * @return mixed\n * @throws Exception\\RuntimeException\n */\n protected function executeInsert(Insert $insert)\n {\n $insertState = $insert->getRawState();\n if ($insertState['table'] != $this->table) {\n throw new Exception\\RuntimeException('The table name of the provided Insert object must match that of the table');\n }\n\n // apply preInsert features\n $this->featureSet->apply('preInsert', array($insert));\n\n $statement = $this->sql->prepareStatementForSqlObject($insert);\n $result = $statement->execute();\n $this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue();\n\n // apply postInsert features\n $this->featureSet->apply('postInsert', array($statement, $result));\n\n return $result->getAffectedRows();\n }\n\n /**\n * Update\n *\n * @param array $set\n * @param string|array|closure $where\n * @return int\n */\n public function update($set, $where = null)\n {\n if (!$this->isInitialized) {\n $this->initialize();\n }\n $sql = $this->sql;\n $update = $sql->update();\n $update->set($set);\n $update->where($where);\n return $this->executeUpdate($update);\n }\n\n /**\n * @param \\Zend\\Db\\Sql\\Update $update\n * @return mixed\n */\n public function updateWith(Update $update)\n {\n if (!$this->isInitialized) {\n $this->initialize();\n }\n return $this->executeUpdate($update);\n }\n\n /**\n * @todo add $columns support\n *\n * @param Update $update\n * @return mixed\n * @throws Exception\\RuntimeException\n */\n protected function executeUpdate(Update $update)\n {\n $updateState = $update->getRawState();\n if ($updateState['table'] != $this->table) {\n throw new Exception\\RuntimeException('The table name of the provided Update object must match that of the table');\n }\n\n // apply preUpdate features\n $this->featureSet->apply('preUpdate', array($update));\n\n $statement = $this->sql->prepareStatementForSqlObject($update);\n $result = $statement->execute();\n\n // apply postUpdate features\n $this->featureSet->apply('postUpdate', array($statement, $result));\n\n return $result->getAffectedRows();\n }\n\n /**\n * Delete\n *\n * @param Closure $where\n * @return int\n */\n public function delete($where)\n {\n if (!$this->isInitialized) {\n $this->initialize();\n }\n $delete = $this->sql->delete();\n if ($where instanceof \\Closure) {\n $where($delete);\n } else {\n $delete->where($where);\n }\n return $this->executeDelete($delete);\n }\n\n /**\n * @param Delete $delete\n * @return mixed\n */\n public function deleteWith(Delete $delete)\n {\n $this->initialize();\n return $this->executeDelete($delete);\n }\n\n /**\n * @todo add $columns support\n *\n * @param Delete $delete\n * @return mixed\n * @throws Exception\\RuntimeException\n */\n protected function executeDelete(Delete $delete)\n {\n $deleteState = $delete->getRawState();\n if ($deleteState['table'] != $this->table) {\n throw new Exception\\RuntimeException('The table name of the provided Update object must match that of the table');\n }\n\n // pre delete update\n $this->featureSet->apply('preDelete', array($delete));\n\n $statement = $this->sql->prepareStatementForSqlObject($delete);\n $result = $statement->execute();\n\n // apply postDelete features\n $this->featureSet->apply('postDelete', array($statement, $result));\n\n return $result->getAffectedRows();\n }\n\n /**\n * Get last insert value\n *\n * @return integer\n */\n public function getLastInsertValue()\n {\n return $this->lastInsertValue;\n }\n\n /**\n * __get\n *\n * @param string $property\n * @return mixed\n */\n public function __get($property)\n {\n switch (strtolower($property)) {\n case 'lastinsertvalue':\n return $this->lastInsertValue;\n case 'adapter':\n return $this->adapter;\n case 'table':\n return $this->table;\n }\n if ($this->featureSet->canCallMagicGet($property)) {\n return $this->featureSet->callMagicGet($property);\n }\n throw new Exception\\InvalidArgumentException('Invalid magic property access in ' . __CLASS__ . '::__get()');\n }\n\n /**\n * @param $property\n * @return mixed\n * @throws Exception\\InvalidArgumentException\n */\n public function __set($property, $value)\n {\n if ($this->featureSet->canCallMagicSet($property)) {\n return $this->featureSet->callMagicSet($property, $value);\n }\n throw new Exception\\InvalidArgumentException('Invalid magic property access in ' . __CLASS__ . '::__set()');\n }\n\n /**\n * @param $method\n * @param $arguments\n * @return mixed\n * @throws Exception\\InvalidArgumentException\n */\n public function __call($method, $arguments)\n {\n if ($this->featureSet->canCallMagicCall($method)) {\n return $this->featureSet->callMagicCall($method, $arguments);\n }\n throw new Exception\\InvalidArgumentException('Invalid method (' . $method . ') called, caught by ' . __CLASS__ . '::__call()');\n }\n\n /**\n * __clone\n */\n public function __clone()\n {\n $this->resultSetPrototype = (isset($this->resultSetPrototype)) ? clone $this->resultSetPrototype : null;\n $this->sql = clone $this->sql;\n if (is_object($this->table)) {\n $this->table = clone $this->table;\n }\n }\n\n}\n
===================================================================
--- library/Zend/Db/TableGateway/AbstractTableGateway.php (revision 0de60c6bb282c2a1dc63a99da936526f83e29509)
+++ library/Zend/Db/TableGateway/AbstractTableGateway.php (revision )
@@ -314,7 +314,13 @@
$sql = $this->sql;
$update = $sql->update();
$update->set($set);
+
+ if (is_null($where)) {
+ $update->where('1 = 1');
+ } else {
- $update->where($where);
+ $update->where($where);
+ }
+
return $this->executeUpdate($update);
}
@postalservice14
Copy link
Author

Or this (slightly more elegant):

$where = is_null($where) ? '1 = 1' : $where;
$update->where($where);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment