2008年7月24日星期四

[fw-db] Re: About new step of Zend_Db_Table*

diff -ruN a/Zend/Db/Table/Abstract.php b/Zend/Db/Table/Abstract.php
--- a/Zend/Db/Table/Abstract.php 2008-03-27 07:20:53.000000000 -0300
+++ b/Zend/Db/Table/Abstract.php 2008-07-24 14:25:00.000000000 -0300
@@ -202,6 +202,18 @@
protected $_dependentTables = array();

/**
+ * Column map
+ *
+ * @var array
+ */
+ protected $_columnMap = array();
+
+ /**
+ * @var string
+ */
+ protected $_fatherRule = "";
+
+ /**
* Constructor.
*
* Supported params for $config are:
@@ -265,7 +277,7 @@
break;
}
}
-
+ $this->_initializeColumnMap();
$this->_setup();
$this->init();
}
@@ -1182,9 +1194,119 @@
*/
protected function _fetch(Zend_Db_Table_Select $select)
{
+ $select = $this->_translateColumnMap($select);
$stmt = $this->_db->query($select);
$data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
return $data;
}
+ /**
+ * @return array
+ */
+ public function getColumnMap()
+ {
+ return $this->_columnMap;
+ }

+ /**
+ * Initializa columnMap
+ *
+ * Called from {@link init()}
+ *
+ * @return void
+ */
+ protected function _initializeColumnMap()
+ {
+ foreach($this->_columnMap as $col_maped=>$col_real)
+ {
+ if (in_array($col_maped,$this->_cols))
+ {
+ throw new Zend_Db_Table_Row_Exception("Mapped column name must be not real name column");
+ }
+ if (!in_array($col_real,$this->_cols))
+ {
+ throw new Zend_Db_Table_Row_Exception("Real column name must be exist in table");
+ }
+ }
+ $_temp = array_combine($this->_cols,$this->_cols);
+ $this->_columnMap = array_flip($this->_columnMap);
+ $this->_columnMap = array_merge($_temp,$this->_columnMap);
+ $this->_columnMap = array_flip($this->_columnMap);
+ }
+ /**
+ * Return the maped column name
+ *
+ * @param string $col_real Real column name given.
+ * @return string The column name after transformation applied.
+ */
+ public function getMapedColumn($col_real)
+ {
+ $return = array_search($col_real, $this->_columnMap);
+ return ($return)?$return:$col_real;
+ }
+
+ /**
+ * Return the real column name
+ *
+ * @param string $map_col Maped column name given.
+ * @return string The column name after transformation applied.
+ */
+ public function getRealColumn($map_col)
+ {
+ $return = $this->_columnMap[$map_col];
+ return ($return)?$return:$map_col;
+ }
+
+ /**
+ * Return the select with column names translated based in Column Map
+ *
+ * @param Zend_Db_Table_Select $select Object to be translated
+ * @param Zend_Db_Table_Select Object translated
+ */
+
+ protected function _translateColumnMap(Zend_Db_Table_Select $select)
+ {
+ $where = $select->getPart('where');
+ $order = $select->getPart('order');
+
+ $select->reset('where');
+ $select->reset('order');
+
+ foreach($order as $k)
+ {
+ $col_name = $this->getRealColumn($k[0]);
+ $select->order(($k[1])?"{$col_name} {$k[1]}":$col_name);
+ }
+
+ foreach($where as &$l)
+ {
+ preg_match('/((AND|OR) \()?([a-zA-Z0-9_-]+)([=<> ])/',$l,$match);
+
+ $col_name = $this->getRealColumn($match[3]);
+ $l = str_replace("({$match[3]}{$match[4]}","({$col_name}{$match[4]}",$l);
+ if ($match[1]=='OR (')
+ {
+ $l = str_replace("OR (",'',$l);
+ $l = str_replace(")",'',$l);
+ $select->orWhere($l);
+ }
+ else if ($match[1]=='AND (')
+ {
+ $l = str_replace("AND (",'',$l);
+ $l = str_replace(")",'',$l);
+ $select->where($l);
+ }
+ else
+ {
+ $l = str_replace(")",'',$l);
+ $l = str_replace("(",'',$l);
+ $select->where($l);
+ }
+ }
+ return $select;
+ }
+
+ public function getFatherRule()
+ {
+ return $this->_fatherRule;
+ }
}
diff -ruN a/Zend/Db/Table/Row/Abstract.php b/Zend/Db/Table/Row/Abstract.php
--- a/Zend/Db/Table/Row/Abstract.php 2008-03-27 07:20:53.000000000 -0300
+++ b/Zend/Db/Table/Row/Abstract.php 2008-07-24 14:34:06.000000000 -0300
@@ -117,6 +117,22 @@
* @return void
* @throws Zend_Db_Table_Row_Exception
*/
+
+ /**
+ * Object father
+ *
+ * @var Zend_Db_Table_Row_Abstract
+ */
+ protected $_father = null;
+
+
+ /**
+ * Information provided by the _initializeColumnMap() method. This data maped the column names in table.
+ *
+ * @var array
+ */
+ protected $_columnMap = array();
+
public function __construct(array $config = array())
{
if (isset($config['table']) && $config['table'] instanceof Zend_Db_Table_Abstract) {
@@ -144,7 +160,12 @@
$info = $table->info();
$this->_primary = (array) $info['primary'];
}
-
+
+ if ($this->_table->getFatherRule())
+ {
+ $this->_father = $this->findParentRow(get_parent_class($this->_table),$this->_table->getFatherRule());
+ }
+ $this->_columnMap = $this->_table->getColumnMap();
$this->init();
}

@@ -164,8 +185,6 @@
require_once 'Zend/Db/Table/Row/Exception.php';
throw new Zend_Db_Table_Row_Exception('Specified column is not a string');
}
- // Perform no transformation by default
- return $columnName;
}

/**
@@ -179,10 +198,20 @@
{
$columnName = $this->_transformColumn($columnName);
if (!array_key_exists($columnName, $this->_data)) {
- require_once 'Zend/Db/Table/Row/Exception.php';
- throw new Zend_Db_Table_Row_Exception("Specified column \"$columnName\" is not in the row");
+ if ($this->_father)
+ {
+ return $this->_father->$columnName;
+ }
+ else
+ {
+ require_once 'Zend/Db/Table/Row/Exception.php';
+ throw new Zend_Db_Table_Row_Exception("Specified column \"$columnName\" is not in the row");
+ }
+ }
+ else
+ {
+ return $this->_data[$columnName];
}
- return $this->_data[$columnName];
}

/**
@@ -197,8 +226,15 @@
{
$columnName = $this->_transformColumn($columnName);
if (!array_key_exists($columnName, $this->_data)) {
- require_once 'Zend/Db/Table/Row/Exception.php';
- throw new Zend_Db_Table_Row_Exception("Specified column \"$columnName\" is not in the row");
+ if ($this->_father)
+ {
+ $this->_father->$columnName=$value;
+ }
+ else
+ {
+ require_once 'Zend/Db/Table/Row/Exception.php';
+ throw new Zend_Db_Table_Row_Exception("Specified column \"$columnName\" is not in the row");
+ }
}
$this->_data[$columnName] = $value;
$this->_modifiedFields[$columnName] = true;
@@ -213,7 +249,18 @@
public function __isset($columnName)
{
$columnName = $this->_transformColumn($columnName);
- return array_key_exists($columnName, $this->_data);
+ if (array_key_exists($columnName, $this->_data))
+ {
+ return true;
+ }
+ else if ($this->_father)
+ {
+ return $this->_father->__isset($columnName);
+ }
+ else
+ {
+ return false;
+ }
}

/**
@@ -223,7 +270,7 @@
*/
public function __sleep()
{
- return array('_tableClass', '_primary', '_data', '_cleanData', '_readOnly' ,'_modifiedFields');
+ return array('_tableClass', '_primary', '_data', '_cleanData', '_readOnly' ,'_modifiedFields','_father');
}

/**
@@ -374,6 +421,10 @@
} else {
return $this->_doUpdate();
}
+ if ($this->_father)
+ {
+ $this->_father->save();
+ }
}

/**
@@ -588,7 +639,14 @@
*/
public function toArray()
{
- return $this->_data;
+ $_data = array();
+
+ foreach ($this->_data as $key=>$val)
+ {
+ $_k = $this->_table->getMapedColumn($key);
+ $_data[$_k] = $this->_data[$key];
+ }
+ return $_data;
}

/**
@@ -869,27 +927,37 @@
throw new Zend_Db_Table_Row_Exception("Parent table must be a Zend_Db_Table_Abstract, but it is $type");
}

- if ($select === null) {
- $select = $parentTable->select();
- } else {
- $select->setTable($parentTable);
- }
-
- $map = $this->_prepareReference($this->_getTable(), $parentTable, $ruleKey);
-
- for ($i = 0; $i < count($map[Zend_Db_Table_Abstract::COLUMNS]); ++$i) {
- $dependentColumnName = $db->foldCase($map[Zend_Db_Table_Abstract::COLUMNS][$i]);
- $value = $this->_data[$dependentColumnName];
- // Use adapter from parent table to ensure correct query construction
- $parentDb = $parentTable->getAdapter();
- $parentColumnName = $parentDb->foldCase($map[Zend_Db_Table_Abstract::REF_COLUMNS][$i]);
- $parentColumn = $parentDb->quoteIdentifier($parentColumnName, true);
- $parentInfo = $parentTable->info();
- $type = $parentInfo[Zend_Db_Table_Abstract::METADATA][$parentColumnName]['DATA_TYPE'];
- $select->where("$parentColumn = ?", $value, $type);
+ try {
+ $map = $this->_prepareReference($this->_getTable(), $parentTable, $ruleKey);
+ if ($select === null) {
+ $select = $parentTable->select();
+ } else {
+ $select->setTable($parentTable);
+ }
+ for ($i = 0; $i < count($map[Zend_Db_Table_Abstract::COLUMNS]); ++$i) {
+ $dependentColumnName = $db->foldCase($map[Zend_Db_Table_Abstract::COLUMNS][$i]);
+ $value = $this->_data[$dependentColumnName];
+ // Use adapter from parent table to ensure correct query construction
+ $parentDb = $parentTable->getAdapter();
+ $parentColumnName = $parentDb->foldCase($map[Zend_Db_Table_Abstract::REF_COLUMNS][$i]);
+ $parentColumn = $parentDb->quoteIdentifier($parentColumnName, true);
+ $parentInfo = $parentTable->info();
+ $type = $parentInfo[Zend_Db_Table_Abstract::METADATA][$parentColumnName]['DATA_TYPE'];
+ $select->where("$parentColumn = ?", $value, $type);
+ }
+ return $parentTable->fetchRow($select);
+ }
+ catch (Zend_Db_Table_Exception $e)
+ {
+ if ($this->_father)
+ {
+ return $this->_father->findParentRow($parentTable,$ruleKey,$select);
+ }
+ else
+ {
+ throw $e;
+ }
}
-
- return $parentTable->fetchRow($select);
}

/**
diff -ruN a/Zend/Db/Table/Rowset/Abstract.php b/Zend/Db/Table/Rowset/Abstract.php
--- a/Zend/Db/Table/Rowset/Abstract.php 2008-03-27 07:20:53.000000000 -0300
+++ b/Zend/Db/Table/Rowset/Abstract.php 2008-07-24 14:26:52.000000000 -0300
@@ -366,10 +366,21 @@
{
// @todo This works only if we have iterated through
// the result set once to instantiate the rows.
+
+ $_data = array();
+
+ foreach ($this->_data as $_lk=>$line)
+ {
+ foreach($line as $key=>$val)
+ {
+ $_k = $this->_table->getMapedColumn($key);
+ $_data[$_lk][$_k] = $line[$key];
+ }
+ }
+
foreach ($this->_rows as $i => $row) {
$this->_data[$i] = $row->toArray();
}
- return $this->_data;
+ return $_data;
}
-
}
Finally the PATCH.

I do know how sent this patch to the ZF dev team. Somebody HELP-ME!!

Thanks


--
Fernando Chucre - LPIC-1
http://www.horizontesdigitais.com

没有评论: