Abstract.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. * @return Zend_Db_Table_Select
  813. */
  814. public function select()
  815. {
  816. require_once 'Zend/Db/Table/Select.php';
  817. return new Zend_Db_Table_Select($this);
  818. }
  819. /**
  820. * Inserts a new row.
  821. *
  822. * @param array $data Column-value pairs.
  823. * @return mixed The primary key of the row inserted.
  824. */
  825. public function insert(array $data)
  826. {
  827. $this->_setupPrimaryKey();
  828. /**
  829. * Zend_Db_Table assumes that if you have a compound primary key
  830. * and one of the columns in the key uses a sequence,
  831. * it's the _first_ column in the compound key.
  832. */
  833. $primary = (array) $this->_primary;
  834. $pkIdentity = $primary[(int)$this->_identity];
  835. /**
  836. * If this table uses a database sequence object and the data does not
  837. * specify a value, then get the next ID from the sequence and add it
  838. * to the row. We assume that only the first column in a compound
  839. * primary key takes a value from a sequence.
  840. */
  841. if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
  842. $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
  843. }
  844. /**
  845. * If the primary key can be generated automatically, and no value was
  846. * specified in the user-supplied data, then omit it from the tuple.
  847. */
  848. if (array_key_exists($pkIdentity, $data) && $data[$pkIdentity] === null) {
  849. unset($data[$pkIdentity]);
  850. }
  851. /**
  852. * INSERT the new row.
  853. */
  854. $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
  855. $this->_db->insert($tableSpec, $data);
  856. /**
  857. * Fetch the most recent ID generated by an auto-increment
  858. * or IDENTITY column, unless the user has specified a value,
  859. * overriding the auto-increment mechanism.
  860. */
  861. if ($this->_sequence === true && !isset($data[$pkIdentity])) {
  862. $data[$pkIdentity] = $this->_db->lastInsertId();
  863. }
  864. /**
  865. * Return the primary key value if the PK is a single column,
  866. * else return an associative array of the PK column/value pairs.
  867. */
  868. $pkData = array_intersect_key($data, array_flip($primary));
  869. if (count($primary) == 1) {
  870. reset($pkData);
  871. return current($pkData);
  872. }
  873. return $pkData;
  874. }
  875. /**
  876. * Check if the provided column is an identity of the table
  877. *
  878. * @param string $column
  879. * @throws Zend_Db_Table_Exception
  880. * @return boolean
  881. */
  882. public function isIdentity($column)
  883. {
  884. $this->_setupPrimaryKey();
  885. if (!isset($this->_metadata[$column])) {
  886. /**
  887. * @see Zend_Db_Table_Exception
  888. */
  889. require_once 'Zend/Db/Table/Exception.php';
  890. throw new Zend_Db_Table_Exception('Column "' . $column . '" not found in table.');
  891. }
  892. return (bool) $this->_metadata[$column]['IDENTITY'];
  893. }
  894. /**
  895. * Updates existing rows.
  896. *
  897. * @param array $data Column-value pairs.
  898. * @param array|string $where An SQL WHERE clause, or an array of SQL WHERE clauses.
  899. * @return int The number of rows updated.
  900. */
  901. public function update(array $data, $where)
  902. {
  903. $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
  904. return $this->_db->update($tableSpec, $data, $where);
  905. }
  906. /**
  907. * Called by a row object for the parent table's class during save() method.
  908. *
  909. * @param string $parentTableClassname
  910. * @param array $oldPrimaryKey
  911. * @param array $newPrimaryKey
  912. * @return int
  913. */
  914. public function _cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey)
  915. {
  916. $this->_setupMetadata();
  917. $rowsAffected = 0;
  918. foreach ($this->_getReferenceMapNormalized() as $map) {
  919. if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_UPDATE])) {
  920. switch ($map[self::ON_UPDATE]) {
  921. case self::CASCADE:
  922. $newRefs = array();
  923. $where = array();
  924. for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
  925. $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
  926. $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
  927. if (array_key_exists($refCol, $newPrimaryKey)) {
  928. $newRefs[$col] = $newPrimaryKey[$refCol];
  929. }
  930. $type = $this->_metadata[$col]['DATA_TYPE'];
  931. $where[] = $this->_db->quoteInto(
  932. $this->_db->quoteIdentifier($col, true) . ' = ?',
  933. $oldPrimaryKey[$refCol], $type);
  934. }
  935. $rowsAffected += $this->update($newRefs, $where);
  936. break;
  937. default:
  938. // no action
  939. break;
  940. }
  941. }
  942. }
  943. return $rowsAffected;
  944. }
  945. /**
  946. * Deletes existing rows.
  947. *
  948. * @param array|string $where SQL WHERE clause(s).
  949. * @return int The number of rows deleted.
  950. */
  951. public function delete($where)
  952. {
  953. $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
  954. return $this->_db->delete($tableSpec, $where);
  955. }
  956. /**
  957. * Called by parent table's class during delete() method.
  958. *
  959. * @param string $parentTableClassname
  960. * @param array $primaryKey
  961. * @return int Number of affected rows
  962. */
  963. public function _cascadeDelete($parentTableClassname, array $primaryKey)
  964. {
  965. $this->_setupMetadata();
  966. $rowsAffected = 0;
  967. foreach ($this->_getReferenceMapNormalized() as $map) {
  968. if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {
  969. switch ($map[self::ON_DELETE]) {
  970. case self::CASCADE:
  971. $where = array();
  972. for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
  973. $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
  974. $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
  975. $type = $this->_metadata[$col]['DATA_TYPE'];
  976. $where[] = $this->_db->quoteInto(
  977. $this->_db->quoteIdentifier($col, true) . ' = ?',
  978. $primaryKey[$refCol], $type);
  979. }
  980. $rowsAffected += $this->delete($where);
  981. break;
  982. default:
  983. // no action
  984. break;
  985. }
  986. }
  987. }
  988. return $rowsAffected;
  989. }
  990. /**
  991. * Fetches rows by primary key. The argument specifies one or more primary
  992. * key value(s). To find multiple rows by primary key, the argument must
  993. * be an array.
  994. *
  995. * This method accepts a variable number of arguments. If the table has a
  996. * multi-column primary key, the number of arguments must be the same as
  997. * the number of columns in the primary key. To find multiple rows in a
  998. * table with a multi-column primary key, each argument must be an array
  999. * with the same number of elements.
  1000. *
  1001. * The find() method always returns a Rowset object, even if only one row
  1002. * was found.
  1003. *
  1004. * @param mixed $key The value(s) of the primary keys.
  1005. * @return Zend_Db_Table_Rowset_Abstract Row(s) matching the criteria.
  1006. * @throws Zend_Db_Table_Exception
  1007. */
  1008. public function find()
  1009. {
  1010. $this->_setupPrimaryKey();
  1011. $args = func_get_args();
  1012. $keyNames = array_values((array) $this->_primary);
  1013. if (count($args) < count($keyNames)) {
  1014. require_once 'Zend/Db/Table/Exception.php';
  1015. throw new Zend_Db_Table_Exception("Too few columns for the primary key");
  1016. }
  1017. if (count($args) > count($keyNames)) {
  1018. require_once 'Zend/Db/Table/Exception.php';
  1019. throw new Zend_Db_Table_Exception("Too many columns for the primary key");
  1020. }
  1021. $whereList = array();
  1022. $numberTerms = 0;
  1023. foreach ($args as $keyPosition => $keyValues) {
  1024. // Coerce the values to an array.
  1025. // Don't simply typecast to array, because the values
  1026. // might be Zend_Db_Expr objects.
  1027. if (!is_array($keyValues)) {
  1028. $keyValues = array($keyValues);
  1029. }
  1030. if ($numberTerms == 0) {
  1031. $numberTerms = count($keyValues);
  1032. } else if (count($keyValues) != $numberTerms) {
  1033. require_once 'Zend/Db/Table/Exception.php';
  1034. throw new Zend_Db_Table_Exception("Missing value(s) for the primary key");
  1035. }
  1036. for ($i = 0; $i < count($keyValues); ++$i) {
  1037. if (!isset($whereList[$i])) {
  1038. $whereList[$i] = array();
  1039. }
  1040. $whereList[$i][$keyPosition] = $keyValues[$i];
  1041. }
  1042. }
  1043. $whereClause = null;
  1044. if (count($whereList)) {
  1045. $whereOrTerms = array();
  1046. foreach ($whereList as $keyValueSets) {
  1047. $whereAndTerms = array();
  1048. foreach ($keyValueSets as $keyPosition => $keyValue) {
  1049. $type = $this->_metadata[$keyNames[$keyPosition]]['DATA_TYPE'];
  1050. $tableName = $this->_db->quoteTableAs($this->_name, null, true);
  1051. $columnName = $this->_db->quoteIdentifier($keyNames[$keyPosition], true);
  1052. $whereAndTerms[] = $this->_db->quoteInto(
  1053. $tableName . '.' . $columnName . ' = ?',
  1054. $keyValue, $type);
  1055. }
  1056. $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')';
  1057. }
  1058. $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')';
  1059. }
  1060. return $this->fetchAll($whereClause);
  1061. }
  1062. /**
  1063. * Fetches all rows.
  1064. *
  1065. * Honors the Zend_Db_Adapter fetch mode.
  1066. *
  1067. * @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
  1068. * @param string|array $order OPTIONAL An SQL ORDER clause.
  1069. * @param int $count OPTIONAL An SQL LIMIT count.
  1070. * @param int $offset OPTIONAL An SQL LIMIT offset.
  1071. * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
  1072. */
  1073. public function fetchAll($where = null, $order = null, $count = null, $offset = null)
  1074. {
  1075. if (!($where instanceof Zend_Db_Table_Select)) {
  1076. $select = $this->select();
  1077. if ($where !== null) {
  1078. $this->_where($select, $where);
  1079. }
  1080. if ($order !== null) {
  1081. $this->_order($select, $order);
  1082. }
  1083. if ($count !== null || $offset !== null) {
  1084. $select->limit($count, $offset);
  1085. }
  1086. } else {
  1087. $select = $where;
  1088. }
  1089. $rows = $this->_fetch($select);
  1090. $data = array(
  1091. 'table' => $this,
  1092. 'data' => $rows,
  1093. 'readOnly' => $select->isReadOnly(),
  1094. 'rowClass' => $this->_rowClass,
  1095. 'stored' => true
  1096. );
  1097. if (!class_exists($this->_rowsetClass)) {
  1098. require_once 'Zend/Loader.php';
  1099. Zend_Loader::loadClass($this->_rowsetClass);
  1100. }
  1101. return new $this->_rowsetClass($data);
  1102. }
  1103. /**
  1104. * Fetches one row in an object of type Zend_Db_Table_Row_Abstract,
  1105. * or returns null if no row matches the specified criteria.
  1106. *
  1107. * @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
  1108. * @param string|array $order OPTIONAL An SQL ORDER clause.
  1109. * @return Zend_Db_Table_Row_Abstract|null The row results per the
  1110. * Zend_Db_Adapter fetch mode, or null if no row found.
  1111. */
  1112. public function fetchRow($where = null, $order = null)
  1113. {
  1114. if (!($where instanceof Zend_Db_Table_Select)) {
  1115. $select = $this->select();
  1116. if ($where !== null) {
  1117. $this->_where($select, $where);
  1118. }
  1119. if ($order !== null) {
  1120. $this->_order($select, $order);
  1121. }
  1122. $select->limit(1);
  1123. } else {
  1124. $select = $where->limit(1);
  1125. }
  1126. $rows = $this->_fetch($select);
  1127. if (count($rows) == 0) {
  1128. return null;
  1129. }
  1130. $data = array(
  1131. 'table' => $this,
  1132. 'data' => $rows[0],
  1133. 'readOnly' => $select->isReadOnly(),
  1134. 'stored' => true
  1135. );
  1136. if (!class_exists($this->_rowClass)) {
  1137. require_once 'Zend/Loader.php';
  1138. Zend_Loader::loadClass($this->_rowClass);
  1139. }
  1140. return new $this->_rowClass($data);
  1141. }
  1142. /**
  1143. * Fetches a new blank row (not from the database).
  1144. *
  1145. * @return Zend_Db_Table_Row_Abstract
  1146. * @deprecated since 0.9.3 - use createRow() instead.
  1147. */
  1148. public function fetchNew()
  1149. {
  1150. return $this->createRow();
  1151. }
  1152. /**
  1153. * Fetches a new blank row (not from the database).
  1154. *
  1155. * @param array $data OPTIONAL data to populate in the new row.
  1156. * @param string $defaultSource OPTIONAL flag to force default values into new row
  1157. * @return Zend_Db_Table_Row_Abstract
  1158. */
  1159. public function createRow(array $data = array(), $defaultSource = null)
  1160. {
  1161. $cols = $this->_getCols();
  1162. $defaults = array_combine($cols, array_fill(0, count($cols), null));
  1163. // nothing provided at call-time, take the class value
  1164. if ($defaultSource == null) {
  1165. $defaultSource = $this->_defaultSource;
  1166. }
  1167. if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
  1168. $defaultSource = self::DEFAULT_NONE;
  1169. }
  1170. if ($defaultSource == self::DEFAULT_DB) {
  1171. foreach ($this->_metadata as $metadataName => $metadata) {
  1172. if (($metadata['DEFAULT'] != null) &&
  1173. ($metadata['NULLABLE'] !== true || ($metadata['NULLABLE'] === true && isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] === true)) &&
  1174. (!(isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] === false))) {
  1175. $defaults[$metadataName] = $metadata['DEFAULT'];
  1176. }
  1177. }
  1178. } elseif ($defaultSource == self::DEFAULT_CLASS && $this->_defaultValues) {
  1179. foreach ($this->_defaultValues as $defaultName => $defaultValue) {
  1180. if (array_key_exists($defaultName, $defaults)) {
  1181. $defaults[$defaultName] = $defaultValue;
  1182. }
  1183. }
  1184. }
  1185. $config = array(
  1186. 'table' => $this,
  1187. 'data' => $defaults,
  1188. 'readOnly' => false,
  1189. 'stored' => false
  1190. );
  1191. if (!class_exists($this->_rowClass)) {
  1192. require_once 'Zend/Loader.php';
  1193. Zend_Loader::loadClass($this->_rowClass);
  1194. }
  1195. $row = new $this->_rowClass($config);
  1196. $row->setFromArray($data);
  1197. return $row;
  1198. }
  1199. /**
  1200. * Generate WHERE clause from user-supplied string or array
  1201. *
  1202. * @param string|array $where OPTIONAL An SQL WHERE clause.
  1203. * @return Zend_Db_Table_Select
  1204. */
  1205. protected function _where(Zend_Db_Table_Select $select, $where)
  1206. {
  1207. $where = (array) $where;
  1208. foreach ($where as $key => $val) {
  1209. // is $key an int?
  1210. if (is_int($key)) {
  1211. // $val is the full condition
  1212. $select->where($val);
  1213. } else {
  1214. // $key is the condition with placeholder,
  1215. // and $val is quoted into the condition
  1216. $select->where($key, $val);
  1217. }
  1218. }
  1219. return $select;
  1220. }
  1221. /**
  1222. * Generate ORDER clause from user-supplied string or array
  1223. *
  1224. * @param string|array $order OPTIONAL An SQL ORDER clause.
  1225. * @return Zend_Db_Table_Select
  1226. */
  1227. protected function _order(Zend_Db_Table_Select $select, $order)
  1228. {
  1229. if (!is_array($order)) {
  1230. $order = array($order);
  1231. }
  1232. foreach ($order as $val) {
  1233. $select->order($val);
  1234. }
  1235. return $select;
  1236. }
  1237. /**
  1238. * Support method for fetching rows.
  1239. *
  1240. * @param Zend_Db_Table_Select $select query options.
  1241. * @return array An array containing the row results in FETCH_ASSOC mode.
  1242. */
  1243. protected function _fetch(Zend_Db_Table_Select $select)
  1244. {
  1245. $stmt = $this->_db->query($select);
  1246. $data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  1247. return $data;
  1248. }
  1249. }