26 #require_once 'Zend/Db/Adapter/Abstract.php'; 31 #require_once 'Zend/Db/Select.php'; 36 #require_once 'Zend/Db.php'; 293 $this->_schema = (string)
$value;
296 $this->_name = (string)
$value;
299 $this->_primary = (array)
$value;
339 $this->_definition = $definition;
361 $this->_definitionConfigName = $definitionConfigName;
381 $this->_rowClass = (string) $classname;
400 $this->_rowsetClass = (string) $classname;
425 $onDelete =
null, $onUpdate =
null)
427 $reference = array(self::COLUMNS => (array)
$columns,
428 self::REF_TABLE_CLASS => $refTableClass,
429 self::REF_COLUMNS => (array) $refColumns);
431 if (!empty($onDelete)) {
435 if (!empty($onUpdate)) {
439 $this->_referenceMap[$ruleKey] = $reference;
450 $this->_referenceMap = $referenceMap;
463 $thisClass = get_class($this);
464 if ($thisClass ===
'Zend_Db_Table') {
468 if ($ruleKey !==
null) {
469 if (!isset($refMap[$ruleKey])) {
470 #require_once "Zend/Db/Table/Exception.php"; 471 throw new Zend_Db_Table_Exception(
"No reference rule \"$ruleKey\" from table $thisClass to table $tableClassname");
473 if ($refMap[$ruleKey][self::REF_TABLE_CLASS] != $tableClassname) {
474 #require_once "Zend/Db/Table/Exception.php"; 477 return $refMap[$ruleKey];
479 foreach ($refMap as $reference) {
480 if ($reference[self::REF_TABLE_CLASS] == $tableClassname) {
484 #require_once "Zend/Db/Table/Exception.php"; 494 $this->_dependentTables = $dependentTables;
515 if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
519 $this->_defaultSource = $defaultSource;
541 foreach ($defaultValues as $defaultName => $defaultValue) {
542 if (array_key_exists($defaultName, $this->_metadata)) {
543 $this->_defaultValues[$defaultName] = $defaultValue;
606 if (is_string($db)) {
607 #require_once 'Zend/Registry.php'; 611 #require_once 'Zend/Db/Table/Exception.php'; 612 throw new Zend_Db_Table_Exception(
'Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored');
676 $this->_metadataCacheInClass = (bool) $flag;
698 if ($metadataCache ===
null) {
701 if (is_string($metadataCache)) {
702 #require_once 'Zend/Registry.php'; 706 #require_once 'Zend/Db/Table/Exception.php'; 707 throw new Zend_Db_Table_Exception(
'Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored');
709 return $metadataCache;
726 $this->_sequence = $sequence;
755 #require_once 'Zend/Db/Table/Exception.php'; 774 if (! $this->_name) {
775 $this->_name = get_class($this);
776 }
else if (strpos($this->_name,
'.')) {
777 list($this->_schema, $this->_name) = explode(
'.', $this->_name);
797 $isMetadataFromCache =
true;
800 if (
null === $this->_metadataCache &&
null !== self::$_defaultMetadataCache) {
806 if (
null !== $this->_metadataCache) {
810 $dbConfig = $this->_db->getConfig();
812 $port = isset($dbConfig[
'options'][
'port'])
813 ?
':'.$dbConfig[
'options'][
'port']
814 : (isset($dbConfig[
'port'])
815 ?
':'.$dbConfig[
'port']
818 $host = isset($dbConfig[
'options'][
'host'])
819 ?
':'.$dbConfig[
'options'][
'host']
820 : (isset($dbConfig[
'host'])
821 ?
':'.$dbConfig[
'host']
826 $port . $host .
'/'. $dbConfig[
'dbname'] .
':' 827 . $this->_schema.
'.' . $this->_name
832 if (
null === $this->_metadataCache || !($metadata = $this->_metadataCache->load($cacheId))) {
834 $isMetadataFromCache =
false;
836 $metadata = $this->_db->describeTable($this->_name, $this->_schema);
838 if (
null !== $this->_metadataCache && !$this->_metadataCache->save($metadata, $cacheId)) {
839 trigger_error(
'Failed saving metadata to metadataCache', E_USER_NOTICE);
844 $this->_metadata = $metadata;
847 return $isMetadataFromCache;
857 if (
null === $this->_cols) {
859 $this->_cols = array_keys($this->_metadata);
874 if (!$this->_primary) {
876 $this->_primary = array();
877 foreach ($this->_metadata as $col) {
878 if ($col[
'PRIMARY']) {
879 $this->_primary[ $col[
'PRIMARY_POSITION'] ] = $col[
'COLUMN_NAME'];
880 if ($col[
'IDENTITY']) {
881 $this->_identity = $col[
'PRIMARY_POSITION'];
887 if (empty($this->_primary)) {
888 #require_once 'Zend/Db/Table/Exception.php'; 889 throw new Zend_Db_Table_Exception(
"A table must have a primary key, but none was found for table '{$this->_name}'");
891 }
else if (!is_array($this->_primary)) {
892 $this->_primary = array(1 => $this->_primary);
893 }
else if (isset($this->_primary[0])) {
894 array_unshift($this->_primary,
null);
895 unset($this->_primary[0]);
899 if (! array_intersect((array) $this->_primary, $cols) == (array) $this->_primary) {
900 #require_once 'Zend/Db/Table/Exception.php'; 902 . implode(
',', (array) $this->_primary)
903 .
") are not columns in this table (" 904 . implode(
',', $cols)
908 $primary = (array) $this->_primary;
909 $pkIdentity = $primary[(int) $this->_identity];
916 $this->_sequence = $this->_db->quoteIdentifier(
"{$this->_name}_{$pkIdentity}_seq");
917 if ($this->_schema) {
918 $this->_sequence = $this->_db->quoteIdentifier($this->_schema) .
'.' .
$this->_sequence;
930 $referenceMapNormalized = array();
932 foreach ($this->_referenceMap as
$rule =>
$map) {
934 $referenceMapNormalized[
$rule] = array();
943 $referenceMapNormalized[
$rule][$key] = array(
$value);
957 return $referenceMapNormalized;
981 public function info($key =
null)
986 self::SCHEMA => $this->_schema,
987 self::NAME => $this->_name,
989 self::PRIMARY => (array) $this->_primary,
990 self::METADATA => $this->_metadata,
993 self::REFERENCE_MAP => $this->_referenceMap,
994 self::DEPENDENT_TABLES => $this->_dependentTables,
995 self::SEQUENCE => $this->_sequence
1002 if (!array_key_exists($key,
$info)) {
1003 #require_once 'Zend/Db/Table/Exception.php'; 1016 public function select($withFromPart = self::SELECT_WITHOUT_FROM_PART)
1018 #require_once 'Zend/Db/Table/Select.php'; 1020 if ($withFromPart == self::SELECT_WITH_FROM_PART) {
1041 $primary = (array) $this->_primary;
1042 $pkIdentity = $primary[(int)$this->_identity];
1053 if (array_key_exists($pkIdentity,
$data)) {
1054 if (
$data[$pkIdentity] ===
null 1055 ||
$data[$pkIdentity] ===
'' 1056 || is_bool(
$data[$pkIdentity])
1057 || (is_array(
$data[$pkIdentity]) && empty(
$data[$pkIdentity]))) {
1058 unset(
$data[$pkIdentity]);
1068 if (is_string($this->_sequence) && !isset(
$data[$pkIdentity])) {
1069 $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
1075 $tableSpec = ($this->_schema ? $this->_schema .
'.' :
'') . $this->_name;
1076 $this->_db->insert($tableSpec,
$data);
1083 if ($this->_sequence ===
true && !isset(
$data[$pkIdentity])) {
1084 $data[$pkIdentity] = $this->_db->lastInsertId();
1091 $pkData = array_intersect_key(
$data, array_flip($primary));
1092 if (count($primary) == 1) {
1094 return current($pkData);
1111 if (!isset($this->_metadata[$column])) {
1115 #require_once 'Zend/Db/Table/Exception.php'; 1120 return (
bool) $this->_metadata[$column][
'IDENTITY'];
1132 $tableSpec = ($this->_schema ? $this->_schema .
'.' :
'') . $this->_name;
1133 return $this->_db->update($tableSpec,
$data, $where);
1144 public function _cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey)
1149 if (
$map[self::REF_TABLE_CLASS] == $parentTableClassname && isset(
$map[self::ON_UPDATE])) {
1150 switch (
$map[self::ON_UPDATE]) {
1154 for (
$i = 0;
$i < count(
$map[self::COLUMNS]); ++
$i) {
1155 $col = $this->_db->foldCase(
$map[self::COLUMNS][
$i]);
1156 $refCol = $this->_db->foldCase(
$map[self::REF_COLUMNS][
$i]);
1157 if (array_key_exists($refCol, $newPrimaryKey)) {
1158 $newRefs[$col] = $newPrimaryKey[$refCol];
1160 $type = $this->_metadata[$col][
'DATA_TYPE'];
1161 $where[] = $this->_db->quoteInto(
1162 $this->_db->quoteIdentifier($col,
true) .
' = ?',
1163 $oldPrimaryKey[$refCol],
$type);
1165 $rowsAffected += $this->
update($newRefs, $where);
1173 return $rowsAffected;
1182 public function delete($where)
1185 if (!empty($depTables)) {
1186 $resultSet = $this->
fetchAll($where);
1187 if (count($resultSet) > 0 ) {
1188 foreach ($resultSet as
$row) {
1192 foreach ($depTables as $tableClass) {
1195 get_class($this),
$row->getPrimaryKey()
1202 $tableSpec = ($this->_schema ? $this->_schema .
'.' :
'') . $this->_name;
1203 return $this->_db->delete($tableSpec, $where);
1219 $thisClass = get_class($this);
1220 if ($thisClass ===
'Zend_Db_Table') {
1227 if (
$map[self::REF_TABLE_CLASS] == $parentTableClassname && isset(
$map[self::ON_DELETE])) {
1232 if (in_array(
$map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
1233 for (
$i = 0;
$i < count(
$map[self::COLUMNS]); ++
$i) {
1234 $col = $this->_db->foldCase(
$map[self::COLUMNS][
$i]);
1235 $refCol = $this->_db->foldCase(
$map[self::REF_COLUMNS][
$i]);
1236 $type = $this->_metadata[$col][
'DATA_TYPE'];
1237 $where[] = $this->_db->quoteInto(
1238 $this->_db->quoteIdentifier($col,
true) .
' = ?',
1239 $primaryKey[$refCol],
$type);
1244 if (
$map[self::ON_DELETE] == self::CASCADE_RECURSE) {
1250 if (!empty($depTables)) {
1251 foreach ($depTables as $tableClass) {
1253 foreach ($this->
fetchAll($where) as $depRow) {
1254 $rowsAffected += $t->_cascadeDelete($thisClass, $depRow->getPrimaryKey());
1261 if (in_array(
$map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
1262 $rowsAffected += $this->
delete($where);
1267 return $rowsAffected;
1291 $args = func_get_args();
1292 $keyNames = array_values((array) $this->_primary);
1294 if (count($args) < count($keyNames)) {
1295 #require_once 'Zend/Db/Table/Exception.php'; 1299 if (count($args) > count($keyNames)) {
1300 #require_once 'Zend/Db/Table/Exception.php'; 1304 $whereList = array();
1306 foreach ($args as $keyPosition => $keyValues) {
1310 if (is_array($keyValues) || $keyValues instanceof \Countable) {
1311 $keyValuesCount = count($keyValues);
1313 if (
null == $keyValues) {
1314 $keyValuesCount = 0;
1316 $keyValuesCount = 1;
1319 #$keyValuesCount = count($keyValues); 1327 if (!is_array($keyValues)) {
1328 $keyValues = array($keyValues);
1330 if ($numberTerms == 0) {
1331 $numberTerms = $keyValuesCount;
1332 }
else if ($keyValuesCount != $numberTerms) {
1333 #require_once 'Zend/Db/Table/Exception.php'; 1336 $keyValues = array_values($keyValues);
1337 for (
$i = 0;
$i < $keyValuesCount; ++
$i) {
1338 if (!isset($whereList[
$i])) {
1339 $whereList[
$i] = array();
1341 $whereList[
$i][$keyPosition] = $keyValues[
$i];
1345 $whereClause =
null;
1346 if (count($whereList)) {
1347 $whereOrTerms = array();
1348 $tableName = $this->_db->quoteTableAs($this->_name,
null,
true);
1349 foreach ($whereList as $keyValueSets) {
1350 $whereAndTerms = array();
1351 foreach ($keyValueSets as $keyPosition => $keyValue) {
1352 $type = $this->_metadata[$keyNames[$keyPosition]][
'DATA_TYPE'];
1353 $columnName = $this->_db->quoteIdentifier($keyNames[$keyPosition],
true);
1354 $whereAndTerms[] = $this->_db->quoteInto(
1358 $whereOrTerms[] =
'(' . implode(
' AND ', $whereAndTerms) .
')';
1360 $whereClause =
'(' . implode(
' OR ', $whereOrTerms) .
')';
1364 if ($whereClause ==
null) {
1367 #require_once 'Zend/Loader.php'; 1370 return new $rowsetClass(array(
'table' => $this,
'rowClass' => $this->
getRowClass(),
'stored' =>
true));
1373 return $this->
fetchAll($whereClause);
1392 if ($where !==
null) {
1400 if (
$count !==
null || $offset !==
null) {
1413 'readOnly' =>
$select->isReadOnly(),
1420 #require_once 'Zend/Loader.php'; 1423 return new $rowsetClass(
$data);
1441 if ($where !==
null) {
1449 $select->limit(1, ((is_numeric($offset)) ? (
int) $offset :
null));
1457 if (count($rows) == 0) {
1464 'readOnly' =>
$select->isReadOnly(),
1470 #require_once 'Zend/Loader.php'; 1473 return new $rowClass(
$data);
1497 $defaults = array_combine($cols, array_fill(0, count($cols),
null));
1500 if ($defaultSource ==
null) {
1504 if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
1508 if ($defaultSource == self::DEFAULT_DB) {
1509 foreach ($this->_metadata as $metadataName => $metadata) {
1510 if (($metadata[
'DEFAULT'] !=
null) &&
1511 ($metadata[
'NULLABLE'] !==
true || ($metadata[
'NULLABLE'] ===
true && isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] ===
true)) &&
1512 (!(isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] ===
false))) {
1513 $defaults[$metadataName] = $metadata[
'DEFAULT'];
1516 }
elseif ($defaultSource == self::DEFAULT_CLASS && $this->_defaultValues) {
1517 foreach ($this->_defaultValues as $defaultName => $defaultValue) {
1518 if (array_key_exists($defaultName, $defaults)) {
1519 $defaults[$defaultName] = $defaultValue;
1526 'data' => $defaults,
1527 'readOnly' =>
false,
1533 #require_once 'Zend/Loader.php'; 1549 $where = (array) $where;
1551 foreach ($where as $key => $val) {
1578 foreach (
$order as $val) {
1593 $stmt = $this->_db->query(
$select);
1609 $tableDefinition = $referenceTable->getDefinition();
1611 if ($tableDefinition !==
null && $tableDefinition->hasTableConfig(
$tableName)) {
1619 #require_once 'Zend/Loader.php'; 1622 #require_once 'Zend/Db/Table/Row/Exception.php'; 1630 $options[
'db'] = $referenceTable->getAdapter();
1633 if (isset($tableDefinition) && $tableDefinition !==
null) {
static _setupAdapter($db)
addReference($ruleKey, $columns, $refTableClass, $refColumns, $onDelete=null, $onUpdate=null)
getReference($tableClassname, $ruleKey=null)
setOptions(Array $options)
_order(Zend_Db_Table_Select $select, $order)
_getReferenceMapNormalized()
elseif(isset( $params[ 'redirect_parent']))
createRow(array $data=array(), $defaultSource=null)
static loadClass($class, $dirs=null)
fetchAll($where=null, $order=null, $count=null, $offset=null)
setDefinitionConfigName($definitionConfigName)
setMetadataCacheInClass($flag)
setReferences(array $referenceMap)
const METADATA_CACHE_IN_CLASS
setDependentTables(array $dependentTables)
getDefinitionConfigName()
static $_defaultMetadataCache
fetchRow($where=null, $order=null, $offset=null)
__construct($config=array())
const SELECT_WITH_FROM_PART
const DEFINITION_CONFIG_NAME
_fetch(Zend_Db_Table_Select $select)
static getDefaultMetadataCache()
setRowsetClass($classname)
_cascadeDelete($parentTableClassname, array $primaryKey)
setDefaultValues(Array $defaultValues)
static getDefaultAdapter()
setDefinition(Zend_Db_Table_Definition $definition)
update(array $data, $where)
static setDefaultAdapter($db=null)
static _setupMetadataCache($metadataCache)
foreach( $_productCollection as $_product)() ?>" class $info
_where(Zend_Db_Table_Select $select, $where)
static setDefaultMetadataCache($metadataCache=null)
setDefaultSource($defaultSource=self::DEFAULT_NONE)
const SELECT_WITHOUT_FROM_PART
_cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey)
_setMetadataCache($metadataCache)
static getTableFromString($tableName, Zend_Db_Table_Abstract $referenceTable=null)
select($withFromPart=self::SELECT_WITHOUT_FROM_PART)