2
0

Abstract.php 45 KB

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