--- library/Zend/Db/Table/Abstract.php (revision 8862) +++ library/Zend/Db/Table/Abstract.php (working copy) @@ -111,7 +111,7 @@ * * @var array */ - protected $_cols; + protected $_cols = null; /** * The primary key column or columns. @@ -148,7 +148,7 @@ * * @var array */ - protected $_metadata = array(); + protected $_metadata = null; /** * Cache for information provided by the adapter's describeTable() method. @@ -531,7 +531,6 @@ { $this->_setupDatabaseAdapter(); $this->_setupTableName(); - $this->_setupMetadata(); $this->_setupPrimaryKey(); } @@ -635,7 +634,7 @@ { if (!$this->_primary) { $this->_primary = array(); - foreach ($this->_metadata as $col) { + foreach ($this->info(self::METADATA) as $col) { if ($col['PRIMARY']) { $this->_primary[ $col['PRIMARY_POSITION'] ] = $col['COLUMN_NAME']; if ($col['IDENTITY']) { @@ -656,12 +655,13 @@ unset($this->_primary[0]); } - if (! array_intersect((array) $this->_primary, $this->_cols) == (array) $this->_primary) { + $cols = $this->info(self::COLS); + if (! array_intersect((array) $this->_primary, $cols) == (array) $this->_primary) { require_once 'Zend/Db/Table/Exception.php'; throw new Zend_Db_Table_Exception("Primary key column(s) (" . implode(',', (array) $this->_primary) . ") are not columns in this table (" - . implode(',', $this->_cols) + . implode(',', $cols) . ")"); } @@ -742,21 +742,30 @@ $info = array( self::SCHEMA => $this->_schema, self::NAME => $this->_name, - self::COLS => (array) $this->_cols, + self::COLS => null, self::PRIMARY => (array) $this->_primary, - self::METADATA => $this->_metadata, + self::METADATA => null, self::ROW_CLASS => $this->_rowClass, self::ROWSET_CLASS => $this->_rowsetClass, self::REFERENCE_MAP => $this->_referenceMap, self::DEPENDENT_TABLES => $this->_dependentTables, self::SEQUENCE => $this->_sequence ); + + // lazy loading of metadata + if($key === null || $key === self::COLS || $key === self::METADATA) { + if($this->_metadata === null || $this->_cols === null) { + $this->_setupMetadata(); + } + $info[self::COLS] = (array) $this->_cols; + $info[self::METADATA] = $this->_metadata; + } if ($key === null) { return $info; } - if (!array_key_exists($key, $info)) { + if (!array_key_exists($key, $info) || $info[$key] === null) { require_once 'Zend/Db/Table/Exception.php'; throw new Zend_Db_Table_Exception('There is no table information for the key "' . $key . '"'); } @@ -866,13 +875,14 @@ switch ($map[self::ON_UPDATE]) { case self::CASCADE: $newRefs = array(); + $metadata = $this->info(self::METADATA); for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) { $col = $this->_db->foldCase($map[self::COLUMNS][$i]); $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]); if (array_key_exists($refCol, $newPrimaryKey)) { $newRefs[$col] = $newPrimaryKey[$refCol]; } - $type = $this->_metadata[$col]['DATA_TYPE']; + $type = $metadata[$col]['DATA_TYPE']; $where[] = $this->_db->quoteInto( $this->_db->quoteIdentifier($col, true) . ' = ?', $oldPrimaryKey[$refCol], $type); @@ -914,10 +924,11 @@ if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) { switch ($map[self::ON_DELETE]) { case self::CASCADE: + $metadata = $this->info(self::METADATA); for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) { $col = $this->_db->foldCase($map[self::COLUMNS][$i]); $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]); - $type = $this->_metadata[$col]['DATA_TYPE']; + $type = $metadata[$col]['DATA_TYPE']; $where[] = $this->_db->quoteInto( $this->_db->quoteIdentifier($col, true) . ' = ?', $primaryKey[$refCol], $type); @@ -987,12 +998,13 @@ } $whereClause = null; + $metadata = $this->info(self::METADATA); if (count($whereList)) { $whereOrTerms = array(); foreach ($whereList as $keyValueSets) { $whereAndTerms = array(); foreach ($keyValueSets as $keyPosition => $keyValue) { - $type = $this->_metadata[$keyNames[$keyPosition]]['DATA_TYPE']; + $type = $metadata[$keyNames[$keyPosition]]['DATA_TYPE']; $whereAndTerms[] = $this->_db->quoteInto( $this->_db->quoteIdentifier($keyNames[$keyPosition], true) . ' = ?', $keyValue, $type); @@ -1115,7 +1127,8 @@ */ public function createRow(array $data = array()) { - $defaults = array_combine($this->_cols, array_fill(0, count($this->_cols), null)); + $cols = $this->info(self::COLS); + $defaults = array_combine($cols, array_fill(0, count($cols), null)); $data = array_intersect_key($data, $defaults); $config = array( 'table' => $this,