#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?m)\s+\w+\s+implements\s+(.*)\s+\{"
Local $sString = "<?php " & @CRLF & _
"namespace Zend\Db\Table\Row;" & @CRLF & _
"/**" & @CRLF & _
" * Zend Framework" & @CRLF & _
" *" & @CRLF & _
" * LICENSE" & @CRLF & _
" *" & @CRLF & _
" * This source file is subject to the new BSD license that is bundled" & @CRLF & _
" * with this package in the file LICENSE.txt." & @CRLF & _
" * It is also available through the world-wide-web at this URL:" & @CRLF & _
" * http://framework.zend.com/license/new-bsd" & @CRLF & _
" * If you did not receive a copy of the license and are unable to" & @CRLF & _
" * obtain it through the world-wide-web, please send an email" & @CRLF & _
" * to license@zend.com so we can send you a copy immediately." & @CRLF & _
" *" & @CRLF & _
" * @category Zend" & @CRLF & _
" * @package Zend_Db" & @CRLF & _
" * @subpackage Table" & @CRLF & _
" * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)" & @CRLF & _
" * @license http://framework.zend.com/license/new-bsd New BSD License" & @CRLF & _
" * @version $Id$" & @CRLF & _
" */" & @CRLF & _
"" & @CRLF & _
"/**" & @CRLF & _
" * @see Zend_Db" & @CRLF & _
" */" & @CRLF & _
"require_once 'Zend/Db.php';" & @CRLF & _
"" & @CRLF & _
"/**" & @CRLF & _
" * @category Zend" & @CRLF & _
" * @package Zend_Db" & @CRLF & _
" * @subpackage Table" & @CRLF & _
" * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)" & @CRLF & _
" * @license http://framework.zend.com/license/new-bsd New BSD License" & @CRLF & _
" */" & @CRLF & _
"abstract class RowAbstract implements \ArrayAccess, IteratorAggregate" & @CRLF & _
"{" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * The data for each column in the row (column_name => value)." & @CRLF & _
" * The keys must match the physical names of columns in the" & @CRLF & _
" * table for which this row is defined." & @CRLF & _
" *" & @CRLF & _
" * @var array" & @CRLF & _
" */" & @CRLF & _
" protected $_data = array();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * This is set to a copy of $_data when the data is fetched from" & @CRLF & _
" * a database, specified as a new tuple in the constructor, or" & @CRLF & _
" * when dirty data is posted to the database with save()." & @CRLF & _
" *" & @CRLF & _
" * @var array" & @CRLF & _
" */" & @CRLF & _
" protected $_cleanData = array();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Tracks columns where data has been updated. Allows more specific insert and" & @CRLF & _
" * update operations." & @CRLF & _
" *" & @CRLF & _
" * @var array" & @CRLF & _
" */" & @CRLF & _
" protected $_modifiedFields = array();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * \Zend\Db\Table\TableAbstract parent class or instance." & @CRLF & _
" *" & @CRLF & _
" * @var \Zend\Db\Table\TableAbstract" & @CRLF & _
" */" & @CRLF & _
" protected $_table = null;" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Connected is true if we have a reference to a live" & @CRLF & _
" * \Zend\Db\Table\TableAbstract object." & @CRLF & _
" * This is false after the Rowset has been deserialized." & @CRLF & _
" *" & @CRLF & _
" * @var boolean" & @CRLF & _
" */" & @CRLF & _
" protected $_connected = true;" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * A row is marked read only if it contains columns that are not physically represented within" & @CRLF & _
" * the database schema (e.g. evaluated columns/Zend_Db_Expr columns). This can also be passed" & @CRLF & _
" * as a run-time config options as a means of protecting row data." & @CRLF & _
" *" & @CRLF & _
" * @var boolean" & @CRLF & _
" */" & @CRLF & _
" protected $_readOnly = false;" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Name of the class of the \Zend\Db\Table\TableAbstract object." & @CRLF & _
" *" & @CRLF & _
" * @var string" & @CRLF & _
" */" & @CRLF & _
" protected $_tableClass = null;" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Primary row key(s)." & @CRLF & _
" *" & @CRLF & _
" * @var array" & @CRLF & _
" */" & @CRLF & _
" protected $_primary;" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Constructor." & @CRLF & _
" *" & @CRLF & _
" * Supported params for $config are:-" & @CRLF & _
" * - table = class name or object of type \Zend\Db\Table\TableAbstract" & @CRLF & _
" * - data = values of columns in this row." & @CRLF & _
" *" & @CRLF & _
" * @param array $config OPTIONAL Array of user-specified config options." & @CRLF & _
" * @return void" & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception" & @CRLF & _
" */" & @CRLF & _
" public function __construct(array $config = array())" & @CRLF & _
" {" & @CRLF & _
" if (isset($config['table']) && $config['table'] instanceof \Zend\Db\Table\TableAbstract) {" & @CRLF & _
" $this->_table = $config['table'];" & @CRLF & _
" $this->_tableClass = get_class($this->_table);" & @CRLF & _
" } elseif ($this->_tableClass !== null) {" & @CRLF & _
" $this->_table = $this->_getTableFromString($this->_tableClass);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if (isset($config['data'])) {" & @CRLF & _
" if (!is_array($config['data'])) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException('Data must be an array');" & @CRLF & _
" }" & @CRLF & _
" $this->_data = $config['data'];" & @CRLF & _
" }" & @CRLF & _
" if (isset($config['stored']) && $config['stored'] === true) {" & @CRLF & _
" $this->_cleanData = $this->_data;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if (isset($config['readOnly']) && $config['readOnly'] === true) {" & @CRLF & _
" $this->setReadOnly(true);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" // Retrieve primary keys from table schema" & @CRLF & _
" if (($table = $this->_getTable())) {" & @CRLF & _
" $info = $table->info();" & @CRLF & _
" $this->_primary = (array) $info['primary'];" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $this->init();" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Transform a column name from the user-specified form" & @CRLF & _
" * to the physical form used in the database." & @CRLF & _
" * You can override this method in a custom Row class" & @CRLF & _
" * to implement column name mappings, for example inflection." & @CRLF & _
" *" & @CRLF & _
" * @param string $columnName Column name given." & @CRLF & _
" * @return string The column name after transformation applied (none by default)." & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception if the $columnName is not a string." & @CRLF & _
" */" & @CRLF & _
" protected function _transformColumn($columnName)" & @CRLF & _
" {" & @CRLF & _
" if (!is_string($columnName)) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException('Specified column is not a string');" & @CRLF & _
" }" & @CRLF & _
" // Perform no transformation by default" & @CRLF & _
" return $columnName;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Retrieve row field value" & @CRLF & _
" *" & @CRLF & _
" * @param string $columnName The user-specified column name." & @CRLF & _
" * @return string The corresponding column value." & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception if the $columnName is not a column in the row." & @CRLF & _
" */" & @CRLF & _
" public function __get($columnName)" & @CRLF & _
" {" & @CRLF & _
" $columnName = $this->_transformColumn($columnName);" & @CRLF & _
" if (!array_key_exists($columnName, $this->_data)) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Specified column \"$columnName\" is not in the row");" & @CRLF & _
" }" & @CRLF & _
" return $this->_data[$columnName];" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Set row field value" & @CRLF & _
" *" & @CRLF & _
" * @param string $columnName The column key." & @CRLF & _
" * @param mixed $value The value for the property." & @CRLF & _
" * @return void" & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception" & @CRLF & _
" */" & @CRLF & _
" public function __set($columnName, $value)" & @CRLF & _
" {" & @CRLF & _
" $columnName = $this->_transformColumn($columnName);" & @CRLF & _
" if (!array_key_exists($columnName, $this->_data)) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Specified column \"$columnName\" is not in the row");" & @CRLF & _
" }" & @CRLF & _
" $this->_data[$columnName] = $value;" & @CRLF & _
" $this->_modifiedFields[$columnName] = true;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Unset row field value" & @CRLF & _
" *" & @CRLF & _
" * @param string $columnName The column key." & @CRLF & _
" * @return Zend_Db_Table_Row_Abstract" & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception" & @CRLF & _
" */" & @CRLF & _
" public function __unset($columnName)" & @CRLF & _
" {" & @CRLF & _
" $columnName = $this->_transformColumn($columnName);" & @CRLF & _
" if (!array_key_exists($columnName, $this->_data)) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Specified column \"$columnName\" is not in the row");" & @CRLF & _
" }" & @CRLF & _
" if ($this->isConnected() && in_array($columnName, $this->_table->info('primary'))) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Specified column \"$columnName\" is a primary key and should not be unset");" & @CRLF & _
" }" & @CRLF & _
" unset($this->_data[$columnName]);" & @CRLF & _
" return $this;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Test existence of row field" & @CRLF & _
" *" & @CRLF & _
" * @param string $columnName The column key." & @CRLF & _
" * @return boolean" & @CRLF & _
" */" & @CRLF & _
" public function __isset($columnName)" & @CRLF & _
" {" & @CRLF & _
" $columnName = $this->_transformColumn($columnName);" & @CRLF & _
" return array_key_exists($columnName, $this->_data);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Store table, primary key and data in serialized object" & @CRLF & _
" *" & @CRLF & _
" * @return array" & @CRLF & _
" */" & @CRLF & _
" public function __sleep()" & @CRLF & _
" {" & @CRLF & _
" return array('_tableClass', '_primary', '_data', '_cleanData', '_readOnly' ,'_modifiedFields');" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Setup to do on wakeup." & @CRLF & _
" * A de-serialized Row should not be assumed to have access to a live" & @CRLF & _
" * database connection, so set _connected = false." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" public function __wakeup()" & @CRLF & _
" {" & @CRLF & _
" $this->_connected = false;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Proxy to __isset" & @CRLF & _
" * Required by the ArrayAccess implementation" & @CRLF & _
" *" & @CRLF & _
" * @param string $offset" & @CRLF & _
" * @return boolean" & @CRLF & _
" */" & @CRLF & _
" public function offsetExists($offset)" & @CRLF & _
" {" & @CRLF & _
" return $this->__isset($offset);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Proxy to __get" & @CRLF & _
" * Required by the ArrayAccess implementation" & @CRLF & _
" *" & @CRLF & _
" * @param string $offset" & @CRLF & _
" * @return string" & @CRLF & _
" */" & @CRLF & _
" public function offsetGet($offset)" & @CRLF & _
" {" & @CRLF & _
" return $this->__get($offset);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Proxy to __set" & @CRLF & _
" * Required by the ArrayAccess implementation" & @CRLF & _
" *" & @CRLF & _
" * @param string $offset" & @CRLF & _
" * @param mixed $value" & @CRLF & _
" */" & @CRLF & _
" public function offsetSet($offset, $value)" & @CRLF & _
" {" & @CRLF & _
" $this->__set($offset, $value);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Proxy to __unset" & @CRLF & _
" * Required by the ArrayAccess implementation" & @CRLF & _
" *" & @CRLF & _
" * @param string $offset" & @CRLF & _
" */" & @CRLF & _
" public function offsetUnset($offset)" & @CRLF & _
" {" & @CRLF & _
" return $this->__unset($offset);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Initialize object" & @CRLF & _
" *" & @CRLF & _
" * Called from {@link __construct()} as final step of object instantiation." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" public function init()" & @CRLF & _
" {" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Returns the table object, or null if this is disconnected row" & @CRLF & _
" *" & @CRLF & _
" * @return \Zend\Db\Table\TableAbstract|null" & @CRLF & _
" */" & @CRLF & _
" public function getTable()" & @CRLF & _
" {" & @CRLF & _
" return $this->_table;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Set the table object, to re-establish a live connection" & @CRLF & _
" * to the database for a Row that has been de-serialized." & @CRLF & _
" *" & @CRLF & _
" * @param \Zend\Db\Table\TableAbstract $table" & @CRLF & _
" * @return boolean" & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception" & @CRLF & _
" */" & @CRLF & _
" public function setTable(\Zend\Db\Table\TableAbstract $table = null)" & @CRLF & _
" {" & @CRLF & _
" if ($table == null) {" & @CRLF & _
" $this->_table = null;" & @CRLF & _
" $this->_connected = false;" & @CRLF & _
" return false;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $tableClass = get_class($table);" & @CRLF & _
" if (! $table instanceof $this->_tableClass) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("The specified Table is of class $tableClass, expecting class to be instance of $this->_tableClass");" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $this->_table = $table;" & @CRLF & _
" $this->_tableClass = $tableClass;" & @CRLF & _
"" & @CRLF & _
" $info = $this->_table->info();" & @CRLF & _
"" & @CRLF & _
" if ($info['cols'] != array_keys($this->_data)) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException('The specified Table does not have the same columns as the Row');" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if (! array_intersect((array) $this->_primary, $info['primary']) == (array) $this->_primary) {" & @CRLF & _
"" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("The specified Table '$tableClass' does not have the same primary key as the Row");" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $this->_connected = true;" & @CRLF & _
" return true;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Query the class name of the Table object for which this" & @CRLF & _
" * Row was created." & @CRLF & _
" *" & @CRLF & _
" * @return string" & @CRLF & _
" */" & @CRLF & _
" public function getTableClass()" & @CRLF & _
" {" & @CRLF & _
" return $this->_tableClass;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Test the connected status of the row." & @CRLF & _
" *" & @CRLF & _
" * @return boolean" & @CRLF & _
" */" & @CRLF & _
" public function isConnected()" & @CRLF & _
" {" & @CRLF & _
" return $this->_connected;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Test the read-only status of the row." & @CRLF & _
" *" & @CRLF & _
" * @return boolean" & @CRLF & _
" */" & @CRLF & _
" public function isReadOnly()" & @CRLF & _
" {" & @CRLF & _
" return $this->_readOnly;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Set the read-only status of the row." & @CRLF & _
" *" & @CRLF & _
" * @param boolean $flag" & @CRLF & _
" * @return boolean" & @CRLF & _
" */" & @CRLF & _
" public function setReadOnly($flag)" & @CRLF & _
" {" & @CRLF & _
" $this->_readOnly = (bool) $flag;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Returns an instance of the parent table's \Zend\Db\Table\Select object." & @CRLF & _
" *" & @CRLF & _
" * @return \Zend\Db\Table\Select" & @CRLF & _
" */" & @CRLF & _
" public function select()" & @CRLF & _
" {" & @CRLF & _
" return $this->getTable()->select();" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Saves the properties to the database." & @CRLF & _
" *" & @CRLF & _
" * This performs an intelligent insert/update, and reloads the" & @CRLF & _
" * properties with fresh data from the table on success." & @CRLF & _
" *" & @CRLF & _
" * @return mixed The primary key value(s), as an associative array if the" & @CRLF & _
" * key is compound, or a scalar if the key is single-column." & @CRLF & _
" */" & @CRLF & _
" public function save()" & @CRLF & _
" {" & @CRLF & _
" /**" & @CRLF & _
" * If the _cleanData array is empty," & @CRLF & _
" * this is an INSERT of a new row." & @CRLF & _
" * Otherwise it is an UPDATE." & @CRLF & _
" */" & @CRLF & _
" if (empty($this->_cleanData)) {" & @CRLF & _
" return $this->_doInsert();" & @CRLF & _
" } else {" & @CRLF & _
" return $this->_doUpdate();" & @CRLF & _
" }" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * @return mixed The primary key value(s), as an associative array if the" & @CRLF & _
" * key is compound, or a scalar if the key is single-column." & @CRLF & _
" */" & @CRLF & _
" protected function _doInsert()" & @CRLF & _
" {" & @CRLF & _
" /**" & @CRLF & _
" * A read-only row cannot be saved." & @CRLF & _
" */" & @CRLF & _
" if ($this->_readOnly === true) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException('This row has been marked read-only');" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Run pre-INSERT logic" & @CRLF & _
" */" & @CRLF & _
" $this->_insert();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Execute the INSERT (this may throw an exception)" & @CRLF & _
" */" & @CRLF & _
" $data = array_intersect_key($this->_data, $this->_modifiedFields);" & @CRLF & _
" $primaryKey = $this->_getTable()->insert($data);" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Normalize the result to an array indexed by primary key column(s)." & @CRLF & _
" * The table insert() method may return a scalar." & @CRLF & _
" */" & @CRLF & _
" if (is_array($primaryKey)) {" & @CRLF & _
" $newPrimaryKey = $primaryKey;" & @CRLF & _
" } else {" & @CRLF & _
" //ZF-6167 Use tempPrimaryKey temporary to avoid that zend encoding fails." & @CRLF & _
" $tempPrimaryKey = (array) $this->_primary;" & @CRLF & _
" $newPrimaryKey = array(current($tempPrimaryKey) => $primaryKey);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Save the new primary key value in _data. The primary key may have" & @CRLF & _
" * been generated by a sequence or auto-increment mechanism, and this" & @CRLF & _
" * merge should be done before the _postInsert() method is run, so the" & @CRLF & _
" * new values are available for logging, etc." & @CRLF & _
" */" & @CRLF & _
" $this->_data = array_merge($this->_data, $newPrimaryKey);" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Run post-INSERT logic" & @CRLF & _
" */" & @CRLF & _
" $this->_postInsert();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Update the _cleanData to reflect that the data has been inserted." & @CRLF & _
" */" & @CRLF & _
" $this->_refresh();" & @CRLF & _
"" & @CRLF & _
" return $primaryKey;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * @return mixed The primary key value(s), as an associative array if the" & @CRLF & _
" * key is compound, or a scalar if the key is single-column." & @CRLF & _
" */" & @CRLF & _
" protected function _doUpdate()" & @CRLF & _
" {" & @CRLF & _
" /**" & @CRLF & _
" * A read-only row cannot be saved." & @CRLF & _
" */" & @CRLF & _
" if ($this->_readOnly === true) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException('This row has been marked read-only');" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Get expressions for a WHERE clause" & @CRLF & _
" * based on the primary key value(s)." & @CRLF & _
" */" & @CRLF & _
" $where = $this->_getWhereQuery(false);" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Run pre-UPDATE logic" & @CRLF & _
" */" & @CRLF & _
" $this->_update();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Compare the data to the modified fields array to discover" & @CRLF & _
" * which columns have been changed." & @CRLF & _
" */" & @CRLF & _
" $diffData = array_intersect_key($this->_data, $this->_modifiedFields);" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Were any of the changed columns part of the primary key?" & @CRLF & _
" */" & @CRLF & _
" $pkDiffData = array_intersect_key($diffData, array_flip((array)$this->_primary));" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Execute cascading updates against dependent tables." & @CRLF & _
" * Do this only if primary key value(s) were changed." & @CRLF & _
" */" & @CRLF & _
" if (count($pkDiffData) > 0) {" & @CRLF & _
" $depTables = $this->_getTable()->getDependentTables();" & @CRLF & _
" if (!empty($depTables)) {" & @CRLF & _
" $pkNew = $this->_getPrimaryKey(true);" & @CRLF & _
" $pkOld = $this->_getPrimaryKey(false);" & @CRLF & _
" foreach ($depTables as $tableClass) {" & @CRLF & _
" $t = $this->_getTableFromString($tableClass);" & @CRLF & _
" $t->_cascadeUpdate($this->getTableClass(), $pkOld, $pkNew);" & @CRLF & _
" }" & @CRLF & _
" }" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Execute the UPDATE (this may throw an exception)" & @CRLF & _
" * Do this only if data values were changed." & @CRLF & _
" * Use the $diffData variable, so the UPDATE statement" & @CRLF & _
" * includes SET terms only for data values that changed." & @CRLF & _
" */" & @CRLF & _
" if (count($diffData) > 0) {" & @CRLF & _
" $this->_getTable()->update($diffData, $where);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Run post-UPDATE logic. Do this before the _refresh()" & @CRLF & _
" * so the _postUpdate() function can tell the difference" & @CRLF & _
" * between changed data and clean (pre-changed) data." & @CRLF & _
" */" & @CRLF & _
" $this->_postUpdate();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Refresh the data just in case triggers in the RDBMS changed" & @CRLF & _
" * any columns. Also this resets the _cleanData." & @CRLF & _
" */" & @CRLF & _
" $this->_refresh();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Return the primary key value(s) as an array" & @CRLF & _
" * if the key is compound or a scalar if the key" & @CRLF & _
" * is a scalar." & @CRLF & _
" */" & @CRLF & _
" $primaryKey = $this->_getPrimaryKey(true);" & @CRLF & _
" if (count($primaryKey) == 1) {" & @CRLF & _
" return current($primaryKey);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" return $primaryKey;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Deletes existing rows." & @CRLF & _
" *" & @CRLF & _
" * @return int The number of rows deleted." & @CRLF & _
" */" & @CRLF & _
" public function delete()" & @CRLF & _
" {" & @CRLF & _
" /**" & @CRLF & _
" * A read-only row cannot be deleted." & @CRLF & _
" */" & @CRLF & _
" if ($this->_readOnly === true) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException('This row has been marked read-only');" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $where = $this->_getWhereQuery();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Execute pre-DELETE logic" & @CRLF & _
" */" & @CRLF & _
" $this->_delete();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Execute cascading deletes against dependent tables" & @CRLF & _
" */" & @CRLF & _
" $depTables = $this->_getTable()->getDependentTables();" & @CRLF & _
" if (!empty($depTables)) {" & @CRLF & _
" $pk = $this->_getPrimaryKey();" & @CRLF & _
" foreach ($depTables as $tableClass) {" & @CRLF & _
" $t = $this->_getTableFromString($tableClass);" & @CRLF & _
" $t->_cascadeDelete($this->getTableClass(), $pk);" & @CRLF & _
" }" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Execute the DELETE (this may throw an exception)" & @CRLF & _
" */" & @CRLF & _
" $result = $this->_getTable()->delete($where);" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Execute post-DELETE logic" & @CRLF & _
" */" & @CRLF & _
" $this->_postDelete();" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Reset all fields to null to indicate that the row is not there" & @CRLF & _
" */" & @CRLF & _
" $this->_data = array_combine(" & @CRLF & _
" array_keys($this->_data)," & @CRLF & _
" array_fill(0, count($this->_data), null)" & @CRLF & _
" );" & @CRLF & _
"" & @CRLF & _
" return $result;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" public function getIterator()" & @CRLF & _
" {" & @CRLF & _
" return new \ArrayIterator((array) $this->_data);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Returns the column/value data as an array." & @CRLF & _
" *" & @CRLF & _
" * @return array" & @CRLF & _
" */" & @CRLF & _
" public function toArray()" & @CRLF & _
" {" & @CRLF & _
" return (array)$this->_data;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Sets all data in the row from an array." & @CRLF & _
" *" & @CRLF & _
" * @param array $data" & @CRLF & _
" * @return Zend_Db_Table_Row_Abstract Provides a fluent interface" & @CRLF & _
" */" & @CRLF & _
" public function setFromArray(array $data)" & @CRLF & _
" {" & @CRLF & _
" $data = array_intersect_key($data, $this->_data);" & @CRLF & _
"" & @CRLF & _
" foreach ($data as $columnName => $value) {" & @CRLF & _
" $this->__set($columnName, $value);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" return $this;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Refreshes properties from the database." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" public function refresh()" & @CRLF & _
" {" & @CRLF & _
" return $this->_refresh();" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Retrieves an instance of the parent table." & @CRLF & _
" *" & @CRLF & _
" * @return \Zend\Db\Table\TableAbstract" & @CRLF & _
" */" & @CRLF & _
" protected function _getTable()" & @CRLF & _
" {" & @CRLF & _
" if (!$this->_connected) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException('Cannot save a Row unless it is connected');" & @CRLF & _
" }" & @CRLF & _
" return $this->_table;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Retrieves an associative array of primary keys." & @CRLF & _
" *" & @CRLF & _
" * @param bool $useDirty" & @CRLF & _
" * @return array" & @CRLF & _
" */" & @CRLF & _
" protected function _getPrimaryKey($useDirty = true)" & @CRLF & _
" {" & @CRLF & _
" if (!is_array($this->_primary)) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("The primary key must be set as an array");" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $primary = array_flip($this->_primary);" & @CRLF & _
" if ($useDirty) {" & @CRLF & _
" $array = array_intersect_key($this->_data, $primary);" & @CRLF & _
" } else {" & @CRLF & _
" $array = array_intersect_key($this->_cleanData, $primary);" & @CRLF & _
" }" & @CRLF & _
" if (count($primary) != count($array)) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("The specified Table '$this->_tableClass' does not have the same primary key as the Row");" & @CRLF & _
" }" & @CRLF & _
" return $array;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Retrieves an associative array of primary keys." & @CRLF & _
" *" & @CRLF & _
" * @param bool $useDirty" & @CRLF & _
" * @return array" & @CRLF & _
" */" & @CRLF & _
" public function getPrimaryKey($useDirty = true)" & @CRLF & _
" {" & @CRLF & _
" return $this->_getPrimaryKey($useDirty);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Constructs where statement for retrieving row(s)." & @CRLF & _
" *" & @CRLF & _
" * @param bool $useDirty" & @CRLF & _
" * @return array" & @CRLF & _
" */" & @CRLF & _
" protected function _getWhereQuery($useDirty = true)" & @CRLF & _
" {" & @CRLF & _
" $where = array();" & @CRLF & _
" $db = $this->_getTable()->getAdapter();" & @CRLF & _
" $primaryKey = $this->_getPrimaryKey($useDirty);" & @CRLF & _
" $info = $this->_getTable()->info();" & @CRLF & _
" $metadata = $info[\Zend\Db\Table\TableAbstract::METADATA];" & @CRLF & _
"" & @CRLF & _
" // retrieve recently updated row using primary keys" & @CRLF & _
" $where = array();" & @CRLF & _
" foreach ($primaryKey as $column => $value) {" & @CRLF & _
" $tableName = $db->quoteIdentifier($info[\Zend\Db\Table\TableAbstract::NAME], true);" & @CRLF & _
" $type = $metadata[$column]['DATA_TYPE'];" & @CRLF & _
" $columnName = $db->quoteIdentifier($column, true);" & @CRLF & _
" $where[] = $db->quoteInto("{$tableName}.{$columnName} = ?", $value, $type);" & @CRLF & _
" }" & @CRLF & _
" return $where;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Refreshes properties from the database." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" protected function _refresh()" & @CRLF & _
" {" & @CRLF & _
" $where = $this->_getWhereQuery();" & @CRLF & _
" $row = $this->_getTable()->fetchRow($where);" & @CRLF & _
"" & @CRLF & _
" if (null === $row) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException('Cannot refresh row as parent is missing');" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $this->_data = $row->toArray();" & @CRLF & _
" $this->_cleanData = $this->_data;" & @CRLF & _
" $this->_modifiedFields = array();" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Allows pre-insert logic to be applied to row." & @CRLF & _
" * Subclasses may override this method." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" protected function _insert()" & @CRLF & _
" {" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Allows post-insert logic to be applied to row." & @CRLF & _
" * Subclasses may override this method." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" protected function _postInsert()" & @CRLF & _
" {" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Allows pre-update logic to be applied to row." & @CRLF & _
" * Subclasses may override this method." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" protected function _update()" & @CRLF & _
" {" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Allows post-update logic to be applied to row." & @CRLF & _
" * Subclasses may override this method." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" protected function _postUpdate()" & @CRLF & _
" {" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Allows pre-delete logic to be applied to row." & @CRLF & _
" * Subclasses may override this method." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" protected function _delete()" & @CRLF & _
" {" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Allows post-delete logic to be applied to row." & @CRLF & _
" * Subclasses may override this method." & @CRLF & _
" *" & @CRLF & _
" * @return void" & @CRLF & _
" */" & @CRLF & _
" protected function _postDelete()" & @CRLF & _
" {" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Prepares a table reference for lookup." & @CRLF & _
" *" & @CRLF & _
" * Ensures all reference keys are set and properly formatted." & @CRLF & _
" *" & @CRLF & _
" * @param \Zend\Db\Table\TableAbstract $dependentTable" & @CRLF & _
" * @param \Zend\Db\Table\TableAbstract $parentTable" & @CRLF & _
" * @param string $ruleKey" & @CRLF & _
" * @return array" & @CRLF & _
" */" & @CRLF & _
" protected function _prepareReference(\Zend\Db\Table\TableAbstract $dependentTable, \Zend\Db\Table\TableAbstract $parentTable, $ruleKey)" & @CRLF & _
" {" & @CRLF & _
" $parentTableName = (get_class($parentTable) === 'Zend_Db_Table') ? $parentTable->getDefinitionConfigName() : get_class($parentTable);" & @CRLF & _
" $map = $dependentTable->getReference($parentTableName, $ruleKey);" & @CRLF & _
"" & @CRLF & _
" if (!isset($map[\Zend\Db\Table\TableAbstract::REF_COLUMNS])) {" & @CRLF & _
" $parentInfo = $parentTable->info();" & @CRLF & _
" $map[\Zend\Db\Table\TableAbstract::REF_COLUMNS] = array_values((array) $parentInfo['primary']);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $map[\Zend\Db\Table\TableAbstract::COLUMNS] = (array) $map[\Zend\Db\Table\TableAbstract::COLUMNS];" & @CRLF & _
" $map[\Zend\Db\Table\TableAbstract::REF_COLUMNS] = (array) $map[\Zend\Db\Table\TableAbstract::REF_COLUMNS];" & @CRLF & _
"" & @CRLF & _
" return $map;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Query a dependent table to retrieve rows matching the current row." & @CRLF & _
" *" & @CRLF & _
" * @param string|\Zend\Db\Table\TableAbstract $dependentTable" & @CRLF & _
" * @param string OPTIONAL $ruleKey" & @CRLF & _
" * @param \Zend\Db\Table\Select OPTIONAL $select" & @CRLF & _
" * @return Zend_Db_Table_Rowset_Abstract Query result from $dependentTable" & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception If $dependentTable is not a table or is not loadable." & @CRLF & _
" */" & @CRLF & _
" public function findDependentRowset($dependentTable, $ruleKey = null, \Zend\Db\Table\Select $select = null)" & @CRLF & _
" {" & @CRLF & _
" $db = $this->_getTable()->getAdapter();" & @CRLF & _
"" & @CRLF & _
" if (is_string($dependentTable)) {" & @CRLF & _
" $dependentTable = $this->_getTableFromString($dependentTable);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if (!$dependentTable instanceof \Zend\Db\Table\TableAbstract) {" & @CRLF & _
" $type = gettype($dependentTable);" & @CRLF & _
" if ($type == 'object') {" & @CRLF & _
" $type = get_class($dependentTable);" & @CRLF & _
" }" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Dependent table must be a \Zend\Db\Table\TableAbstract, but it is $type");" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" // even if we are interacting between a table defined in a class and a" & @CRLF & _
" // table via extension, ensure to persist the definition" & @CRLF & _
" if (($tableDefinition = $this->_table->getDefinition()) !== null" & @CRLF & _
" && ($dependentTable->getDefinition() == null)) {" & @CRLF & _
" $dependentTable->setOptions(array(\Zend\Db\Table\TableAbstract::DEFINITION => $tableDefinition));" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if ($select === null) {" & @CRLF & _
" $select = $dependentTable->select();" & @CRLF & _
" } else {" & @CRLF & _
" $select->setTable($dependentTable);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $map = $this->_prepareReference($dependentTable, $this->_getTable(), $ruleKey);" & @CRLF & _
"" & @CRLF & _
" for ($i = 0; $i < count($map[\Zend\Db\Table\TableAbstract::COLUMNS]); ++$i) {" & @CRLF & _
" $parentColumnName = $db->foldCase($map[\Zend\Db\Table\TableAbstract::REF_COLUMNS][$i]);" & @CRLF & _
" $value = $this->_data[$parentColumnName];" & @CRLF & _
" // Use adapter from dependent table to ensure correct query construction" & @CRLF & _
" $dependentDb = $dependentTable->getAdapter();" & @CRLF & _
" $dependentColumnName = $dependentDb->foldCase($map[\Zend\Db\Table\TableAbstract::COLUMNS][$i]);" & @CRLF & _
" $dependentColumn = $dependentDb->quoteIdentifier($dependentColumnName, true);" & @CRLF & _
" $dependentInfo = $dependentTable->info();" & @CRLF & _
" $type = $dependentInfo[\Zend\Db\Table\TableAbstract::METADATA][$dependentColumnName]['DATA_TYPE'];" & @CRLF & _
" $select->where("$dependentColumn = ?", $value, $type);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" return $dependentTable->fetchAll($select);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Query a parent table to retrieve the single row matching the current row." & @CRLF & _
" *" & @CRLF & _
" * @param string|\Zend\Db\Table\TableAbstract $parentTable" & @CRLF & _
" * @param string OPTIONAL $ruleKey" & @CRLF & _
" * @param \Zend\Db\Table\Select OPTIONAL $select" & @CRLF & _
" * @return Zend_Db_Table_Row_Abstract Query result from $parentTable" & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception If $parentTable is not a table or is not loadable." & @CRLF & _
" */" & @CRLF & _
" public function findParentRow($parentTable, $ruleKey = null, \Zend\Db\Table\Select $select = null)" & @CRLF & _
" {" & @CRLF & _
" $db = $this->_getTable()->getAdapter();" & @CRLF & _
"" & @CRLF & _
" if (is_string($parentTable)) {" & @CRLF & _
" $parentTable = $this->_getTableFromString($parentTable);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if (!$parentTable instanceof \Zend\Db\Table\TableAbstract) {" & @CRLF & _
" $type = gettype($parentTable);" & @CRLF & _
" if ($type == 'object') {" & @CRLF & _
" $type = get_class($parentTable);" & @CRLF & _
" }" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Parent table must be a \Zend\Db\Table\TableAbstract, but it is $type");" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" // even if we are interacting between a table defined in a class and a" & @CRLF & _
" // table via extension, ensure to persist the definition" & @CRLF & _
" if (($tableDefinition = $this->_table->getDefinition()) !== null" & @CRLF & _
" && ($parentTable->getDefinition() == null)) {" & @CRLF & _
" $parentTable->setOptions(array(\Zend\Db\Table\TableAbstract::DEFINITION => $tableDefinition));" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if ($select === null) {" & @CRLF & _
" $select = $parentTable->select();" & @CRLF & _
" } else {" & @CRLF & _
" $select->setTable($parentTable);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $map = $this->_prepareReference($this->_getTable(), $parentTable, $ruleKey);" & @CRLF & _
"" & @CRLF & _
" // iterate the map, creating the proper wheres" & @CRLF & _
" for ($i = 0; $i < count($map[\Zend\Db\Table\TableAbstract::COLUMNS]); ++$i) {" & @CRLF & _
" $dependentColumnName = $db->foldCase($map[\Zend\Db\Table\TableAbstract::COLUMNS][$i]);" & @CRLF & _
" $value = $this->_data[$dependentColumnName];" & @CRLF & _
" // Use adapter from parent table to ensure correct query construction" & @CRLF & _
" $parentDb = $parentTable->getAdapter();" & @CRLF & _
" $parentColumnName = $parentDb->foldCase($map[\Zend\Db\Table\TableAbstract::REF_COLUMNS][$i]);" & @CRLF & _
" $parentColumn = $parentDb->quoteIdentifier($parentColumnName, true);" & @CRLF & _
" $parentInfo = $parentTable->info();" & @CRLF & _
"" & @CRLF & _
" // determine where part" & @CRLF & _
" $type = $parentInfo[\Zend\Db\Table\TableAbstract::METADATA][$parentColumnName]['DATA_TYPE'];" & @CRLF & _
" $nullable = $parentInfo[\Zend\Db\Table\TableAbstract::METADATA][$parentColumnName]['NULLABLE'];" & @CRLF & _
" if ($value === null && $nullable == true) {" & @CRLF & _
" $select->where("$parentColumn IS NULL");" & @CRLF & _
" } elseif ($value === null && $nullable == false) {" & @CRLF & _
" return null;" & @CRLF & _
" } else {" & @CRLF & _
" $select->where("$parentColumn = ?", $value, $type);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" return $parentTable->fetchRow($select);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * @param string|\Zend\Db\Table\TableAbstract $matchTable" & @CRLF & _
" * @param string|\Zend\Db\Table\TableAbstract $intersectionTable" & @CRLF & _
" * @param string OPTIONAL $callerRefRule" & @CRLF & _
" * @param string OPTIONAL $matchRefRule" & @CRLF & _
" * @param \Zend\Db\Table\Select OPTIONAL $select" & @CRLF & _
" * @return Zend_Db_Table_Rowset_Abstract Query result from $matchTable" & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception If $matchTable or $intersectionTable is not a table class or is not loadable." & @CRLF & _
" */" & @CRLF & _
" public function findManyToManyRowset($matchTable, $intersectionTable, $callerRefRule = null," & @CRLF & _
" $matchRefRule = null, \Zend\Db\Table\Select $select = null)" & @CRLF & _
" {" & @CRLF & _
" $db = $this->_getTable()->getAdapter();" & @CRLF & _
"" & @CRLF & _
" if (is_string($intersectionTable)) {" & @CRLF & _
" $intersectionTable = $this->_getTableFromString($intersectionTable);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if (!$intersectionTable instanceof \Zend\Db\Table\TableAbstract) {" & @CRLF & _
" $type = gettype($intersectionTable);" & @CRLF & _
" if ($type == 'object') {" & @CRLF & _
" $type = get_class($intersectionTable);" & @CRLF & _
" }" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Intersection table must be a \Zend\Db\Table\TableAbstract, but it is $type");" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" // even if we are interacting between a table defined in a class and a" & @CRLF & _
" // table via extension, ensure to persist the definition" & @CRLF & _
" if (($tableDefinition = $this->_table->getDefinition()) !== null" & @CRLF & _
" && ($intersectionTable->getDefinition() == null)) {" & @CRLF & _
" $intersectionTable->setOptions(array(\Zend\Db\Table\TableAbstract::DEFINITION => $tableDefinition));" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if (is_string($matchTable)) {" & @CRLF & _
" $matchTable = $this->_getTableFromString($matchTable);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if (! $matchTable instanceof \Zend\Db\Table\TableAbstract) {" & @CRLF & _
" $type = gettype($matchTable);" & @CRLF & _
" if ($type == 'object') {" & @CRLF & _
" $type = get_class($matchTable);" & @CRLF & _
" }" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Match table must be a \Zend\Db\Table\TableAbstract, but it is $type");" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" // even if we are interacting between a table defined in a class and a" & @CRLF & _
" // table via extension, ensure to persist the definition" & @CRLF & _
" if (($tableDefinition = $this->_table->getDefinition()) !== null" & @CRLF & _
" && ($matchTable->getDefinition() == null)) {" & @CRLF & _
" $matchTable->setOptions(array(\Zend\Db\Table\TableAbstract::DEFINITION => $tableDefinition));" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" if ($select === null) {" & @CRLF & _
" $select = $matchTable->select();" & @CRLF & _
" } else {" & @CRLF & _
" $select->setTable($matchTable);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" // Use adapter from intersection table to ensure correct query construction" & @CRLF & _
" $interInfo = $intersectionTable->info();" & @CRLF & _
" $interDb = $intersectionTable->getAdapter();" & @CRLF & _
" $interName = $interInfo['name'];" & @CRLF & _
" $interSchema = isset($interInfo['schema']) ? $interInfo['schema'] : null;" & @CRLF & _
" $matchInfo = $matchTable->info();" & @CRLF & _
" $matchName = $matchInfo['name'];" & @CRLF & _
" $matchSchema = isset($matchInfo['schema']) ? $matchInfo['schema'] : null;" & @CRLF & _
"" & @CRLF & _
" $matchMap = $this->_prepareReference($intersectionTable, $matchTable, $matchRefRule);" & @CRLF & _
"" & @CRLF & _
" for ($i = 0; $i < count($matchMap[\Zend\Db\Table\TableAbstract::COLUMNS]); ++$i) {" & @CRLF & _
" $interCol = $interDb->quoteIdentifier('i' . '.' . $matchMap[\Zend\Db\Table\TableAbstract::COLUMNS][$i], true);" & @CRLF & _
" $matchCol = $interDb->quoteIdentifier('m' . '.' . $matchMap[\Zend\Db\Table\TableAbstract::REF_COLUMNS][$i], true);" & @CRLF & _
" $joinCond[] = "$interCol = $matchCol";" & @CRLF & _
" }" & @CRLF & _
" $joinCond = implode(' AND ', $joinCond);" & @CRLF & _
"" & @CRLF & _
" $select->from(array('i' => $interName), array(), $interSchema)" & @CRLF & _
" ->joinInner(array('m' => $matchName), $joinCond, \Zend\Db\Select::SQL_WILDCARD, $matchSchema)" & @CRLF & _
" ->setIntegrityCheck(false);" & @CRLF & _
"" & @CRLF & _
" $callerMap = $this->_prepareReference($intersectionTable, $this->_getTable(), $callerRefRule);" & @CRLF & _
"" & @CRLF & _
" for ($i = 0; $i < count($callerMap[\Zend\Db\Table\TableAbstract::COLUMNS]); ++$i) {" & @CRLF & _
" $callerColumnName = $db->foldCase($callerMap[\Zend\Db\Table\TableAbstract::REF_COLUMNS][$i]);" & @CRLF & _
" $value = $this->_data[$callerColumnName];" & @CRLF & _
" $interColumnName = $interDb->foldCase($callerMap[\Zend\Db\Table\TableAbstract::COLUMNS][$i]);" & @CRLF & _
" $interCol = $interDb->quoteIdentifier("i.$interColumnName", true);" & @CRLF & _
" $interInfo = $intersectionTable->info();" & @CRLF & _
" $type = $interInfo[\Zend\Db\Table\TableAbstract::METADATA][$interColumnName]['DATA_TYPE'];" & @CRLF & _
" $select->where($interDb->quoteInto("$interCol = ?", $value, $type));" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" $stmt = $select->query();" & @CRLF & _
"" & @CRLF & _
" $config = array(" & @CRLF & _
" 'table' => $matchTable," & @CRLF & _
" 'data' => $stmt->fetchAll(Zend_Db::FETCH_ASSOC)," & @CRLF & _
" 'rowClass' => $matchTable->getRowClass()," & @CRLF & _
" 'readOnly' => false," & @CRLF & _
" 'stored' => true" & @CRLF & _
" );" & @CRLF & _
"" & @CRLF & _
" $rowsetClass = $matchTable->getRowsetClass();" & @CRLF & _
" if (!class_exists($rowsetClass)) {" & @CRLF & _
" try {" & @CRLF & _
" require_once 'Zend/Loader.php';" & @CRLF & _
" Zend_Loader::loadClass($rowsetClass);" & @CRLF & _
" } catch (Zend_Exception $e) {" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException($e->getMessage(), $e->getCode(), $e);" & @CRLF & _
" }" & @CRLF & _
" }" & @CRLF & _
" $rowset = new $rowsetClass($config);" & @CRLF & _
" return $rowset;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Turn magic function calls into non-magic function calls" & @CRLF & _
" * to the above methods." & @CRLF & _
" *" & @CRLF & _
" * @param string $method" & @CRLF & _
" * @param array $args OPTIONAL \Zend\Db\Table\Select query modifier" & @CRLF & _
" * @return Zend_Db_Table_Row_Abstract|Zend_Db_Table_Rowset_Abstract" & @CRLF & _
" * @throws Zend_Db_Table_Row_Exception If an invalid method is called." & @CRLF & _
" */" & @CRLF & _
" public function __call($method, array $args)" & @CRLF & _
" {" & @CRLF & _
" $matches = array();" & @CRLF & _
"" & @CRLF & _
" if (count($args) && $args[0] instanceof \Zend\Db\Table\Select) {" & @CRLF & _
" $select = $args[0];" & @CRLF & _
" } else {" & @CRLF & _
" $select = null;" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Recognize methods for Has-Many cases:" & @CRLF & _
" * findParent<Class>()" & @CRLF & _
" * findParent<Class>By<Rule>()" & @CRLF & _
" * Use the non-greedy pattern repeat modifier e.g. \w+?" & @CRLF & _
" */" & @CRLF & _
" if (preg_match('/^findParent(\w+?)(?:By(\w+))?$/', $method, $matches)) {" & @CRLF & _
" $class = $matches[1];" & @CRLF & _
" $ruleKey1 = isset($matches[2]) ? $matches[2] : null;" & @CRLF & _
" return $this->findParentRow($class, $ruleKey1, $select);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Recognize methods for Many-to-Many cases:" & @CRLF & _
" * find<Class1>Via<Class2>()" & @CRLF & _
" * find<Class1>Via<Class2>By<Rule>()" & @CRLF & _
" * find<Class1>Via<Class2>By<Rule1>And<Rule2>()" & @CRLF & _
" * Use the non-greedy pattern repeat modifier e.g. \w+?" & @CRLF & _
" */" & @CRLF & _
" if (preg_match('/^find(\w+?)Via(\w+?)(?:By(\w+?)(?:And(\w+))?)?$/', $method, $matches)) {" & @CRLF & _
" $class = $matches[1];" & @CRLF & _
" $viaClass = $matches[2];" & @CRLF & _
" $ruleKey1 = isset($matches[3]) ? $matches[3] : null;" & @CRLF & _
" $ruleKey2 = isset($matches[4]) ? $matches[4] : null;" & @CRLF & _
" return $this->findManyToManyRowset($class, $viaClass, $ruleKey1, $ruleKey2, $select);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * Recognize methods for Belongs-To cases:" & @CRLF & _
" * find<Class>()" & @CRLF & _
" * find<Class>By<Rule>()" & @CRLF & _
" * Use the non-greedy pattern repeat modifier e.g. \w+?" & @CRLF & _
" */" & @CRLF & _
" if (preg_match('/^find(\w+?)(?:By(\w+))?$/', $method, $matches)) {" & @CRLF & _
" $class = $matches[1];" & @CRLF & _
" $ruleKey1 = isset($matches[2]) ? $matches[2] : null;" & @CRLF & _
" return $this->findDependentRowset($class, $ruleKey1, $select);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
" require_once 'Zend/Db/Table/Row/Exception.php';" & @CRLF & _
" throw new \Zend\Db\Table\Row\RowException("Unrecognized method '$method()'");" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
" /**" & @CRLF & _
" * _getTableFromString" & @CRLF & _
" *" & @CRLF & _
" * @param string $tableName" & @CRLF & _
" * @return \Zend\Db\Table\TableAbstract" & @CRLF & _
" */" & @CRLF & _
" protected function _getTableFromString($tableName)" & @CRLF & _
" {" & @CRLF & _
" return \Zend\Db\Table\TableAbstract::getTableFromString($tableName, $this->_table);" & @CRLF & _
" }" & @CRLF & _
"" & @CRLF & _
"}" & @CRLF & _
""
Local $aArray = StringRegExp($sString, $sRegex, $STR_REGEXPARRAYGLOBALFULLMATCH)
Local $aFullArray[0]
For $i = 0 To UBound($aArray) -1
_ArrayConcatenate($aFullArray, $aArray[$i])
Next
$aArray = $aFullArray
; Present the entire match result
_ArrayDisplay($aArray, "Result")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm