Abstract.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage Table
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Abstract.php 6320 2007-09-12 00:27:22Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Abstract
  24. */
  25. require_once 'Zend/Db/Adapter/Abstract.php';
  26. /**
  27. * @see Zend_Db_Adapter_Abstract
  28. */
  29. require_once 'Zend/Db/Select.php';
  30. /**
  31. * @see Zend_Db
  32. */
  33. require_once 'Zend/Db.php';
  34. /**
  35. * Class for SQL table interface.
  36. *
  37. * @category Zend
  38. * @package Zend_Db
  39. * @subpackage Table
  40. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. abstract class Zend_Db_Table_Abstract
  44. {
  45. const ADAPTER = 'db';
  46. const SCHEMA = 'schema';
  47. const NAME = 'name';
  48. const PRIMARY = 'primary';
  49. const COLS = 'cols';
  50. const METADATA = 'metadata';
  51. const METADATA_CACHE = 'metadataCache';
  52. const METADATA_CACHE_IN_CLASS = 'metadataCacheInClass';
  53. const ROW_CLASS = 'rowClass';
  54. const ROWSET_CLASS = 'rowsetClass';
  55. const REFERENCE_MAP = 'referenceMap';
  56. const DEPENDENT_TABLES = 'dependentTables';
  57. const SEQUENCE = 'sequence';
  58. const COLUMNS = 'columns';
  59. const REF_TABLE_CLASS = 'refTableClass';
  60. const REF_COLUMNS = 'refColumns';
  61. const ON_DELETE = 'onDelete';
  62. const ON_UPDATE = 'onUpdate';
  63. const CASCADE = 'cascade';
  64. const RESTRICT = 'restrict';
  65. const SET_NULL = 'setNull';
  66. const DEFAULT_NONE = 'defaultNone';
  67. const DEFAULT_CLASS = 'defaultClass';
  68. const DEFAULT_DB = 'defaultDb';
  69. /**
  70. * Default Zend_Db_Adapter_Abstract object.
  71. *
  72. * @var Zend_Db_Adapter_Abstract
  73. */
  74. protected static $_defaultDb;
  75. /**
  76. * Default cache for information provided by the adapter's describeTable() method.
  77. *
  78. * @var Zend_Cache_Core
  79. */
  80. protected static $_defaultMetadataCache = null;
  81. /**
  82. * Zend_Db_Adapter_Abstract object.
  83. *
  84. * @var Zend_Db_Adapter_Abstract
  85. */
  86. protected $_db;
  87. /**
  88. * The schema name (default null means current schema)
  89. *
  90. * @var array
  91. */
  92. protected $_schema = null;
  93. /**
  94. * The table name.
  95. *
  96. * @var array
  97. */
  98. protected $_name = null;
  99. /**
  100. * The table column names derived from Zend_Db_Adapter_Abstract::describeTable().
  101. *
  102. * @var array
  103. */
  104. protected $_cols;
  105. /**
  106. * The primary key column or columns.
  107. * A compound key should be declared as an array.
  108. * You may declare a single-column primary key
  109. * as a string.
  110. *
  111. * @var mixed
  112. */
  113. protected $_primary = null;
  114. /**
  115. * If your primary key is a compound key, and one of the columns uses
  116. * an auto-increment or sequence-generated value, set _identity
  117. * to the ordinal index in the $_primary array for that column.
  118. * Note this index is the position of the column in the primary key,
  119. * not the position of the column in the table. The primary key
  120. * array is 1-based.
  121. *
  122. * @var integer
  123. */
  124. protected $_identity = 1;
  125. /**
  126. * Define the logic for new values in the primary key.
  127. * May be a string, boolean true, or boolean false.
  128. *
  129. * @var mixed
  130. */
  131. protected $_sequence = true;
  132. /**
  133. * Information provided by the adapter's describeTable() method.
  134. *
  135. * @var array
  136. */
  137. protected $_metadata = array();
  138. /**
  139. * Cache for information provided by the adapter's describeTable() method.
  140. *
  141. * @var Zend_Cache_Core
  142. */
  143. protected $_metadataCache = null;
  144. /**
  145. * Flag: whether or not to cache metadata in the class
  146. * @var bool
  147. */
  148. protected $_metadataCacheInClass = true;
  149. /**
  150. * Classname for row
  151. *
  152. * @var string
  153. */
  154. protected $_rowClass = 'Zend_Db_Table_Row';
  155. /**
  156. * Classname for rowset
  157. *
  158. * @var string
  159. */
  160. protected $_rowsetClass = 'Zend_Db_Table_Rowset';
  161. /**
  162. * Associative array map of declarative referential integrity rules.
  163. * This array has one entry per foreign key in the current table.
  164. * Each key is a mnemonic name for one reference rule.
  165. *
  166. * Each value is also an associative array, with the following keys:
  167. * - columns = array of names of column(s) in the child table.
  168. * - refTableClass = class name of the parent table.
  169. * - refColumns = array of names of column(s) in the parent table,
  170. * in the same order as those in the 'columns' entry.
  171. * - onDelete = "cascade" means that a delete in the parent table also
  172. * causes a delete of referencing rows in the child table.
  173. * - onUpdate = "cascade" means that an update of primary key values in
  174. * the parent table also causes an update of referencing
  175. * rows in the child table.
  176. *
  177. * @var array
  178. */
  179. protected $_referenceMap = array();
  180. /**
  181. * Simple array of class names of tables that are "children" of the current
  182. * table, in other words tables that contain a foreign key to this one.
  183. * Array elements are not table names; they are class names of classes that
  184. * extend Zend_Db_Table_Abstract.
  185. *
  186. * @var array
  187. */
  188. protected $_dependentTables = array();
  189. protected $_defaultSource = self::DEFAULT_NONE;
  190. protected $_defaultValues = array();
  191. /**
  192. * Constructor.
  193. *
  194. * Supported params for $config are:
  195. * - db = user-supplied instance of database connector,
  196. * or key name of registry instance.
  197. * - name = table name.
  198. * - primary = string or array of primary key(s).
  199. * - rowClass = row class name.
  200. * - rowsetClass = rowset class name.
  201. * - referenceMap = array structure to declare relationship
  202. * to parent tables.
  203. * - dependentTables = array of child tables.
  204. * - metadataCache = cache for information from adapter describeTable().
  205. *
  206. * @param mixed $config Array of user-specified config options, or just the Db Adapter.
  207. * @return void
  208. */
  209. public function __construct($config = array())
  210. {
  211. /**
  212. * Allow a scalar argument to be the Adapter object or Registry key.
  213. */
  214. if (!is_array($config)) {
  215. $config = array(self::ADAPTER => $config);
  216. }
  217. foreach ($config as $key => $value) {
  218. switch ($key) {
  219. case self::ADAPTER:
  220. $this->_setAdapter($value);
  221. break;
  222. case self::SCHEMA:
  223. $this->_schema = (string) $value;
  224. break;
  225. case self::NAME:
  226. $this->_name = (string) $value;
  227. break;
  228. case self::PRIMARY:
  229. $this->_primary = (array) $value;
  230. break;
  231. case self::ROW_CLASS:
  232. $this->setRowClass($value);
  233. break;
  234. case self::ROWSET_CLASS:
  235. $this->setRowsetClass($value);
  236. break;
  237. case self::REFERENCE_MAP:
  238. $this->setReferences($value);
  239. break;
  240. case self::DEPENDENT_TABLES:
  241. $this->setDependentTables($value);
  242. break;
  243. case self::METADATA_CACHE:
  244. $this->_setMetadataCache($value);
  245. break;
  246. case self::METADATA_CACHE_IN_CLASS:
  247. $this->setMetadataCacheInClass($value);
  248. break;
  249. case self::SEQUENCE:
  250. $this->_setSequence($value);
  251. break;
  252. default:
  253. // ignore unrecognized configuration directive
  254. break;
  255. }
  256. }
  257. $this->_setup();
  258. $this->init();
  259. }
  260. /**
  261. * @param string $classname
  262. * @return Zend_Db_Table_Abstract Provides a fluent interface
  263. */
  264. public function setRowClass($classname)
  265. {
  266. $this->_rowClass = (string) $classname;
  267. return $this;
  268. }
  269. /**
  270. * @return string
  271. */
  272. public function getRowClass()
  273. {
  274. return $this->_rowClass;
  275. }
  276. /**
  277. * @param string $classname
  278. * @return Zend_Db_Table_Abstract Provides a fluent interface
  279. */
  280. public function setRowsetClass($classname)
  281. {
  282. $this->_rowsetClass = (string) $classname;
  283. return $this;
  284. }
  285. /**
  286. * @return string
  287. */
  288. public function getRowsetClass()
  289. {
  290. return $this->_rowsetClass;
  291. }
  292. /**
  293. * Add a reference to the reference map
  294. *
  295. * @param string $ruleKey
  296. * @param string|array $columns
  297. * @param string $refTableClass
  298. * @param string|array $refColumns
  299. * @param string $onDelete
  300. * @param string $onUpdate
  301. * @return Zend_Db_Table_Abstract
  302. */
  303. public function addReference($ruleKey, $columns, $refTableClass, $refColumns,
  304. $onDelete = null, $onUpdate = null)
  305. {
  306. $reference = array(self::COLUMNS => (array) $columns,
  307. self::REF_TABLE_CLASS => $refTableClass,
  308. self::REF_COLUMNS => (array) $refColumns);
  309. if (!empty($onDelete)) {
  310. $reference[self::ON_DELETE] = $onDelete;
  311. }
  312. if (!empty($onUpdate)) {
  313. $reference[self::ON_UPDATE] = $onUpdate;
  314. }
  315. $this->_referenceMap[$ruleKey] = $reference;
  316. return $this;
  317. }
  318. /**
  319. * @param array $referenceMap
  320. * @return Zend_Db_Table_Abstract Provides a fluent interface
  321. */
  322. public function setReferences(array $referenceMap)
  323. {
  324. $this->_referenceMap = $referenceMap;
  325. return $this;
  326. }
  327. /**
  328. * @param string $tableClassname
  329. * @param string $ruleKey OPTIONAL
  330. * @return array
  331. * @throws Zend_Db_Table_Exception
  332. */
  333. public function getReference($tableClassname, $ruleKey = null)
  334. {
  335. $thisClass = get_class($this);
  336. $refMap = $this->_getReferenceMapNormalized();
  337. if ($ruleKey !== null) {
  338. if (!isset($refMap[$ruleKey])) {
  339. require_once "Zend/Db/Table/Exception.php";
  340. throw new Zend_Db_Table_Exception("No reference rule \"$ruleKey\" from table $thisClass to table $tableClassname");
  341. }
  342. if ($refMap[$ruleKey][self::REF_TABLE_CLASS] != $tableClassname) {
  343. require_once "Zend/Db/Table/Exception.php";
  344. throw new Zend_Db_Table_Exception("Reference rule \"$ruleKey\" does not reference table $tableClassname");
  345. }
  346. return $refMap[$ruleKey];
  347. }
  348. foreach ($refMap as $reference) {
  349. if ($reference[self::REF_TABLE_CLASS] == $tableClassname) {
  350. return $reference;
  351. }
  352. }
  353. require_once "Zend/Db/Table/Exception.php";
  354. throw new Zend_Db_Table_Exception("No reference from table $thisClass to table $tableClassname");
  355. }
  356. /**
  357. * @param array $dependentTables
  358. * @return Zend_Db_Table_Abstract Provides a fluent interface
  359. */
  360. public function setDependentTables(array $dependentTables)
  361. {
  362. $this->_dependentTables = $dependentTables;
  363. return $this;
  364. }
  365. /**
  366. * @return array
  367. */
  368. public function getDependentTables()
  369. {
  370. return $this->_dependentTables;
  371. }
  372. /**
  373. * set the defaultSource property - this tells the table class where to find default values
  374. *
  375. * @param string $defaultSource
  376. * @return Zend_Db_Table_Abstract
  377. */
  378. public function setDefaultSource($defaultSource = self::DEFAULT_NONE)
  379. {
  380. if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
  381. $defaultSource = self::DEFAULT_NONE;
  382. }
  383. $this->_defaultSource = $defaultSource;
  384. return $this;
  385. }
  386. /**
  387. * returns the default source flag that determines where defaultSources come from
  388. *
  389. * @return unknown
  390. */
  391. public function getDefaultSource()
  392. {
  393. return $this->_defaultSource;
  394. }
  395. /**
  396. * set the default values for the table class
  397. *
  398. * @param array $defaultValues
  399. * @return Zend_Db_Table_Abstract
  400. */
  401. public function setDefaultValues(Array $defaultValues)
  402. {
  403. foreach ($defaultValues as $defaultName => $defaultValue) {
  404. if (array_key_exists($defaultName, $this->_metadata)) {
  405. $this->_defaultValues[$defaultName] = $defaultValue;
  406. }
  407. }
  408. return $this;
  409. }
  410. public function getDefaultValues()
  411. {
  412. return $this->_defaultValues;
  413. }
  414. /**
  415. * Sets the default Zend_Db_Adapter_Abstract for all Zend_Db_Table objects.
  416. *
  417. * @param mixed $db Either an Adapter object, or a string naming a Registry key
  418. * @return void
  419. */
  420. public static function setDefaultAdapter($db = null)
  421. {
  422. self::$_defaultDb = self::_setupAdapter($db);
  423. }
  424. /**
  425. * Gets the default Zend_Db_Adapter_Abstract for all Zend_Db_Table objects.
  426. *
  427. * @return Zend_Db_Adapter_Abstract or null
  428. */
  429. public static function getDefaultAdapter()
  430. {
  431. return self::$_defaultDb;
  432. }
  433. /**
  434. * @param mixed $db Either an Adapter object, or a string naming a Registry key
  435. * @return Zend_Db_Table_Abstract Provides a fluent interface
  436. */
  437. protected function _setAdapter($db)
  438. {
  439. $this->_db = self::_setupAdapter($db);
  440. return $this;
  441. }
  442. /**
  443. * Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Table object.
  444. *
  445. * @return Zend_Db_Adapter_Abstract
  446. */
  447. public function getAdapter()
  448. {
  449. return $this->_db;
  450. }
  451. /**
  452. * @param mixed $db Either an Adapter object, or a string naming a Registry key
  453. * @return Zend_Db_Adapter_Abstract
  454. * @throws Zend_Db_Table_Exception
  455. */
  456. protected static function _setupAdapter($db)
  457. {
  458. if ($db === null) {
  459. return null;
  460. }
  461. if (is_string($db)) {
  462. require_once 'Zend/Registry.php';
  463. $db = Zend_Registry::get($db);
  464. }
  465. if (!$db instanceof Zend_Db_Adapter_Abstract) {
  466. require_once 'Zend/Db/Table/Exception.php';
  467. 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');
  468. }
  469. return $db;
  470. }
  471. /**
  472. * Sets the default metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
  473. *
  474. * If $defaultMetadataCache is null, then no metadata cache is used by default.
  475. *
  476. * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
  477. * @return void
  478. */
  479. public static function setDefaultMetadataCache($metadataCache = null)
  480. {
  481. self::$_defaultMetadataCache = self::_setupMetadataCache($metadataCache);
  482. }
  483. /**
  484. * Gets the default metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
  485. *
  486. * @return Zend_Cache_Core or null
  487. */
  488. public static function getDefaultMetadataCache()
  489. {
  490. return self::$_defaultMetadataCache;
  491. }
  492. /**
  493. * Sets the metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
  494. *
  495. * If $metadataCache is null, then no metadata cache is used. Since there is no opportunity to reload metadata
  496. * after instantiation, this method need not be public, particularly because that it would have no effect
  497. * results in unnecessary API complexity. To configure the metadata cache, use the metadataCache configuration
  498. * option for the class constructor upon instantiation.
  499. *
  500. * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
  501. * @return Zend_Db_Table_Abstract Provides a fluent interface
  502. */
  503. protected function _setMetadataCache($metadataCache)
  504. {
  505. $this->_metadataCache = self::_setupMetadataCache($metadataCache);
  506. return $this;
  507. }
  508. /**
  509. * Gets the metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
  510. *
  511. * @return Zend_Cache_Core or null
  512. */
  513. public function getMetadataCache()
  514. {
  515. return $this->_metadataCache;
  516. }
  517. /**
  518. * Indicate whether metadata should be cached in the class for the duration
  519. * of the instance
  520. *
  521. * @param bool $flag
  522. * @return Zend_Db_Table_Abstract
  523. */
  524. public function setMetadataCacheInClass($flag)
  525. {
  526. $this->_metadataCacheInClass = (bool) $flag;
  527. return $this;
  528. }
  529. /**
  530. * Retrieve flag indicating if metadata should be cached for duration of
  531. * instance
  532. *
  533. * @return bool
  534. */
  535. public function metadataCacheInClass()
  536. {
  537. return $this->_metadataCacheInClass;
  538. }
  539. /**
  540. * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
  541. * @return Zend_Cache_Core
  542. * @throws Zend_Db_Table_Exception
  543. */
  544. protected static function _setupMetadataCache($metadataCache)
  545. {
  546. if ($metadataCache === null) {
  547. return null;
  548. }
  549. if (is_string($metadataCache)) {
  550. require_once 'Zend/Registry.php';
  551. $metadataCache = Zend_Registry::get($metadataCache);
  552. }
  553. if (!$metadataCache instanceof Zend_Cache_Core) {
  554. require_once 'Zend/Db/Table/Exception.php';
  555. 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');
  556. }
  557. return $metadataCache;
  558. }
  559. /**
  560. * Sets the sequence member, which defines the behavior for generating
  561. * primary key values in new rows.
  562. * - If this is a string, then the string names the sequence object.
  563. * - If this is boolean true, then the key uses an auto-incrementing
  564. * or identity mechanism.
  565. * - If this is boolean false, then the key is user-defined.
  566. * Use this for natural keys, for example.
  567. *
  568. * @param mixed $sequence
  569. * @return Zend_Db_Table_Adapter_Abstract Provides a fluent interface
  570. */
  571. protected function _setSequence($sequence)
  572. {
  573. $this->_sequence = $sequence;
  574. return $this;
  575. }
  576. /**
  577. * Turnkey for initialization of a table object.
  578. * Calls other protected methods for individual tasks, to make it easier
  579. * for a subclass to override part of the setup logic.
  580. *
  581. * @return void
  582. */
  583. protected function _setup()
  584. {
  585. $this->_setupDatabaseAdapter();
  586. $this->_setupTableName();
  587. }
  588. /**
  589. * Initialize database adapter.
  590. *
  591. * @return void
  592. */
  593. protected function _setupDatabaseAdapter()
  594. {
  595. if (! $this->_db) {
  596. $this->_db = self::getDefaultAdapter();
  597. if (!$this->_db instanceof Zend_Db_Adapter_Abstract) {
  598. require_once 'Zend/Db/Table/Exception.php';
  599. throw new Zend_Db_Table_Exception('No adapter found for ' . get_class($this));
  600. }
  601. }
  602. }
  603. /**
  604. * Initialize table and schema names.
  605. *
  606. * If the table name is not set in the class definition,
  607. * use the class name itself as the table name.
  608. *
  609. * A schema name provided with the table name (e.g., "schema.table") overrides
  610. * any existing value for $this->_schema.
  611. *
  612. * @return void
  613. */
  614. protected function _setupTableName()
  615. {
  616. if (! $this->_name) {
  617. $this->_name = get_class($this);
  618. } else if (strpos($this->_name, '.')) {
  619. list($this->_schema, $this->_name) = explode('.', $this->_name);
  620. }
  621. }
  622. /**
  623. * Initializes metadata.
  624. *
  625. * If metadata cannot be loaded from cache, adapter's describeTable() method is called to discover metadata
  626. * information. Returns true if and only if the metadata are loaded from cache.
  627. *
  628. * @return boolean
  629. * @throws Zend_Db_Table_Exception
  630. */
  631. protected function _setupMetadata()
  632. {
  633. if ($this->metadataCacheInClass() && (count($this->_metadata) > 0)) {
  634. return true;
  635. }
  636. // Assume that metadata will be loaded from cache
  637. $isMetadataFromCache = true;
  638. // If $this has no metadata cache but the class has a default metadata cache
  639. if (null === $this->_metadataCache && null !== self::$_defaultMetadataCache) {
  640. // Make $this use the default metadata cache of the class
  641. $this->_setMetadataCache(self::$_defaultMetadataCache);
  642. }
  643. // If $this has a metadata cache
  644. if (null !== $this->_metadataCache) {
  645. // Define the cache identifier where the metadata are saved
  646. $cacheId = md5("$this->_schema.$this->_name");
  647. }
  648. // If $this has no metadata cache or metadata cache misses
  649. if (null === $this->_metadataCache || !($metadata = $this->_metadataCache->load($cacheId))) {
  650. // Metadata are not loaded from cache
  651. $isMetadataFromCache = false;
  652. // Fetch metadata from the adapter's describeTable() method
  653. $metadata = $this->_db->describeTable($this->_name, $this->_schema);
  654. // If $this has a metadata cache, then cache the metadata
  655. if (null !== $this->_metadataCache && !$this->_metadataCache->save($metadata, $cacheId)) {
  656. /**
  657. * @see Zend_Db_Table_Exception
  658. */
  659. require_once 'Zend/Db/Table/Exception.php';
  660. throw new Zend_Db_Table_Exception('Failed saving metadata to metadataCache');
  661. }
  662. }
  663. // Assign the metadata to $this
  664. $this->_metadata = $metadata;
  665. // Return whether the metadata were loaded from cache
  666. return $isMetadataFromCache;
  667. }
  668. /**
  669. * Retrieve table columns
  670. *
  671. * @return array
  672. */
  673. protected function _getCols()
  674. {
  675. if (null === $this->_cols) {
  676. $this->_setupMetadata();
  677. $this->_cols = array_keys($this->_metadata);
  678. }
  679. return $this->_cols;
  680. }
  681. /**
  682. * Initialize primary key from metadata.
  683. * If $_primary is not defined, discover primary keys
  684. * from the information returned by describeTable().
  685. *
  686. * @return void
  687. * @throws Zend_Db_Table_Exception
  688. */
  689. protected function _setupPrimaryKey()
  690. {
  691. if (!$this->_primary) {
  692. $this->_setupMetadata();
  693. $this->_primary = array();
  694. foreach ($this->_metadata as $col) {
  695. if ($col['PRIMARY']) {
  696. $this->_primary[ $col['PRIMARY_POSITION'] ] = $col['COLUMN_NAME'];
  697. if ($col['IDENTITY']) {
  698. $this->_identity = $col['PRIMARY_POSITION'];
  699. }
  700. }
  701. }
  702. // if no primary key was specified and none was found in the metadata
  703. // then throw an exception.
  704. if (empty($this->_primary)) {
  705. require_once 'Zend/Db/Table/Exception.php';
  706. throw new Zend_Db_Table_Exception('A table must have a primary key, but none was found');
  707. }
  708. } else if (!is_array($this->_primary)) {
  709. $this->_primary = array(1 => $this->_primary);
  710. } else if (isset($this->_primary[0])) {
  711. array_unshift($this->_primary, null);
  712. unset($this->_primary[0]);
  713. }
  714. $cols = $this->_getCols();
  715. if (! array_intersect((array) $this->_primary, $cols) == (array) $this->_primary) {
  716. require_once 'Zend/Db/Table/Exception.php';
  717. throw new Zend_Db_Table_Exception("Primary key column(s) ("
  718. . implode(',', (array) $this->_primary)
  719. . ") are not columns in this table ("
  720. . implode(',', $cols)
  721. . ")");
  722. }
  723. $primary = (array) $this->_primary;
  724. $pkIdentity = $primary[(int) $this->_identity];
  725. /**
  726. * Special case for PostgreSQL: a SERIAL key implicitly uses a sequence
  727. * object whose name is "<table>_<column>_seq".
  728. */
  729. if ($this->_sequence === true && $this->_db instanceof Zend_Db_Adapter_Pdo_Pgsql) {
  730. $this->_sequence = "{$this->_name}_{$pkIdentity}_seq";
  731. if ($this->_schema) {
  732. $this->_sequence = $this->_schema . '.' . $this->_sequence;
  733. }
  734. }
  735. }
  736. /**
  737. * Returns a normalized version of the reference map
  738. *
  739. * @return array
  740. */
  741. protected function _getReferenceMapNormalized()
  742. {
  743. $referenceMapNormalized = array();
  744. foreach ($this->_referenceMap as $rule => $map) {
  745. $referenceMapNormalized[$rule] = array();
  746. foreach ($map as $key => $value) {
  747. switch ($key) {
  748. // normalize COLUMNS and REF_COLUMNS to arrays
  749. case self::COLUMNS:
  750. case self::REF_COLUMNS:
  751. if (!is_array($value)) {
  752. $referenceMapNormalized[$rule][$key] = array($value);
  753. } else {
  754. $referenceMapNormalized[$rule][$key] = $value;
  755. }
  756. break;
  757. // other values are copied as-is
  758. default:
  759. $referenceMapNormalized[$rule][$key] = $value;
  760. break;
  761. }
  762. }
  763. }
  764. return $referenceMapNormalized;
  765. }
  766. /**
  767. * Initialize object
  768. *
  769. * Called from {@link __construct()} as final step of object instantiation.
  770. *
  771. * @return void
  772. */
  773. public function init()
  774. {
  775. }
  776. /**
  777. * Returns table information.
  778. *
  779. * You can elect to return only a part of this information by supplying its key name,
  780. * otherwise all information is returned as an array.
  781. *
  782. * @param $key The specific info part to return OPTIONAL
  783. * @return mixed
  784. */
  785. public function info($key = null)
  786. {
  787. $this->_setupPrimaryKey();
  788. $info = array(
  789. self::SCHEMA => $this->_schema,
  790. self::NAME => $this->_name,
  791. self::COLS => $this->_getCols(),
  792. self::PRIMARY => (array) $this->_primary,
  793. self::METADATA => $this->_metadata,
  794. self::ROW_CLASS => $this->_rowClass,
  795. self::ROWSET_CLASS => $this->_rowsetClass,
  796. self::REFERENCE_MAP => $this->_referenceMap,
  797. self::DEPENDENT_TABLES => $this->_dependentTables,
  798. self::SEQUENCE => $this->_sequence
  799. );
  800. if ($key === null) {
  801. return $info;
  802. }
  803. if (!array_key_exists($key, $info)) {
  804. require_once 'Zend/Db/Table/Exception.php';
  805. throw new Zend_Db_Table_Exception('There is no table information for the key "' . $key . '"');
  806. }
  807. return $info[$key];
  808. }
  809. /**
  810. * Returns an instance of a Zend_Db_Table_Select object.
  811. *
  812. * @param bool $withFromPart Whether or not to include the from part of the select based on the table
  813. * @return Zend_Db_Table_Select
  814. */
  815. public function select($withFromPart = false)
  816. {
  817. require_once 'Zend/Db/Table/Select.php';
  818. $select = new Zend_Db_Table_Select($this);
  819. if ($withFromPart == true) {
  820. $select->from($this->info(self::NAME), Zend_Db_Table_Select::SQL_WILDCARD, $this->info(self::SCHEMA));
  821. }
  822. return $select;
  823. }
  824. /**
  825. * Inserts a new row.
  826. *
  827. * @param array $data Column-value pairs.
  828. * @return mixed The primary key of the row inserted.
  829. */
  830. public function insert(array $data)
  831. {
  832. $this->_setupPrimaryKey();
  833. /**
  834. * Zend_Db_Table assumes that if you have a compound primary key
  835. * and one of the columns in the key uses a sequence,
  836. * it's the _first_ column in the compound key.
  837. */
  838. $primary = (array) $this->_primary;
  839. $pkIdentity = $primary[(int)$this->_identity];
  840. /**
  841. * If this table uses a database sequence object and the data does not
  842. * specify a value, then get the next ID from the sequence and add it
  843. * to the row. We assume that only the first column in a compound
  844. * primary key takes a value from a sequence.
  845. */
  846. if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
  847. $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
  848. }
  849. /**
  850. * If the primary key can be generated automatically, and no value was
  851. * specified in the user-supplied data, then omit it from the tuple.
  852. */
  853. if (array_key_exists($pkIdentity, $data) && $data[$pkIdentity] === null) {
  854. unset($data[$pkIdentity]);
  855. }
  856. /**
  857. * INSERT the new row.
  858. */
  859. $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
  860. $this->_db->insert($tableSpec, $data);
  861. /**
  862. * Fetch the most recent ID generated by an auto-increment
  863. * or IDENTITY column, unless the user has specified a value,
  864. * overriding the auto-increment mechanism.
  865. */
  866. if ($this->_sequence === true && !isset($data[$pkIdentity])) {
  867. $data[$pkIdentity] = $this->_db->lastInsertId();
  868. }
  869. /**
  870. * Return the primary key value if the PK is a single column,
  871. * else return an associative array of the PK column/value pairs.
  872. */
  873. $pkData = array_intersect_key($data, array_flip($primary));
  874. if (count($primary) == 1) {
  875. reset($pkData);
  876. return current($pkData);
  877. }
  878. return $pkData;
  879. }
  880. /**
  881. * Check if the provided column is an identity of the table
  882. *
  883. * @param string $column
  884. * @throws Zend_Db_Table_Exception
  885. * @return boolean
  886. */
  887. public function isIdentity($column)
  888. {
  889. $this->_setupPrimaryKey();
  890. if (!isset($this->_metadata[$column])) {
  891. /**
  892. * @see Zend_Db_Table_Exception
  893. */
  894. require_once 'Zend/Db/Table/Exception.php';
  895. throw new Zend_Db_Table_Exception('Column "' . $column . '" not found in table.');
  896. }
  897. return (bool) $this->_metadata[$column]['IDENTITY'];
  898. }
  899. /**
  900. * Updates existing rows.
  901. *
  902. * @param array $data Column-value pairs.
  903. * @param array|string $where An SQL WHERE clause, or an array of SQL WHERE clauses.
  904. * @return int The number of rows updated.
  905. */
  906. public function update(array $data, $where)
  907. {
  908. $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
  909. return $this->_db->update($tableSpec, $data, $where);
  910. }
  911. /**
  912. * Called by a row object for the parent table's class during save() method.
  913. *
  914. * @param string $parentTableClassname
  915. * @param array $oldPrimaryKey
  916. * @param array $newPrimaryKey
  917. * @return int
  918. */
  919. public function _cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey)
  920. {
  921. $this->_setupMetadata();
  922. $rowsAffected = 0;
  923. foreach ($this->_getReferenceMapNormalized() as $map) {
  924. if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_UPDATE])) {
  925. switch ($map[self::ON_UPDATE]) {
  926. case self::CASCADE:
  927. $newRefs = array();
  928. $where = array();
  929. for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
  930. $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
  931. $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
  932. if (array_key_exists($refCol, $newPrimaryKey)) {
  933. $newRefs[$col] = $newPrimaryKey[$refCol];
  934. }
  935. $type = $this->_metadata[$col]['DATA_TYPE'];
  936. $where[] = $this->_db->quoteInto(
  937. $this->_db->quoteIdentifier($col, true) . ' = ?',
  938. $oldPrimaryKey[$refCol], $type);
  939. }
  940. $rowsAffected += $this->update($newRefs, $where);
  941. break;
  942. default:
  943. // no action
  944. break;
  945. }
  946. }
  947. }
  948. return $rowsAffected;
  949. }
  950. /**
  951. * Deletes existing rows.
  952. *
  953. * @param array|string $where SQL WHERE clause(s).
  954. * @return int The number of rows deleted.
  955. */
  956. public function delete($where)
  957. {
  958. $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
  959. return $this->_db->delete($tableSpec, $where);
  960. }
  961. /**
  962. * Called by parent table's class during delete() method.
  963. *
  964. * @param string $parentTableClassname
  965. * @param array $primaryKey
  966. * @return int Number of affected rows
  967. */
  968. public function _cascadeDelete($parentTableClassname, array $primaryKey)
  969. {
  970. $this->_setupMetadata();
  971. $rowsAffected = 0;
  972. foreach ($this->_getReferenceMapNormalized() as $map) {
  973. if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {
  974. switch ($map[self::ON_DELETE]) {
  975. case self::CASCADE:
  976. $where = array();
  977. for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
  978. $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
  979. $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
  980. $type = $this->_metadata[$col]['DATA_TYPE'];
  981. $where[] = $this->_db->quoteInto(
  982. $this->_db->quoteIdentifier($col, true) . ' = ?',
  983. $primaryKey[$refCol], $type);
  984. }
  985. $rowsAffected += $this->delete($where);
  986. break;
  987. default:
  988. // no action
  989. break;
  990. }
  991. }
  992. }
  993. return $rowsAffected;
  994. }
  995. /**
  996. * Fetches rows by primary key. The argument specifies one or more primary
  997. * key value(s). To find multiple rows by primary key, the argument must
  998. * be an array.
  999. *
  1000. * This method accepts a variable number of arguments. If the table has a
  1001. * multi-column primary key, the number of arguments must be the same as
  1002. * the number of columns in the primary key. To find multiple rows in a
  1003. * table with a multi-column primary key, each argument must be an array
  1004. * with the same number of elements.
  1005. *
  1006. * The find() method always returns a Rowset object, even if only one row
  1007. * was found.
  1008. *
  1009. * @param mixed $key The value(s) of the primary keys.
  1010. * @return Zend_Db_Table_Rowset_Abstract Row(s) matching the criteria.
  1011. * @throws Zend_Db_Table_Exception
  1012. */
  1013. public function find()
  1014. {
  1015. $this->_setupPrimaryKey();
  1016. $args = func_get_args();
  1017. $keyNames = array_values((array) $this->_primary);
  1018. if (count($args) < count($keyNames)) {
  1019. require_once 'Zend/Db/Table/Exception.php';
  1020. throw new Zend_Db_Table_Exception("Too few columns for the primary key");
  1021. }
  1022. if (count($args) > count($keyNames)) {
  1023. require_once 'Zend/Db/Table/Exception.php';
  1024. throw new Zend_Db_Table_Exception("Too many columns for the primary key");
  1025. }
  1026. $whereList = array();
  1027. $numberTerms = 0;
  1028. foreach ($args as $keyPosition => $keyValues) {
  1029. // Coerce the values to an array.
  1030. // Don't simply typecast to array, because the values
  1031. // might be Zend_Db_Expr objects.
  1032. if (!is_array($keyValues)) {
  1033. $keyValues = array($keyValues);
  1034. }
  1035. if ($numberTerms == 0) {
  1036. $numberTerms = count($keyValues);
  1037. } else if (count($keyValues) != $numberTerms) {
  1038. require_once 'Zend/Db/Table/Exception.php';
  1039. throw new Zend_Db_Table_Exception("Missing value(s) for the primary key");
  1040. }
  1041. for ($i = 0; $i < count($keyValues); ++$i) {
  1042. if (!isset($whereList[$i])) {
  1043. $whereList[$i] = array();
  1044. }
  1045. $whereList[$i][$keyPosition] = $keyValues[$i];
  1046. }
  1047. }
  1048. $whereClause = null;
  1049. if (count($whereList)) {
  1050. $whereOrTerms = array();
  1051. foreach ($whereList as $keyValueSets) {
  1052. $whereAndTerms = array();
  1053. foreach ($keyValueSets as $keyPosition => $keyValue) {
  1054. $type = $this->_metadata[$keyNames[$keyPosition]]['DATA_TYPE'];
  1055. $tableName = $this->_db->quoteTableAs($this->_name, null, true);
  1056. $columnName = $this->_db->quoteIdentifier($keyNames[$keyPosition], true);
  1057. $whereAndTerms[] = $this->_db->quoteInto(
  1058. $tableName . '.' . $columnName . ' = ?',
  1059. $keyValue, $type);
  1060. }
  1061. $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')';
  1062. }
  1063. $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')';
  1064. }
  1065. return $this->fetchAll($whereClause);
  1066. }
  1067. /**
  1068. * Fetches all rows.
  1069. *
  1070. * Honors the Zend_Db_Adapter fetch mode.
  1071. *
  1072. * @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
  1073. * @param string|array $order OPTIONAL An SQL ORDER clause.
  1074. * @param int $count OPTIONAL An SQL LIMIT count.
  1075. * @param int $offset OPTIONAL An SQL LIMIT offset.
  1076. * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
  1077. */
  1078. public function fetchAll($where = null, $order = null, $count = null, $offset = null)
  1079. {
  1080. if (!($where instanceof Zend_Db_Table_Select)) {
  1081. $select = $this->select();
  1082. if ($where !== null) {
  1083. $this->_where($select, $where);
  1084. }
  1085. if ($order !== null) {
  1086. $this->_order($select, $order);
  1087. }
  1088. if ($count !== null || $offset !== null) {
  1089. $select->limit($count, $offset);
  1090. }
  1091. } else {
  1092. $select = $where;
  1093. }
  1094. $rows = $this->_fetch($select);
  1095. $data = array(
  1096. 'table' => $this,
  1097. 'data' => $rows,
  1098. 'readOnly' => $select->isReadOnly(),
  1099. 'rowClass' => $this->_rowClass,
  1100. 'stored' => true
  1101. );
  1102. if (!class_exists($this->_rowsetClass)) {
  1103. require_once 'Zend/Loader.php';
  1104. Zend_Loader::loadClass($this->_rowsetClass);
  1105. }
  1106. return new $this->_rowsetClass($data);
  1107. }
  1108. /**
  1109. * Fetches one row in an object of type Zend_Db_Table_Row_Abstract,
  1110. * or returns null if no row matches the specified criteria.
  1111. *
  1112. * @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
  1113. * @param string|array $order OPTIONAL An SQL ORDER clause.
  1114. * @return Zend_Db_Table_Row_Abstract|null The row results per the
  1115. * Zend_Db_Adapter fetch mode, or null if no row found.
  1116. */
  1117. public function fetchRow($where = null, $order = null)
  1118. {
  1119. if (!($where instanceof Zend_Db_Table_Select)) {
  1120. $select = $this->select();
  1121. if ($where !== null) {
  1122. $this->_where($select, $where);
  1123. }
  1124. if ($order !== null) {
  1125. $this->_order($select, $order);
  1126. }
  1127. $select->limit(1);
  1128. } else {
  1129. $select = $where->limit(1);
  1130. }
  1131. $rows = $this->_fetch($select);
  1132. if (count($rows) == 0) {
  1133. return null;
  1134. }
  1135. $data = array(
  1136. 'table' => $this,
  1137. 'data' => $rows[0],
  1138. 'readOnly' => $select->isReadOnly(),
  1139. 'stored' => true
  1140. );
  1141. if (!class_exists($this->_rowClass)) {
  1142. require_once 'Zend/Loader.php';
  1143. Zend_Loader::loadClass($this->_rowClass);
  1144. }
  1145. return new $this->_rowClass($data);
  1146. }
  1147. /**
  1148. * Fetches a new blank row (not from the database).
  1149. *
  1150. * @return Zend_Db_Table_Row_Abstract
  1151. * @deprecated since 0.9.3 - use createRow() instead.
  1152. */
  1153. public function fetchNew()
  1154. {
  1155. return $this->createRow();
  1156. }
  1157. /**
  1158. * Fetches a new blank row (not from the database).
  1159. *
  1160. * @param array $data OPTIONAL data to populate in the new row.
  1161. * @param string $defaultSource OPTIONAL flag to force default values into new row
  1162. * @return Zend_Db_Table_Row_Abstract
  1163. */
  1164. public function createRow(array $data = array(), $defaultSource = null)
  1165. {
  1166. $cols = $this->_getCols();
  1167. $defaults = array_combine($cols, array_fill(0, count($cols), null));
  1168. // nothing provided at call-time, take the class value
  1169. if ($defaultSource == null) {
  1170. $defaultSource = $this->_defaultSource;
  1171. }
  1172. if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
  1173. $defaultSource = self::DEFAULT_NONE;
  1174. }
  1175. if ($defaultSource == self::DEFAULT_DB) {
  1176. foreach ($this->_metadata as $metadataName => $metadata) {
  1177. if (($metadata['DEFAULT'] != null) &&
  1178. ($metadata['NULLABLE'] !== true || ($metadata['NULLABLE'] === true && isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] === true)) &&
  1179. (!(isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] === false))) {
  1180. $defaults[$metadataName] = $metadata['DEFAULT'];
  1181. }
  1182. }
  1183. } elseif ($defaultSource == self::DEFAULT_CLASS && $this->_defaultValues) {
  1184. foreach ($this->_defaultValues as $defaultName => $defaultValue) {
  1185. if (array_key_exists($defaultName, $defaults)) {
  1186. $defaults[$defaultName] = $defaultValue;
  1187. }
  1188. }
  1189. }
  1190. $config = array(
  1191. 'table' => $this,
  1192. 'data' => $defaults,
  1193. 'readOnly' => false,
  1194. 'stored' => false
  1195. );
  1196. if (!class_exists($this->_rowClass)) {
  1197. require_once 'Zend/Loader.php';
  1198. Zend_Loader::loadClass($this->_rowClass);
  1199. }
  1200. $row = new $this->_rowClass($config);
  1201. $row->setFromArray($data);
  1202. return $row;
  1203. }
  1204. /**
  1205. * Generate WHERE clause from user-supplied string or array
  1206. *
  1207. * @param string|array $where OPTIONAL An SQL WHERE clause.
  1208. * @return Zend_Db_Table_Select
  1209. */
  1210. protected function _where(Zend_Db_Table_Select $select, $where)
  1211. {
  1212. $where = (array) $where;
  1213. foreach ($where as $key => $val) {
  1214. // is $key an int?
  1215. if (is_int($key)) {
  1216. // $val is the full condition
  1217. $select->where($val);
  1218. } else {
  1219. // $key is the condition with placeholder,
  1220. // and $val is quoted into the condition
  1221. $select->where($key, $val);
  1222. }
  1223. }
  1224. return $select;
  1225. }
  1226. /**
  1227. * Generate ORDER clause from user-supplied string or array
  1228. *
  1229. * @param string|array $order OPTIONAL An SQL ORDER clause.
  1230. * @return Zend_Db_Table_Select
  1231. */
  1232. protected function _order(Zend_Db_Table_Select $select, $order)
  1233. {
  1234. if (!is_array($order)) {
  1235. $order = array($order);
  1236. }
  1237. foreach ($order as $val) {
  1238. $select->order($val);
  1239. }
  1240. return $select;
  1241. }
  1242. /**
  1243. * Support method for fetching rows.
  1244. *
  1245. * @param Zend_Db_Table_Select $select query options.
  1246. * @return array An array containing the row results in FETCH_ASSOC mode.
  1247. */
  1248. protected function _fetch(Zend_Db_Table_Select $select)
  1249. {
  1250. $stmt = $this->_db->query($select);
  1251. $data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  1252. return $data;
  1253. }
  1254. }