Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Abstract.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Db/Adapter/Abstract.php';
27 
31 #require_once 'Zend/Db/Select.php';
32 
36 #require_once 'Zend/Db.php';
37 
47 abstract class Zend_Db_Table_Abstract
48 {
49 
50  const ADAPTER = 'db';
51  const DEFINITION = 'definition';
52  const DEFINITION_CONFIG_NAME = 'definitionConfigName';
53  const SCHEMA = 'schema';
54  const NAME = 'name';
55  const PRIMARY = 'primary';
56  const COLS = 'cols';
57  const METADATA = 'metadata';
58  const METADATA_CACHE = 'metadataCache';
59  const METADATA_CACHE_IN_CLASS = 'metadataCacheInClass';
60  const ROW_CLASS = 'rowClass';
61  const ROWSET_CLASS = 'rowsetClass';
62  const REFERENCE_MAP = 'referenceMap';
63  const DEPENDENT_TABLES = 'dependentTables';
64  const SEQUENCE = 'sequence';
65 
66  const COLUMNS = 'columns';
67  const REF_TABLE_CLASS = 'refTableClass';
68  const REF_COLUMNS = 'refColumns';
69  const ON_DELETE = 'onDelete';
70  const ON_UPDATE = 'onUpdate';
71 
72  const CASCADE = 'cascade';
73  const CASCADE_RECURSE = 'cascadeRecurse';
74  const RESTRICT = 'restrict';
75  const SET_NULL = 'setNull';
76 
77  const DEFAULT_NONE = 'defaultNone';
78  const DEFAULT_CLASS = 'defaultClass';
79  const DEFAULT_DB = 'defaultDb';
80 
81  const SELECT_WITH_FROM_PART = true;
82  const SELECT_WITHOUT_FROM_PART = false;
83 
89  protected static $_defaultDb;
90 
96  protected $_definition = null;
97 
103  protected $_definitionConfigName = null;
104 
110  protected static $_defaultMetadataCache = null;
111 
117  protected $_db;
118 
124  protected $_schema = null;
125 
131  protected $_name = null;
132 
138  protected $_cols;
139 
148  protected $_primary = null;
149 
160  protected $_identity = 1;
161 
168  protected $_sequence = true;
169 
175  protected $_metadata = array();
176 
182  protected $_metadataCache = null;
183 
188  protected $_metadataCacheInClass = true;
189 
195  protected $_rowClass = 'Zend_Db_Table_Row';
196 
202  protected $_rowsetClass = 'Zend_Db_Table_Rowset';
203 
222  protected $_referenceMap = array();
223 
232  protected $_dependentTables = array();
233 
234 
236  protected $_defaultValues = array();
237 
256  public function __construct($config = array())
257  {
261  if (!is_array($config)) {
262  $config = array(self::ADAPTER => $config);
263  }
264 
265  if ($config) {
266  $this->setOptions($config);
267  }
268 
269  $this->_setup();
270  $this->init();
271  }
272 
279  public function setOptions(Array $options)
280  {
281  foreach ($options as $key => $value) {
282  switch ($key) {
283  case self::ADAPTER:
284  $this->_setAdapter($value);
285  break;
286  case self::DEFINITION:
287  $this->setDefinition($value);
288  break;
291  break;
292  case self::SCHEMA:
293  $this->_schema = (string) $value;
294  break;
295  case self::NAME:
296  $this->_name = (string) $value;
297  break;
298  case self::PRIMARY:
299  $this->_primary = (array) $value;
300  break;
301  case self::ROW_CLASS:
302  $this->setRowClass($value);
303  break;
304  case self::ROWSET_CLASS:
305  $this->setRowsetClass($value);
306  break;
307  case self::REFERENCE_MAP:
308  $this->setReferences($value);
309  break;
311  $this->setDependentTables($value);
312  break;
314  $this->_setMetadataCache($value);
315  break;
318  break;
319  case self::SEQUENCE:
320  $this->_setSequence($value);
321  break;
322  default:
323  // ignore unrecognized configuration directive
324  break;
325  }
326  }
327 
328  return $this;
329  }
330 
337  public function setDefinition(Zend_Db_Table_Definition $definition)
338  {
339  $this->_definition = $definition;
340  return $this;
341  }
342 
348  public function getDefinition()
349  {
350  return $this->_definition;
351  }
352 
359  public function setDefinitionConfigName($definitionConfigName)
360  {
361  $this->_definitionConfigName = $definitionConfigName;
362  return $this;
363  }
364 
370  public function getDefinitionConfigName()
371  {
373  }
374 
379  public function setRowClass($classname)
380  {
381  $this->_rowClass = (string) $classname;
382 
383  return $this;
384  }
385 
389  public function getRowClass()
390  {
391  return $this->_rowClass;
392  }
393 
398  public function setRowsetClass($classname)
399  {
400  $this->_rowsetClass = (string) $classname;
401 
402  return $this;
403  }
404 
408  public function getRowsetClass()
409  {
410  return $this->_rowsetClass;
411  }
412 
424  public function addReference($ruleKey, $columns, $refTableClass, $refColumns,
425  $onDelete = null, $onUpdate = null)
426  {
427  $reference = array(self::COLUMNS => (array) $columns,
428  self::REF_TABLE_CLASS => $refTableClass,
429  self::REF_COLUMNS => (array) $refColumns);
430 
431  if (!empty($onDelete)) {
432  $reference[self::ON_DELETE] = $onDelete;
433  }
434 
435  if (!empty($onUpdate)) {
436  $reference[self::ON_UPDATE] = $onUpdate;
437  }
438 
439  $this->_referenceMap[$ruleKey] = $reference;
440 
441  return $this;
442  }
443 
448  public function setReferences(array $referenceMap)
449  {
450  $this->_referenceMap = $referenceMap;
451 
452  return $this;
453  }
454 
461  public function getReference($tableClassname, $ruleKey = null)
462  {
463  $thisClass = get_class($this);
464  if ($thisClass === 'Zend_Db_Table') {
465  $thisClass = $this->_definitionConfigName;
466  }
467  $refMap = $this->_getReferenceMapNormalized();
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");
472  }
473  if ($refMap[$ruleKey][self::REF_TABLE_CLASS] != $tableClassname) {
474  #require_once "Zend/Db/Table/Exception.php";
475  throw new Zend_Db_Table_Exception("Reference rule \"$ruleKey\" does not reference table $tableClassname");
476  }
477  return $refMap[$ruleKey];
478  }
479  foreach ($refMap as $reference) {
480  if ($reference[self::REF_TABLE_CLASS] == $tableClassname) {
481  return $reference;
482  }
483  }
484  #require_once "Zend/Db/Table/Exception.php";
485  throw new Zend_Db_Table_Exception("No reference from table $thisClass to table $tableClassname");
486  }
487 
492  public function setDependentTables(array $dependentTables)
493  {
494  $this->_dependentTables = $dependentTables;
495 
496  return $this;
497  }
498 
502  public function getDependentTables()
503  {
505  }
506 
513  public function setDefaultSource($defaultSource = self::DEFAULT_NONE)
514  {
515  if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
516  $defaultSource = self::DEFAULT_NONE;
517  }
518 
519  $this->_defaultSource = $defaultSource;
520  return $this;
521  }
522 
528  public function getDefaultSource()
529  {
530  return $this->_defaultSource;
531  }
532 
539  public function setDefaultValues(Array $defaultValues)
540  {
541  foreach ($defaultValues as $defaultName => $defaultValue) {
542  if (array_key_exists($defaultName, $this->_metadata)) {
543  $this->_defaultValues[$defaultName] = $defaultValue;
544  }
545  }
546  return $this;
547  }
548 
549  public function getDefaultValues()
550  {
551  return $this->_defaultValues;
552  }
553 
554 
561  public static function setDefaultAdapter($db = null)
562  {
563  self::$_defaultDb = self::_setupAdapter($db);
564  }
565 
571  public static function getDefaultAdapter()
572  {
573  return self::$_defaultDb;
574  }
575 
580  protected function _setAdapter($db)
581  {
582  $this->_db = self::_setupAdapter($db);
583  return $this;
584  }
585 
591  public function getAdapter()
592  {
593  return $this->_db;
594  }
595 
601  protected static function _setupAdapter($db)
602  {
603  if ($db === null) {
604  return null;
605  }
606  if (is_string($db)) {
607  #require_once 'Zend/Registry.php';
608  $db = Zend_Registry::get($db);
609  }
610  if (!$db instanceof Zend_Db_Adapter_Abstract) {
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');
613  }
614  return $db;
615  }
616 
625  public static function setDefaultMetadataCache($metadataCache = null)
626  {
627  self::$_defaultMetadataCache = self::_setupMetadataCache($metadataCache);
628  }
629 
635  public static function getDefaultMetadataCache()
636  {
638  }
639 
651  protected function _setMetadataCache($metadataCache)
652  {
653  $this->_metadataCache = self::_setupMetadataCache($metadataCache);
654  return $this;
655  }
656 
662  public function getMetadataCache()
663  {
664  return $this->_metadataCache;
665  }
666 
674  public function setMetadataCacheInClass($flag)
675  {
676  $this->_metadataCacheInClass = (bool) $flag;
677  return $this;
678  }
679 
686  public function metadataCacheInClass()
687  {
689  }
690 
696  protected static function _setupMetadataCache($metadataCache)
697  {
698  if ($metadataCache === null) {
699  return null;
700  }
701  if (is_string($metadataCache)) {
702  #require_once 'Zend/Registry.php';
703  $metadataCache = Zend_Registry::get($metadataCache);
704  }
705  if (!$metadataCache instanceof Zend_Cache_Core) {
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');
708  }
709  return $metadataCache;
710  }
711 
724  protected function _setSequence($sequence)
725  {
726  $this->_sequence = $sequence;
727 
728  return $this;
729  }
730 
738  protected function _setup()
739  {
740  $this->_setupDatabaseAdapter();
741  $this->_setupTableName();
742  }
743 
750  protected function _setupDatabaseAdapter()
751  {
752  if (! $this->_db) {
753  $this->_db = self::getDefaultAdapter();
754  if (!$this->_db instanceof Zend_Db_Adapter_Abstract) {
755  #require_once 'Zend/Db/Table/Exception.php';
756  throw new Zend_Db_Table_Exception('No adapter found for ' . get_class($this));
757  }
758  }
759  }
760 
772  protected function _setupTableName()
773  {
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);
778  }
779  }
780 
790  protected function _setupMetadata()
791  {
792  if ($this->metadataCacheInClass() && (count($this->_metadata) > 0)) {
793  return true;
794  }
795 
796  // Assume that metadata will be loaded from cache
797  $isMetadataFromCache = true;
798 
799  // If $this has no metadata cache but the class has a default metadata cache
800  if (null === $this->_metadataCache && null !== self::$_defaultMetadataCache) {
801  // Make $this use the default metadata cache of the class
802  $this->_setMetadataCache(self::$_defaultMetadataCache);
803  }
804 
805  // If $this has a metadata cache
806  if (null !== $this->_metadataCache) {
807  // Define the cache identifier where the metadata are saved
808 
809  //get db configuration
810  $dbConfig = $this->_db->getConfig();
811 
812  $port = isset($dbConfig['options']['port'])
813  ? ':'.$dbConfig['options']['port']
814  : (isset($dbConfig['port'])
815  ? ':'.$dbConfig['port']
816  : null);
817 
818  $host = isset($dbConfig['options']['host'])
819  ? ':'.$dbConfig['options']['host']
820  : (isset($dbConfig['host'])
821  ? ':'.$dbConfig['host']
822  : null);
823 
824  // Define the cache identifier where the metadata are saved
825  $cacheId = md5( // port:host/dbname:schema.table (based on availabilty)
826  $port . $host . '/'. $dbConfig['dbname'] . ':'
827  . $this->_schema. '.' . $this->_name
828  );
829  }
830 
831  // If $this has no metadata cache or metadata cache misses
832  if (null === $this->_metadataCache || !($metadata = $this->_metadataCache->load($cacheId))) {
833  // Metadata are not loaded from cache
834  $isMetadataFromCache = false;
835  // Fetch metadata from the adapter's describeTable() method
836  $metadata = $this->_db->describeTable($this->_name, $this->_schema);
837  // If $this has a metadata cache, then cache the metadata
838  if (null !== $this->_metadataCache && !$this->_metadataCache->save($metadata, $cacheId)) {
839  trigger_error('Failed saving metadata to metadataCache', E_USER_NOTICE);
840  }
841  }
842 
843  // Assign the metadata to $this
844  $this->_metadata = $metadata;
845 
846  // Return whether the metadata were loaded from cache
847  return $isMetadataFromCache;
848  }
849 
855  protected function _getCols()
856  {
857  if (null === $this->_cols) {
858  $this->_setupMetadata();
859  $this->_cols = array_keys($this->_metadata);
860  }
861  return $this->_cols;
862  }
863 
872  protected function _setupPrimaryKey()
873  {
874  if (!$this->_primary) {
875  $this->_setupMetadata();
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'];
882  }
883  }
884  }
885  // if no primary key was specified and none was found in the metadata
886  // then throw an exception.
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}'");
890  }
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]);
896  }
897 
898  $cols = $this->_getCols();
899  if (! array_intersect((array) $this->_primary, $cols) == (array) $this->_primary) {
900  #require_once 'Zend/Db/Table/Exception.php';
901  throw new Zend_Db_Table_Exception("Primary key column(s) ("
902  . implode(',', (array) $this->_primary)
903  . ") are not columns in this table ("
904  . implode(',', $cols)
905  . ")");
906  }
907 
908  $primary = (array) $this->_primary;
909  $pkIdentity = $primary[(int) $this->_identity];
910 
915  if ($this->_sequence === true && $this->_db instanceof Zend_Db_Adapter_Pdo_Pgsql) {
916  $this->_sequence = $this->_db->quoteIdentifier("{$this->_name}_{$pkIdentity}_seq");
917  if ($this->_schema) {
918  $this->_sequence = $this->_db->quoteIdentifier($this->_schema) . '.' . $this->_sequence;
919  }
920  }
921  }
922 
928  protected function _getReferenceMapNormalized()
929  {
930  $referenceMapNormalized = array();
931 
932  foreach ($this->_referenceMap as $rule => $map) {
933 
934  $referenceMapNormalized[$rule] = array();
935 
936  foreach ($map as $key => $value) {
937  switch ($key) {
938 
939  // normalize COLUMNS and REF_COLUMNS to arrays
940  case self::COLUMNS:
941  case self::REF_COLUMNS:
942  if (!is_array($value)) {
943  $referenceMapNormalized[$rule][$key] = array($value);
944  } else {
945  $referenceMapNormalized[$rule][$key] = $value;
946  }
947  break;
948 
949  // other values are copied as-is
950  default:
951  $referenceMapNormalized[$rule][$key] = $value;
952  break;
953  }
954  }
955  }
956 
957  return $referenceMapNormalized;
958  }
959 
967  public function init()
968  {
969  }
970 
981  public function info($key = null)
982  {
983  $this->_setupPrimaryKey();
984 
985  $info = array(
986  self::SCHEMA => $this->_schema,
987  self::NAME => $this->_name,
988  self::COLS => $this->_getCols(),
989  self::PRIMARY => (array) $this->_primary,
990  self::METADATA => $this->_metadata,
991  self::ROW_CLASS => $this->getRowClass(),
992  self::ROWSET_CLASS => $this->getRowsetClass(),
993  self::REFERENCE_MAP => $this->_referenceMap,
994  self::DEPENDENT_TABLES => $this->_dependentTables,
995  self::SEQUENCE => $this->_sequence
996  );
997 
998  if ($key === null) {
999  return $info;
1000  }
1001 
1002  if (!array_key_exists($key, $info)) {
1003  #require_once 'Zend/Db/Table/Exception.php';
1004  throw new Zend_Db_Table_Exception('There is no table information for the key "' . $key . '"');
1005  }
1006 
1007  return $info[$key];
1008  }
1009 
1016  public function select($withFromPart = self::SELECT_WITHOUT_FROM_PART)
1017  {
1018  #require_once 'Zend/Db/Table/Select.php';
1019  $select = new Zend_Db_Table_Select($this);
1020  if ($withFromPart == self::SELECT_WITH_FROM_PART) {
1021  $select->from($this->info(self::NAME), Zend_Db_Table_Select::SQL_WILDCARD, $this->info(self::SCHEMA));
1022  }
1023  return $select;
1024  }
1025 
1032  public function insert(array $data)
1033  {
1034  $this->_setupPrimaryKey();
1035 
1041  $primary = (array) $this->_primary;
1042  $pkIdentity = $primary[(int)$this->_identity];
1043 
1044 
1053  if (array_key_exists($pkIdentity, $data)) {
1054  if ($data[$pkIdentity] === null // null
1055  || $data[$pkIdentity] === '' // empty string
1056  || is_bool($data[$pkIdentity]) // boolean
1057  || (is_array($data[$pkIdentity]) && empty($data[$pkIdentity]))) { // empty array
1058  unset($data[$pkIdentity]);
1059  }
1060  }
1061 
1068  if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
1069  $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
1070  }
1071 
1075  $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1076  $this->_db->insert($tableSpec, $data);
1077 
1083  if ($this->_sequence === true && !isset($data[$pkIdentity])) {
1084  $data[$pkIdentity] = $this->_db->lastInsertId();
1085  }
1086 
1091  $pkData = array_intersect_key($data, array_flip($primary));
1092  if (count($primary) == 1) {
1093  reset($pkData);
1094  return current($pkData);
1095  }
1096 
1097  return $pkData;
1098  }
1099 
1107  public function isIdentity($column)
1108  {
1109  $this->_setupPrimaryKey();
1110 
1111  if (!isset($this->_metadata[$column])) {
1115  #require_once 'Zend/Db/Table/Exception.php';
1116 
1117  throw new Zend_Db_Table_Exception('Column "' . $column . '" not found in table.');
1118  }
1119 
1120  return (bool) $this->_metadata[$column]['IDENTITY'];
1121  }
1122 
1130  public function update(array $data, $where)
1131  {
1132  $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1133  return $this->_db->update($tableSpec, $data, $where);
1134  }
1135 
1144  public function _cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey)
1145  {
1146  $this->_setupMetadata();
1147  $rowsAffected = 0;
1148  foreach ($this->_getReferenceMapNormalized() as $map) {
1149  if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_UPDATE])) {
1150  switch ($map[self::ON_UPDATE]) {
1151  case self::CASCADE:
1152  $newRefs = array();
1153  $where = array();
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];
1159  }
1160  $type = $this->_metadata[$col]['DATA_TYPE'];
1161  $where[] = $this->_db->quoteInto(
1162  $this->_db->quoteIdentifier($col, true) . ' = ?',
1163  $oldPrimaryKey[$refCol], $type);
1164  }
1165  $rowsAffected += $this->update($newRefs, $where);
1166  break;
1167  default:
1168  // no action
1169  break;
1170  }
1171  }
1172  }
1173  return $rowsAffected;
1174  }
1175 
1182  public function delete($where)
1183  {
1184  $depTables = $this->getDependentTables();
1185  if (!empty($depTables)) {
1186  $resultSet = $this->fetchAll($where);
1187  if (count($resultSet) > 0 ) {
1188  foreach ($resultSet as $row) {
1192  foreach ($depTables as $tableClass) {
1193  $t = self::getTableFromString($tableClass, $this);
1194  $t->_cascadeDelete(
1195  get_class($this), $row->getPrimaryKey()
1196  );
1197  }
1198  }
1199  }
1200  }
1201 
1202  $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1203  return $this->_db->delete($tableSpec, $where);
1204  }
1205 
1213  public function _cascadeDelete($parentTableClassname, array $primaryKey)
1214  {
1215  // setup metadata
1216  $this->_setupMetadata();
1217 
1218  // get this class name
1219  $thisClass = get_class($this);
1220  if ($thisClass === 'Zend_Db_Table') {
1221  $thisClass = $this->_definitionConfigName;
1222  }
1223 
1224  $rowsAffected = 0;
1225 
1226  foreach ($this->_getReferenceMapNormalized() as $map) {
1227  if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {
1228 
1229  $where = array();
1230 
1231  // CASCADE or CASCADE_RECURSE
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);
1240  }
1241  }
1242 
1243  // CASCADE_RECURSE
1244  if ($map[self::ON_DELETE] == self::CASCADE_RECURSE) {
1245 
1249  $depTables = $this->getDependentTables();
1250  if (!empty($depTables)) {
1251  foreach ($depTables as $tableClass) {
1252  $t = self::getTableFromString($tableClass, $this);
1253  foreach ($this->fetchAll($where) as $depRow) {
1254  $rowsAffected += $t->_cascadeDelete($thisClass, $depRow->getPrimaryKey());
1255  }
1256  }
1257  }
1258  }
1259 
1260  // CASCADE or CASCADE_RECURSE
1261  if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
1262  $rowsAffected += $this->delete($where);
1263  }
1264 
1265  }
1266  }
1267  return $rowsAffected;
1268  }
1269 
1288  public function find()
1289  {
1290  $this->_setupPrimaryKey();
1291  $args = func_get_args();
1292  $keyNames = array_values((array) $this->_primary);
1293 
1294  if (count($args) < count($keyNames)) {
1295  #require_once 'Zend/Db/Table/Exception.php';
1296  throw new Zend_Db_Table_Exception("Too few columns for the primary key");
1297  }
1298 
1299  if (count($args) > count($keyNames)) {
1300  #require_once 'Zend/Db/Table/Exception.php';
1301  throw new Zend_Db_Table_Exception("Too many columns for the primary key");
1302  }
1303 
1304  $whereList = array();
1305  $numberTerms = 0;
1306  foreach ($args as $keyPosition => $keyValues) {
1310  if (is_array($keyValues) || $keyValues instanceof \Countable) {
1311  $keyValuesCount = count($keyValues);
1312  } else {
1313  if (null == $keyValues) {
1314  $keyValuesCount = 0;
1315  } else {
1316  $keyValuesCount = 1;
1317  }
1318  }
1319  #$keyValuesCount = count($keyValues);
1320 
1324  // Coerce the values to an array.
1325  // Don't simply typecast to array, because the values
1326  // might be Zend_Db_Expr objects.
1327  if (!is_array($keyValues)) {
1328  $keyValues = array($keyValues);
1329  }
1330  if ($numberTerms == 0) {
1331  $numberTerms = $keyValuesCount;
1332  } else if ($keyValuesCount != $numberTerms) {
1333  #require_once 'Zend/Db/Table/Exception.php';
1334  throw new Zend_Db_Table_Exception("Missing value(s) for the primary key");
1335  }
1336  $keyValues = array_values($keyValues);
1337  for ($i = 0; $i < $keyValuesCount; ++$i) {
1338  if (!isset($whereList[$i])) {
1339  $whereList[$i] = array();
1340  }
1341  $whereList[$i][$keyPosition] = $keyValues[$i];
1342  }
1343  }
1344 
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(
1355  $tableName . '.' . $columnName . ' = ?',
1356  $keyValue, $type);
1357  }
1358  $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')';
1359  }
1360  $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')';
1361  }
1362 
1363  // issue ZF-5775 (empty where clause should return empty rowset)
1364  if ($whereClause == null) {
1365  $rowsetClass = $this->getRowsetClass();
1366  if (!class_exists($rowsetClass)) {
1367  #require_once 'Zend/Loader.php';
1368  Zend_Loader::loadClass($rowsetClass);
1369  }
1370  return new $rowsetClass(array('table' => $this, 'rowClass' => $this->getRowClass(), 'stored' => true));
1371  }
1372 
1373  return $this->fetchAll($whereClause);
1374  }
1375 
1387  public function fetchAll($where = null, $order = null, $count = null, $offset = null)
1388  {
1389  if (!($where instanceof Zend_Db_Table_Select)) {
1390  $select = $this->select();
1391 
1392  if ($where !== null) {
1393  $this->_where($select, $where);
1394  }
1395 
1396  if ($order !== null) {
1397  $this->_order($select, $order);
1398  }
1399 
1400  if ($count !== null || $offset !== null) {
1401  $select->limit($count, $offset);
1402  }
1403 
1404  } else {
1405  $select = $where;
1406  }
1407 
1408  $rows = $this->_fetch($select);
1409 
1410  $data = array(
1411  'table' => $this,
1412  'data' => $rows,
1413  'readOnly' => $select->isReadOnly(),
1414  'rowClass' => $this->getRowClass(),
1415  'stored' => true
1416  );
1417 
1418  $rowsetClass = $this->getRowsetClass();
1419  if (!class_exists($rowsetClass)) {
1420  #require_once 'Zend/Loader.php';
1421  Zend_Loader::loadClass($rowsetClass);
1422  }
1423  return new $rowsetClass($data);
1424  }
1425 
1436  public function fetchRow($where = null, $order = null, $offset = null)
1437  {
1438  if (!($where instanceof Zend_Db_Table_Select)) {
1439  $select = $this->select();
1440 
1441  if ($where !== null) {
1442  $this->_where($select, $where);
1443  }
1444 
1445  if ($order !== null) {
1446  $this->_order($select, $order);
1447  }
1448 
1449  $select->limit(1, ((is_numeric($offset)) ? (int) $offset : null));
1450 
1451  } else {
1452  $select = $where->limit(1, $where->getPart(Zend_Db_Select::LIMIT_OFFSET));
1453  }
1454 
1455  $rows = $this->_fetch($select);
1456 
1457  if (count($rows) == 0) {
1458  return null;
1459  }
1460 
1461  $data = array(
1462  'table' => $this,
1463  'data' => $rows[0],
1464  'readOnly' => $select->isReadOnly(),
1465  'stored' => true
1466  );
1467 
1468  $rowClass = $this->getRowClass();
1469  if (!class_exists($rowClass)) {
1470  #require_once 'Zend/Loader.php';
1471  Zend_Loader::loadClass($rowClass);
1472  }
1473  return new $rowClass($data);
1474  }
1475 
1482  public function fetchNew()
1483  {
1484  return $this->createRow();
1485  }
1486 
1494  public function createRow(array $data = array(), $defaultSource = null)
1495  {
1496  $cols = $this->_getCols();
1497  $defaults = array_combine($cols, array_fill(0, count($cols), null));
1498 
1499  // nothing provided at call-time, take the class value
1500  if ($defaultSource == null) {
1501  $defaultSource = $this->_defaultSource;
1502  }
1503 
1504  if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
1505  $defaultSource = self::DEFAULT_NONE;
1506  }
1507 
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'];
1514  }
1515  }
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;
1520  }
1521  }
1522  }
1523 
1524  $config = array(
1525  'table' => $this,
1526  'data' => $defaults,
1527  'readOnly' => false,
1528  'stored' => false
1529  );
1530 
1531  $rowClass = $this->getRowClass();
1532  if (!class_exists($rowClass)) {
1533  #require_once 'Zend/Loader.php';
1534  Zend_Loader::loadClass($rowClass);
1535  }
1536  $row = new $rowClass($config);
1537  $row->setFromArray($data);
1538  return $row;
1539  }
1540 
1547  protected function _where(Zend_Db_Table_Select $select, $where)
1548  {
1549  $where = (array) $where;
1550 
1551  foreach ($where as $key => $val) {
1552  // is $key an int?
1553  if (is_int($key)) {
1554  // $val is the full condition
1555  $select->where($val);
1556  } else {
1557  // $key is the condition with placeholder,
1558  // and $val is quoted into the condition
1559  $select->where($key, $val);
1560  }
1561  }
1562 
1563  return $select;
1564  }
1565 
1573  {
1574  if (!is_array($order)) {
1575  $order = array($order);
1576  }
1577 
1578  foreach ($order as $val) {
1579  $select->order($val);
1580  }
1581 
1582  return $select;
1583  }
1584 
1592  {
1593  $stmt = $this->_db->query($select);
1594  $data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
1595  return $data;
1596  }
1597 
1606  public static function getTableFromString($tableName, Zend_Db_Table_Abstract $referenceTable = null)
1607  {
1608  if ($referenceTable instanceof Zend_Db_Table_Abstract) {
1609  $tableDefinition = $referenceTable->getDefinition();
1610 
1611  if ($tableDefinition !== null && $tableDefinition->hasTableConfig($tableName)) {
1612  return new Zend_Db_Table($tableName, $tableDefinition);
1613  }
1614  }
1615 
1616  // assume the tableName is the class name
1617  if (!class_exists($tableName)) {
1618  try {
1619  #require_once 'Zend/Loader.php';
1621  } catch (Zend_Exception $e) {
1622  #require_once 'Zend/Db/Table/Row/Exception.php';
1623  throw new Zend_Db_Table_Row_Exception($e->getMessage(), $e->getCode(), $e);
1624  }
1625  }
1626 
1627  $options = array();
1628 
1629  if ($referenceTable instanceof Zend_Db_Table_Abstract) {
1630  $options['db'] = $referenceTable->getAdapter();
1631  }
1632 
1633  if (isset($tableDefinition) && $tableDefinition !== null) {
1634  $options[Zend_Db_Table_Abstract::DEFINITION] = $tableDefinition;
1635  }
1636 
1637  return new $tableName($options);
1638  }
1639 
1640 }
static _setupAdapter($db)
Definition: Abstract.php:601
addReference($ruleKey, $columns, $refTableClass, $refColumns, $onDelete=null, $onUpdate=null)
Definition: Abstract.php:424
getReference($tableClassname, $ruleKey=null)
Definition: Abstract.php:461
setOptions(Array $options)
Definition: Abstract.php:279
_order(Zend_Db_Table_Select $select, $order)
Definition: Abstract.php:1572
$tableName
Definition: trigger.php:13
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
createRow(array $data=array(), $defaultSource=null)
Definition: Abstract.php:1494
static loadClass($class, $dirs=null)
Definition: Loader.php:52
$config
Definition: fraud_order.php:17
fetchAll($where=null, $order=null, $count=null, $offset=null)
Definition: Abstract.php:1387
$count
Definition: recent.phtml:13
setDefinitionConfigName($definitionConfigName)
Definition: Abstract.php:359
setMetadataCacheInClass($flag)
Definition: Abstract.php:674
setReferences(array $referenceMap)
Definition: Abstract.php:448
const FETCH_ASSOC
Definition: Db.php:142
$order
Definition: order.php:55
const METADATA_CACHE_IN_CLASS
Definition: Abstract.php:59
setDependentTables(array $dependentTables)
Definition: Abstract.php:492
static $_defaultMetadataCache
Definition: Abstract.php:110
fetchRow($where=null, $order=null, $offset=null)
Definition: Abstract.php:1436
__construct($config=array())
Definition: Abstract.php:256
$columns
Definition: default.phtml:15
const SQL_WILDCARD
Definition: Select.php:66
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
const DEFINITION_CONFIG_NAME
Definition: Abstract.php:52
const LIMIT_OFFSET
Definition: Select.php:56
_fetch(Zend_Db_Table_Select $select)
Definition: Abstract.php:1591
static getDefaultMetadataCache()
Definition: Abstract.php:635
setRowsetClass($classname)
Definition: Abstract.php:398
_cascadeDelete($parentTableClassname, array $primaryKey)
Definition: Abstract.php:1213
_setSequence($sequence)
Definition: Abstract.php:724
setDefaultValues(Array $defaultValues)
Definition: Abstract.php:539
static getDefaultAdapter()
Definition: Abstract.php:571
setDefinition(Zend_Db_Table_Definition $definition)
Definition: Abstract.php:337
update(array $data, $where)
Definition: Abstract.php:1130
static setDefaultAdapter($db=null)
Definition: Abstract.php:561
static _setupMetadataCache($metadataCache)
Definition: Abstract.php:696
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
_where(Zend_Db_Table_Select $select, $where)
Definition: Abstract.php:1547
static setDefaultMetadataCache($metadataCache=null)
Definition: Abstract.php:625
setDefaultSource($defaultSource=self::DEFAULT_NONE)
Definition: Abstract.php:513
const SELECT_WITHOUT_FROM_PART
Definition: Abstract.php:82
_cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey)
Definition: Abstract.php:1144
$i
Definition: gallery.phtml:31
_setMetadataCache($metadataCache)
Definition: Abstract.php:651
static getTableFromString($tableName, Zend_Db_Table_Abstract $referenceTable=null)
Definition: Abstract.php:1606
insert(array $data)
Definition: Abstract.php:1032
setRowClass($classname)
Definition: Abstract.php:379
select($withFromPart=self::SELECT_WITHOUT_FROM_PART)
Definition: Abstract.php:1016
static get($index)
Definition: Registry.php:141