-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Db/Statement.php
Zend_Db_Statement_Pdo->_execute(Array(0))
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 Statement
18 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
21 */
22
23 /**
24 * @see Zend_Db
25 */
26 require_once 'Zend/Db.php';
27
28 /**
29 * @see Zend_Db_Statement_Interface
30 */
31 require_once 'Zend/Db/Statement/Interface.php';
32
33 /**
34 * Abstract class to emulate a PDOStatement for native database adapters.
35 *
36 * @category Zend
37 * @package Zend_Db
38 * @subpackage Statement
39 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
40 * @license http://framework.zend.com/license/new-bsd New BSD License
41 */
42 abstract class Zend_Db_Statement implements Zend_Db_Statement_Interface
43 {
44
45 /**
46 * @var resource|object The driver level statement object/resource
47 */
48 protected $_stmt = null;
49
50 /**
51 * @var Zend_Db_Adapter_Abstract
52 */
53 protected $_adapter = null;
54
55 /**
56 * The current fetch mode.
57 *
58 * @var integer
59 */
60 protected $_fetchMode = Zend_Db::FETCH_ASSOC;
61
62 /**
63 * Attributes.
64 *
65 * @var array
66 */
67 protected $_attribute = array();
68
69 /**
70 * Column result bindings.
71 *
72 * @var array
73 */
74 protected $_bindColumn = array();
75
76 /**
77 * Query parameter bindings; covers bindParam() and bindValue().
78 *
79 * @var array
80 */
81 protected $_bindParam = array();
82
83 /**
84 * SQL string split into an array at placeholders.
85 *
86 * @var array
87 */
88 protected $_sqlSplit = array();
89
90 /**
91 * Parameter placeholders in the SQL string by position in the split array.
92 *
93 * @var array
94 */
95 protected $_sqlParam = array();
96
97 /**
98 * @var Zend_Db_Profiler_Query
99 */
100 protected $_queryId = null;
101
102 /**
103 * Constructor for a statement.
104 *
105 * @param Zend_Db_Adapter_Abstract $adapter
106 * @param mixed $sql Either a string or Zend_Db_Select.
107 */
108 public function __construct($adapter, $sql)
109 {
110 $this->_adapter = $adapter;
111 if ($sql instanceof Zend_Db_Select) {
112 $sql = $sql->assemble();
113 }
114 $this->_parseParameters($sql);
115 $this->_prepare($sql);
116
117 $this->_queryId = $this->_adapter->getProfiler()->queryStart($sql);
118 }
119
120 /**
121 * Internal method called by abstract statment constructor to setup
122 * the driver level statement
123 *
124 * @return void
125 */
126 protected function _prepare($sql)
127 {
128 return;
129 }
130
131 /**
132 * @param string $sql
133 * @return void
134 */
135 protected function _parseParameters($sql)
136 {
137 $sql = $this->_stripQuoted($sql);
138
139 // split into text and params
140 $this->_sqlSplit = preg_split('/(\?|\:[a-zA-Z0-9_]+)/',
141 $sql, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
142
143 // map params
144 $this->_sqlParam = array();
145 foreach ($this->_sqlSplit as $key => $val) {
146 if ($val == '?') {
147 if ($this->_adapter->supportsParameters('positional') === false) {
148 /**
149 * @see Zend_Db_Statement_Exception
150 */
151 require_once 'Zend/Db/Statement/Exception.php';
152 throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$val'");
153 }
154 } else if ($val[0] == ':') {
155 if ($this->_adapter->supportsParameters('named') === false) {
156 /**
157 * @see Zend_Db_Statement_Exception
158 */
159 require_once 'Zend/Db/Statement/Exception.php';
160 throw new Zend_Db_Statement_Exception("Invalid bind-variable name '$val'");
161 }
162 }
163 $this->_sqlParam[] = $val;
164 }
165
166 // set up for binding
167 $this->_bindParam = array();
168 }
169
170 /**
171 * Remove parts of a SQL string that contain quoted strings
172 * of values or identifiers.
173 *
174 * @param string $sql
175 * @return string
176 */
177 protected function _stripQuoted($sql)
178 {
179
180 // get the character for value quoting
181 // this should be '
182 $q = $this->_adapter->quote('a');
183 $q = $q[0];
184 // get the value used as an escaped quote,
185 // e.g. \' or ''
186 $qe = $this->_adapter->quote($q);
187 $qe = substr($qe, 1, 2);
188 $qe = preg_quote($qe);
189 $escapeChar = substr($qe,0,1);
190 // remove 'foo\'bar'
191 if (!empty($q)) {
192 $escapeChar = preg_quote($escapeChar);
193 // this segfaults only after 65,000 characters instead of 9,000
194 $sql = preg_replace("/$q([^$q{$escapeChar}]*|($qe)*)*$q/s", '', $sql);
195 }
196
197 // get a version of the SQL statement with all quoted
198 // values and delimited identifiers stripped out
199 // remove "foo\"bar"
200 $sql = preg_replace("/\"(\\\\\"|[^\"])*\"/Us", '', $sql);
201
202 // get the character for delimited id quotes,
203 // this is usually " but in MySQL is `
204 $d = $this->_adapter->quoteIdentifier('a');
205 $d = $d[0];
206 // get the value used as an escaped delimited id quote,
207 // e.g. \" or "" or \`
208 $de = $this->_adapter->quoteIdentifier($d);
209 $de = substr($de, 1, 2);
210 $de = preg_quote($de);
211 // Note: $de and $d where never used..., now they are:
212 $sql = preg_replace("/$d($de|\\\\{2}|[^$d])*$d/Us", '', $sql);
213 return $sql;
214 }
215
216 /**
217 * Bind a column of the statement result set to a PHP variable.
218 *
219 * @param string $column Name the column in the result set, either by
220 * position or by name.
221 * @param mixed $param Reference to the PHP variable containing the value.
222 * @param mixed $type OPTIONAL
223 * @return bool
224 */
225 public function bindColumn($column, &$param, $type = null)
226 {
227 $this->_bindColumn[$column] =& $param;
228 return true;
229 }
230
231 /**
232 * Binds a parameter to the specified variable name.
233 *
234 * @param mixed $parameter Name the parameter, either integer or string.
235 * @param mixed $variable Reference to PHP variable containing the value.
236 * @param mixed $type OPTIONAL Datatype of SQL parameter.
237 * @param mixed $length OPTIONAL Length of SQL parameter.
238 * @param mixed $options OPTIONAL Other options.
239 * @return bool
240 */
241 public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
242 {
243 if (!is_int($parameter) && !is_string($parameter)) {
244 /**
245 * @see Zend_Db_Statement_Exception
246 */
247 require_once 'Zend/Db/Statement/Exception.php';
248 throw new Zend_Db_Statement_Exception('Invalid bind-variable position');
249 }
250
251 $position = null;
252 if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) {
253 if ($intval >= 1 || $intval <= count($this->_sqlParam)) {
254 $position = $intval;
255 }
256 } else if ($this->_adapter->supportsParameters('named')) {
257 if ($parameter[0] != ':') {
258 $parameter = ':' . $parameter;
259 }
260 if (in_array($parameter, $this->_sqlParam) !== false) {
261 $position = $parameter;
262 }
263 }
264
265 if ($position === null) {
266 /**
267 * @see Zend_Db_Statement_Exception
268 */
269 require_once 'Zend/Db/Statement/Exception.php';
270 throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$parameter'");
271 }
272
273 // Finally we are assured that $position is valid
274 $this->_bindParam[$position] =& $variable;
275 return $this->_bindParam($position, $variable, $type, $length, $options);
276 }
277
278 /**
279 * Binds a value to a parameter.
280 *
281 * @param mixed $parameter Name the parameter, either integer or string.
282 * @param mixed $value Scalar value to bind to the parameter.
283 * @param mixed $type OPTIONAL Datatype of the parameter.
284 * @return bool
285 */
286 public function bindValue($parameter, $value, $type = null)
287 {
288 return $this->bindParam($parameter, $value, $type);
289 }
290
291 /**
292 * Executes a prepared statement.
293 *
294 * @param array $params OPTIONAL Values to bind to parameter placeholders.
295 * @return bool
296 */
297 public function execute(array $params = null)
298 {
299 /*
300 * Simple case - no query profiler to manage.
301 */
302 if ($this->_queryId === null) {
303 return $this->_execute($params);
304 }
305
306 /*
307 * Do the same thing, but with query profiler
308 * management before and after the execute.
309 */
310 $prof = $this->_adapter->getProfiler();
311 $qp = $prof->getQueryProfile($this->_queryId);
312 if ($qp->hasEnded()) {
313 $this->_queryId = $prof->queryClone($qp);
314 $qp = $prof->getQueryProfile($this->_queryId);
315 }
316 if ($params !== null) {
317 $qp->bindParams($params);
318 } else {
319 $qp->bindParams($this->_bindParam);
320 }
321 $qp->start($this->_queryId);
322
323 $retval = $this->_execute($params);
324
325 $prof->queryEnd($this->_queryId);
326
327 return $retval;
328 }
329
330 /**
331 * Returns an array containing all of the result set rows.
332 *
333 * @param int $style OPTIONAL Fetch mode.
334 * @param int $col OPTIONAL Column number, if fetch mode is by column.
335 * @return array Collection of rows, each in a format by the fetch mode.
336 */
337 public function fetchAll($style = null, $col = null)
338 {
339 $data = array();
340 if ($style === Zend_Db::FETCH_COLUMN && $col === null) {
341 $col = 0;
342 }
343 if ($col === null) {
344 while ($row = $this->fetch($style)) {
345 $data[] = $row;
346 }
347 } else {
348 while (false !== ($val = $this->fetchColumn($col))) {
349 $data[] = $val;
350 }
351 }
352 return $data;
353 }
354
355 /**
356 * Returns a single column from the next row of a result set.
357 *
358 * @param int $col OPTIONAL Position of the column to fetch.
359 * @return string One value from the next row of result set, or false.
360 */
361 public function fetchColumn($col = 0)
362 {
363 $data = array();
364 $col = (int) $col;
365 $row = $this->fetch(Zend_Db::FETCH_NUM);
366 if (!is_array($row)) {
367 return false;
368 }
369 return $row[$col];
370 }
371
372 /**
373 * Fetches the next row and returns it as an object.
374 *
375 * @param string $class OPTIONAL Name of the class to create.
376 * @param array $config OPTIONAL Constructor arguments for the class.
377 * @return mixed One object instance of the specified class, or false.
378 */
379 public function fetchObject($class = 'stdClass', array $config = array())
380 {
381 $obj = new $class($config);
382 $row = $this->fetch(Zend_Db::FETCH_ASSOC);
383 if (!is_array($row)) {
384 return false;
385 }
386 foreach ($row as $key => $val) {
387 $obj->$key = $val;
388 }
389 return $obj;
390 }
391
392 /**
393 * Retrieve a statement attribute.
394 *
395 * @param string $key Attribute name.
396 * @return mixed Attribute value.
397 */
398 public function getAttribute($key)
399 {
400 if (array_key_exists($key, $this->_attribute)) {
401 return $this->_attribute[$key];
402 }
403 }
404
405 /**
406 * Set a statement attribute.
407 *
408 * @param string $key Attribute name.
409 * @param mixed $val Attribute value.
410 * @return bool
411 */
412 public function setAttribute($key, $val)
413 {
414 $this->_attribute[$key] = $val;
415 }
416
417 /**
418 * Set the default fetch mode for this statement.
419 *
420 * @param int $mode The fetch mode.
421 * @return bool
422 * @throws Zend_Db_Statement_Exception
423 */
424 public function setFetchMode($mode)
425 {
426 switch ($mode) {
427 case Zend_Db::FETCH_NUM:
428 case Zend_Db::FETCH_ASSOC:
429 case Zend_Db::FETCH_BOTH:
430 case Zend_Db::FETCH_OBJ:
431 $this->_fetchMode = $mode;
432 break;
433 case Zend_Db::FETCH_BOUND:
434 default:
435 $this->closeCursor();
436 /**
437 * @see Zend_Db_Statement_Exception
438 */
439 require_once 'Zend/Db/Statement/Exception.php';
440 throw new Zend_Db_Statement_Exception('invalid fetch mode');
441 break;
442 }
443 }
444
445 /**
446 * Helper function to map retrieved row
447 * to bound column variables
448 *
449 * @param array $row
450 * @return bool True
451 */
452 public function _fetchBound($row)
453 {
454 foreach ($row as $key => $value) {
455 // bindColumn() takes 1-based integer positions
456 // but fetch() returns 0-based integer indexes
457 if (is_int($key)) {
458 $key++;
459 }
460 // set results only to variables that were bound previously
461 if (isset($this->_bindColumn[$key])) {
462 $this->_bindColumn[$key] = $value;
463 }
464 }
465 return true;
466 }
467
468 /**
469 * Gets the Zend_Db_Adapter_Abstract for this
470 * particular Zend_Db_Statement object.
471 *
472 * @return Zend_Db_Adapter_Abstract
473 */
474 public function getAdapter()
475 {
476 return $this->_adapter;
477 }
478
479 /**
480 * Gets the resource or object setup by the
481 * _parse
482 * @return unknown_type
483 */
484 public function getDriverStatement()
485 {
486 return $this->_stmt;
487 }
488 }
489
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Db/Adapter/Abstract.php
Zend_Db_Statement->execute(Array(0))
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 Adapter
18 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
21 */
22
23
24 /**
25 * @see Zend_Db
26 */
27 require_once 'Zend/Db.php';
28
29 /**
30 * @see Zend_Db_Select
31 */
32 require_once 'Zend/Db/Select.php';
33
34 /**
35 * Class for connecting to SQL databases and performing common operations.
36 *
37 * @category Zend
38 * @package Zend_Db
39 * @subpackage Adapter
40 * @copyright Copyright (c) 2005-2015 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_Adapter_Abstract
44 {
45
46 /**
47 * User-provided configuration
48 *
49 * @var array
50 */
51 protected $_config = array();
52
53 /**
54 * Fetch mode
55 *
56 * @var integer
57 */
58 protected $_fetchMode = Zend_Db::FETCH_ASSOC;
59
60 /**
61 * Query profiler object, of type Zend_Db_Profiler
62 * or a subclass of that.
63 *
64 * @var Zend_Db_Profiler
65 */
66 protected $_profiler;
67
68 /**
69 * Default class name for a DB statement.
70 *
71 * @var string
72 */
73 protected $_defaultStmtClass = 'Zend_Db_Statement';
74
75 /**
76 * Default class name for the profiler object.
77 *
78 * @var string
79 */
80 protected $_defaultProfilerClass = 'Zend_Db_Profiler';
81
82 /**
83 * Database connection
84 *
85 * @var object|resource|null
86 */
87 protected $_connection = null;
88
89 /**
90 * Specifies the case of column names retrieved in queries
91 * Options
92 * Zend_Db::CASE_NATURAL (default)
93 * Zend_Db::CASE_LOWER
94 * Zend_Db::CASE_UPPER
95 *
96 * @var integer
97 */
98 protected $_caseFolding = Zend_Db::CASE_NATURAL;
99
100 /**
101 * Specifies whether the adapter automatically quotes identifiers.
102 * If true, most SQL generated by Zend_Db classes applies
103 * identifier quoting automatically.
104 * If false, developer must quote identifiers themselves
105 * by calling quoteIdentifier().
106 *
107 * @var bool
108 */
109 protected $_autoQuoteIdentifiers = true;
110
111 /**
112 * Keys are UPPERCASE SQL datatypes or the constants
113 * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
114 *
115 * Values are:
116 * 0 = 32-bit integer
117 * 1 = 64-bit integer
118 * 2 = float or decimal
119 *
120 * @var array Associative array of datatypes to values 0, 1, or 2.
121 */
122 protected $_numericDataTypes = array(
123 Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
124 Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
125 Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE
126 );
127
128 /** Weither or not that object can get serialized
129 *
130 * @var bool
131 */
132 protected $_allowSerialization = true;
133
134 /**
135 * Weither or not the database should be reconnected
136 * to that adapter when waking up
137 *
138 * @var bool
139 */
140 protected $_autoReconnectOnUnserialize = false;
141
142 /**
143 * Constructor.
144 *
145 * $config is an array of key/value pairs or an instance of Zend_Config
146 * containing configuration options. These options are common to most adapters:
147 *
148 * dbname => (string) The name of the database to user
149 * username => (string) Connect to the database as this username.
150 * password => (string) Password associated with the username.
151 * host => (string) What host to connect to, defaults to localhost
152 *
153 * Some options are used on a case-by-case basis by adapters:
154 *
155 * port => (string) The port of the database
156 * persistent => (boolean) Whether to use a persistent connection or not, defaults to false
157 * protocol => (string) The network protocol, defaults to TCPIP
158 * caseFolding => (int) style of case-alteration used for identifiers
159 * socket => (string) The socket or named pipe that should be used
160 *
161 * @param array|Zend_Config $config An array or instance of Zend_Config having configuration data
162 * @throws Zend_Db_Adapter_Exception
163 */
164 public function __construct($config)
165 {
166 /*
167 * Verify that adapter parameters are in an array.
168 */
169 if (!is_array($config)) {
170 /*
171 * Convert Zend_Config argument to a plain array.
172 */
173 if ($config instanceof Zend_Config) {
174 $config = $config->toArray();
175 } else {
176 /**
177 * @see Zend_Db_Adapter_Exception
178 */
179 require_once 'Zend/Db/Adapter/Exception.php';
180 throw new Zend_Db_Adapter_Exception('Adapter parameters must be in an array or a Zend_Config object');
181 }
182 }
183
184 $this->_checkRequiredOptions($config);
185
186 $options = array(
187 Zend_Db::CASE_FOLDING => $this->_caseFolding,
188 Zend_Db::AUTO_QUOTE_IDENTIFIERS => $this->_autoQuoteIdentifiers,
189 Zend_Db::FETCH_MODE => $this->_fetchMode,
190 );
191 $driverOptions = array();
192
193 /*
194 * normalize the config and merge it with the defaults
195 */
196 if (array_key_exists('options', $config)) {
197 // can't use array_merge() because keys might be integers
198 foreach ((array) $config['options'] as $key => $value) {
199 $options[$key] = $value;
200 }
201 }
202 if (array_key_exists('driver_options', $config)) {
203 if (!empty($config['driver_options'])) {
204 // can't use array_merge() because keys might be integers
205 foreach ((array) $config['driver_options'] as $key => $value) {
206 $driverOptions[$key] = $value;
207 }
208 }
209 }
210
211 if (!isset($config['charset'])) {
212 $config['charset'] = null;
213 }
214
215 if (!isset($config['persistent'])) {
216 $config['persistent'] = false;
217 }
218
219 $this->_config = array_merge($this->_config, $config);
220 $this->_config['options'] = $options;
221 $this->_config['driver_options'] = $driverOptions;
222
223
224 // obtain the case setting, if there is one
225 if (array_key_exists(Zend_Db::CASE_FOLDING, $options)) {
226 $case = (int) $options[Zend_Db::CASE_FOLDING];
227 switch ($case) {
228 case Zend_Db::CASE_LOWER:
229 case Zend_Db::CASE_UPPER:
230 case Zend_Db::CASE_NATURAL:
231 $this->_caseFolding = $case;
232 break;
233 default:
234 /** @see Zend_Db_Adapter_Exception */
235 require_once 'Zend/Db/Adapter/Exception.php';
236 throw new Zend_Db_Adapter_Exception('Case must be one of the following constants: '
237 . 'Zend_Db::CASE_NATURAL, Zend_Db::CASE_LOWER, Zend_Db::CASE_UPPER');
238 }
239 }
240
241 if (array_key_exists(Zend_Db::FETCH_MODE, $options)) {
242 if (is_string($options[Zend_Db::FETCH_MODE])) {
243 $constant = 'Zend_Db::FETCH_' . strtoupper($options[Zend_Db::FETCH_MODE]);
244 if(defined($constant)) {
245 $options[Zend_Db::FETCH_MODE] = constant($constant);
246 }
247 }
248 $this->setFetchMode((int) $options[Zend_Db::FETCH_MODE]);
249 }
250
251 // obtain quoting property if there is one
252 if (array_key_exists(Zend_Db::AUTO_QUOTE_IDENTIFIERS, $options)) {
253 $this->_autoQuoteIdentifiers = (bool) $options[Zend_Db::AUTO_QUOTE_IDENTIFIERS];
254 }
255
256 // obtain allow serialization property if there is one
257 if (array_key_exists(Zend_Db::ALLOW_SERIALIZATION, $options)) {
258 $this->_allowSerialization = (bool) $options[Zend_Db::ALLOW_SERIALIZATION];
259 }
260
261 // obtain auto reconnect on unserialize property if there is one
262 if (array_key_exists(Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE, $options)) {
263 $this->_autoReconnectOnUnserialize = (bool) $options[Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE];
264 }
265
266 // create a profiler object
267 $profiler = false;
268 if (array_key_exists(Zend_Db::PROFILER, $this->_config)) {
269 $profiler = $this->_config[Zend_Db::PROFILER];
270 unset($this->_config[Zend_Db::PROFILER]);
271 }
272 $this->setProfiler($profiler);
273 }
274
275 /**
276 * Check for config options that are mandatory.
277 * Throw exceptions if any are missing.
278 *
279 * @param array $config
280 * @throws Zend_Db_Adapter_Exception
281 */
282 protected function _checkRequiredOptions(array $config)
283 {
284 // we need at least a dbname
285 if (! array_key_exists('dbname', $config)) {
286 /** @see Zend_Db_Adapter_Exception */
287 require_once 'Zend/Db/Adapter/Exception.php';
288 throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'dbname' that names the database instance");
289 }
290
291 if (! array_key_exists('password', $config)) {
292 /**
293 * @see Zend_Db_Adapter_Exception
294 */
295 require_once 'Zend/Db/Adapter/Exception.php';
296 throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'password' for login credentials");
297 }
298
299 if (! array_key_exists('username', $config)) {
300 /**
301 * @see Zend_Db_Adapter_Exception
302 */
303 require_once 'Zend/Db/Adapter/Exception.php';
304 throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'username' for login credentials");
305 }
306 }
307
308 /**
309 * Returns the underlying database connection object or resource.
310 * If not presently connected, this initiates the connection.
311 *
312 * @return object|resource|null
313 */
314 public function getConnection()
315 {
316 $this->_connect();
317 return $this->_connection;
318 }
319
320 /**
321 * Returns the configuration variables in this adapter.
322 *
323 * @return array
324 */
325 public function getConfig()
326 {
327 return $this->_config;
328 }
329
330 /**
331 * Set the adapter's profiler object.
332 *
333 * The argument may be a boolean, an associative array, an instance of
334 * Zend_Db_Profiler, or an instance of Zend_Config.
335 *
336 * A boolean argument sets the profiler to enabled if true, or disabled if
337 * false. The profiler class is the adapter's default profiler class,
338 * Zend_Db_Profiler.
339 *
340 * An instance of Zend_Db_Profiler sets the adapter's instance to that
341 * object. The profiler is enabled and disabled separately.
342 *
343 * An associative array argument may contain any of the keys 'enabled',
344 * 'class', and 'instance'. The 'enabled' and 'instance' keys correspond to the
345 * boolean and object types documented above. The 'class' key is used to name a
346 * class to use for a custom profiler. The class must be Zend_Db_Profiler or a
347 * subclass. The class is instantiated with no constructor arguments. The 'class'
348 * option is ignored when the 'instance' option is supplied.
349 *
350 * An object of type Zend_Config may contain the properties 'enabled', 'class', and
351 * 'instance', just as if an associative array had been passed instead.
352 *
353 * @param Zend_Db_Profiler|Zend_Config|array|boolean $profiler
354 * @return Zend_Db_Adapter_Abstract Provides a fluent interface
355 * @throws Zend_Db_Profiler_Exception if the object instance or class specified
356 * is not Zend_Db_Profiler or an extension of that class.
357 */
358 public function setProfiler($profiler)
359 {
360 $enabled = null;
361 $profilerClass = $this->_defaultProfilerClass;
362 $profilerInstance = null;
363
364 if ($profilerIsObject = is_object($profiler)) {
365 if ($profiler instanceof Zend_Db_Profiler) {
366 $profilerInstance = $profiler;
367 } else if ($profiler instanceof Zend_Config) {
368 $profiler = $profiler->toArray();
369 } else {
370 /**
371 * @see Zend_Db_Profiler_Exception
372 */
373 require_once 'Zend/Db/Profiler/Exception.php';
374 throw new Zend_Db_Profiler_Exception('Profiler argument must be an instance of either Zend_Db_Profiler'
375 . ' or Zend_Config when provided as an object');
376 }
377 }
378
379 if (is_array($profiler)) {
380 if (isset($profiler['enabled'])) {
381 $enabled = (bool) $profiler['enabled'];
382 }
383 if (isset($profiler['class'])) {
384 $profilerClass = $profiler['class'];
385 }
386 if (isset($profiler['instance'])) {
387 $profilerInstance = $profiler['instance'];
388 }
389 } else if (!$profilerIsObject) {
390 $enabled = (bool) $profiler;
391 }
392
393 if ($profilerInstance === null) {
394 if (!class_exists($profilerClass)) {
395 require_once 'Zend/Loader.php';
396 Zend_Loader::loadClass($profilerClass);
397 }
398 $profilerInstance = new $profilerClass();
399 }
400
401 if (!$profilerInstance instanceof Zend_Db_Profiler) {
402 /** @see Zend_Db_Profiler_Exception */
403 require_once 'Zend/Db/Profiler/Exception.php';
404 throw new Zend_Db_Profiler_Exception('Class ' . get_class($profilerInstance) . ' does not extend '
405 . 'Zend_Db_Profiler');
406 }
407
408 if (null !== $enabled) {
409 $profilerInstance->setEnabled($enabled);
410 }
411
412 $this->_profiler = $profilerInstance;
413
414 return $this;
415 }
416
417
418 /**
419 * Returns the profiler for this adapter.
420 *
421 * @return Zend_Db_Profiler
422 */
423 public function getProfiler()
424 {
425 return $this->_profiler;
426 }
427
428 /**
429 * Get the default statement class.
430 *
431 * @return string
432 */
433 public function getStatementClass()
434 {
435 return $this->_defaultStmtClass;
436 }
437
438 /**
439 * Set the default statement class.
440 *
441 * @return Zend_Db_Adapter_Abstract Fluent interface
442 */
443 public function setStatementClass($class)
444 {
445 $this->_defaultStmtClass = $class;
446 return $this;
447 }
448
449 /**
450 * Prepares and executes an SQL statement with bound data.
451 *
452 * @param mixed $sql The SQL statement with placeholders.
453 * May be a string or Zend_Db_Select.
454 * @param mixed $bind An array of data to bind to the placeholders.
455 * @return Zend_Db_Statement_Interface
456 */
457 public function query($sql, $bind = array())
458 {
459 // connect to the database if needed
460 $this->_connect();
461
462 // is the $sql a Zend_Db_Select object?
463 if ($sql instanceof Zend_Db_Select) {
464 if (empty($bind)) {
465 $bind = $sql->getBind();
466 }
467
468 $sql = $sql->assemble();
469 }
470
471 // make sure $bind to an array;
472 // don't use (array) typecasting because
473 // because $bind may be a Zend_Db_Expr object
474 if (!is_array($bind)) {
475 $bind = array($bind);
476 }
477
478 // prepare and execute the statement with profiling
479 $stmt = $this->prepare($sql);
480 $stmt->execute($bind);
481
482 // return the results embedded in the prepared statement object
483 $stmt->setFetchMode($this->_fetchMode);
484 return $stmt;
485 }
486
487 /**
488 * Leave autocommit mode and begin a transaction.
489 *
490 * @return Zend_Db_Adapter_Abstract
491 */
492 public function beginTransaction()
493 {
494 $this->_connect();
495 $q = $this->_profiler->queryStart('begin', Zend_Db_Profiler::TRANSACTION);
496 $this->_beginTransaction();
497 $this->_profiler->queryEnd($q);
498 return $this;
499 }
500
501 /**
502 * Commit a transaction and return to autocommit mode.
503 *
504 * @return Zend_Db_Adapter_Abstract
505 */
506 public function commit()
507 {
508 $this->_connect();
509 $q = $this->_profiler->queryStart('commit', Zend_Db_Profiler::TRANSACTION);
510 $this->_commit();
511 $this->_profiler->queryEnd($q);
512 return $this;
513 }
514
515 /**
516 * Roll back a transaction and return to autocommit mode.
517 *
518 * @return Zend_Db_Adapter_Abstract
519 */
520 public function rollBack()
521 {
522 $this->_connect();
523 $q = $this->_profiler->queryStart('rollback', Zend_Db_Profiler::TRANSACTION);
524 $this->_rollBack();
525 $this->_profiler->queryEnd($q);
526 return $this;
527 }
528
529 /**
530 * Inserts a table row with specified data.
531 *
532 * @param mixed $table The table to insert data into.
533 * @param array $bind Column-value pairs.
534 * @return int The number of affected rows.
535 * @throws Zend_Db_Adapter_Exception
536 */
537 public function insert($table, array $bind)
538 {
539 // extract and quote col names from the array keys
540 $cols = array();
541 $vals = array();
542 $i = 0;
543 foreach ($bind as $col => $val) {
544 $cols[] = $this->quoteIdentifier($col, true);
545 if ($val instanceof Zend_Db_Expr) {
546 $vals[] = $val->__toString();
547 unset($bind[$col]);
548 } else {
549 if ($this->supportsParameters('positional')) {
550 $vals[] = '?';
551 } else {
552 if ($this->supportsParameters('named')) {
553 unset($bind[$col]);
554 $bind[':col'.$i] = $val;
555 $vals[] = ':col'.$i;
556 $i++;
557 } else {
558 /** @see Zend_Db_Adapter_Exception */
559 require_once 'Zend/Db/Adapter/Exception.php';
560 throw new Zend_Db_Adapter_Exception(get_class($this) ." doesn't support positional or named binding");
561 }
562 }
563 }
564 }
565
566 // build the statement
567 $sql = "INSERT INTO "
568 . $this->quoteIdentifier($table, true)
569 . ' (' . implode(', ', $cols) . ') '
570 . 'VALUES (' . implode(', ', $vals) . ')';
571
572 // execute the statement and return the number of affected rows
573 if ($this->supportsParameters('positional')) {
574 $bind = array_values($bind);
575 }
576 $stmt = $this->query($sql, $bind);
577 $result = $stmt->rowCount();
578 return $result;
579 }
580
581 /**
582 * Updates table rows with specified data based on a WHERE clause.
583 *
584 * @param mixed $table The table to update.
585 * @param array $bind Column-value pairs.
586 * @param mixed $where UPDATE WHERE clause(s).
587 * @return int The number of affected rows.
588 * @throws Zend_Db_Adapter_Exception
589 */
590 public function update($table, array $bind, $where = '')
591 {
592 /**
593 * Build "col = ?" pairs for the statement,
594 * except for Zend_Db_Expr which is treated literally.
595 */
596 $set = array();
597 $i = 0;
598 foreach ($bind as $col => $val) {
599 if ($val instanceof Zend_Db_Expr) {
600 $val = $val->__toString();
601 unset($bind[$col]);
602 } else {
603 if ($this->supportsParameters('positional')) {
604 $val = '?';
605 } else {
606 if ($this->supportsParameters('named')) {
607 unset($bind[$col]);
608 $bind[':col'.$i] = $val;
609 $val = ':col'.$i;
610 $i++;
611 } else {
612 /** @see Zend_Db_Adapter_Exception */
613 require_once 'Zend/Db/Adapter/Exception.php';
614 throw new Zend_Db_Adapter_Exception(get_class($this) ." doesn't support positional or named binding");
615 }
616 }
617 }
618 $set[] = $this->quoteIdentifier($col, true) . ' = ' . $val;
619 }
620
621 $where = $this->_whereExpr($where);
622
623 /**
624 * Build the UPDATE statement
625 */
626 $sql = "UPDATE "
627 . $this->quoteIdentifier($table, true)
628 . ' SET ' . implode(', ', $set)
629 . (($where) ? " WHERE $where" : '');
630
631 /**
632 * Execute the statement and return the number of affected rows
633 */
634 if ($this->supportsParameters('positional')) {
635 $stmt = $this->query($sql, array_values($bind));
636 } else {
637 $stmt = $this->query($sql, $bind);
638 }
639 $result = $stmt->rowCount();
640 return $result;
641 }
642
643 /**
644 * Deletes table rows based on a WHERE clause.
645 *
646 * @param mixed $table The table to update.
647 * @param mixed $where DELETE WHERE clause(s).
648 * @return int The number of affected rows.
649 */
650 public function delete($table, $where = '')
651 {
652 $where = $this->_whereExpr($where);
653
654 /**
655 * Build the DELETE statement
656 */
657 $sql = "DELETE FROM "
658 . $this->quoteIdentifier($table, true)
659 . (($where) ? " WHERE $where" : '');
660
661 /**
662 * Execute the statement and return the number of affected rows
663 */
664 $stmt = $this->query($sql);
665 $result = $stmt->rowCount();
666 return $result;
667 }
668
669 /**
670 * Convert an array, string, or Zend_Db_Expr object
671 * into a string to put in a WHERE clause.
672 *
673 * @param mixed $where
674 * @return string
675 */
676 protected function _whereExpr($where)
677 {
678 if (empty($where)) {
679 return $where;
680 }
681 if (!is_array($where)) {
682 $where = array($where);
683 }
684 foreach ($where as $cond => &$term) {
685 // is $cond an int? (i.e. Not a condition)
686 if (is_int($cond)) {
687 // $term is the full condition
688 if ($term instanceof Zend_Db_Expr) {
689 $term = $term->__toString();
690 }
691 } else {
692 // $cond is the condition with placeholder,
693 // and $term is quoted into the condition
694 $term = $this->quoteInto($cond, $term);
695 }
696 $term = '(' . $term . ')';
697 }
698
699 $where = implode(' AND ', $where);
700 return $where;
701 }
702
703 /**
704 * Creates and returns a new Zend_Db_Select object for this adapter.
705 *
706 * @return Zend_Db_Select
707 */
708 public function select()
709 {
710 return new Zend_Db_Select($this);
711 }
712
713 /**
714 * Get the fetch mode.
715 *
716 * @return int
717 */
718 public function getFetchMode()
719 {
720 return $this->_fetchMode;
721 }
722
723 /**
724 * Fetches all SQL result rows as a sequential array.
725 * Uses the current fetchMode for the adapter.
726 *
727 * @param string|Zend_Db_Select $sql An SQL SELECT statement.
728 * @param mixed $bind Data to bind into SELECT placeholders.
729 * @param mixed $fetchMode Override current fetch mode.
730 * @return array
731 */
732 public function fetchAll($sql, $bind = array(), $fetchMode = null)
733 {
734 if ($fetchMode === null) {
735 $fetchMode = $this->_fetchMode;
736 }
737 $stmt = $this->query($sql, $bind);
738 $result = $stmt->fetchAll($fetchMode);
739 return $result;
740 }
741
742 /**
743 * Fetches the first row of the SQL result.
744 * Uses the current fetchMode for the adapter.
745 *
746 * @param string|Zend_Db_Select $sql An SQL SELECT statement.
747 * @param mixed $bind Data to bind into SELECT placeholders.
748 * @param mixed $fetchMode Override current fetch mode.
749 * @return mixed Array, object, or scalar depending on fetch mode.
750 */
751 public function fetchRow($sql, $bind = array(), $fetchMode = null)
752 {
753 if ($fetchMode === null) {
754 $fetchMode = $this->_fetchMode;
755 }
756 $stmt = $this->query($sql, $bind);
757 $result = $stmt->fetch($fetchMode);
758 return $result;
759 }
760
761 /**
762 * Fetches all SQL result rows as an associative array.
763 *
764 * The first column is the key, the entire row array is the
765 * value. You should construct the query to be sure that
766 * the first column contains unique values, or else
767 * rows with duplicate values in the first column will
768 * overwrite previous data.
769 *
770 * @param string|Zend_Db_Select $sql An SQL SELECT statement.
771 * @param mixed $bind Data to bind into SELECT placeholders.
772 * @return array
773 */
774 public function fetchAssoc($sql, $bind = array())
775 {
776 $stmt = $this->query($sql, $bind);
777 $data = array();
778 while ($row = $stmt->fetch(Zend_Db::FETCH_ASSOC)) {
779 $tmp = array_values(array_slice($row, 0, 1));
780 $data[$tmp[0]] = $row;
781 }
782 return $data;
783 }
784
785 /**
786 * Fetches the first column of all SQL result rows as an array.
787 *
788 * @param string|Zend_Db_Select $sql An SQL SELECT statement.
789 * @param mixed $bind Data to bind into SELECT placeholders.
790 * @return array
791 */
792 public function fetchCol($sql, $bind = array())
793 {
794 $stmt = $this->query($sql, $bind);
795 $result = $stmt->fetchAll(Zend_Db::FETCH_COLUMN, 0);
796 return $result;
797 }
798
799 /**
800 * Fetches all SQL result rows as an array of key-value pairs.
801 *
802 * The first column is the key, the second column is the
803 * value.
804 *
805 * @param string|Zend_Db_Select $sql An SQL SELECT statement.
806 * @param mixed $bind Data to bind into SELECT placeholders.
807 * @return array
808 */
809 public function fetchPairs($sql, $bind = array())
810 {
811 $stmt = $this->query($sql, $bind);
812 $data = array();
813 while ($row = $stmt->fetch(Zend_Db::FETCH_NUM)) {
814 $data[$row[0]] = $row[1];
815 }
816 return $data;
817 }
818
819 /**
820 * Fetches the first column of the first row of the SQL result.
821 *
822 * @param string|Zend_Db_Select $sql An SQL SELECT statement.
823 * @param mixed $bind Data to bind into SELECT placeholders.
824 * @return string
825 */
826 public function fetchOne($sql, $bind = array())
827 {
828 $stmt = $this->query($sql, $bind);
829 $result = $stmt->fetchColumn(0);
830 return $result;
831 }
832
833 /**
834 * Quote a raw string.
835 *
836 * @param string $value Raw string
837 * @return string Quoted string
838 */
839 protected function _quote($value)
840 {
841 if (is_int($value)) {
842 return $value;
843 } elseif (is_float($value)) {
844 return sprintf('%F', $value);
845 }
846 return "'" . addcslashes($value, "\000\n\r\\'\"\032") . "'";
847 }
848
849 /**
850 * Safely quotes a value for an SQL statement.
851 *
852 * If an array is passed as the value, the array values are quoted
853 * and then returned as a comma-separated string.
854 *
855 * @param mixed $value The value to quote.
856 * @param mixed $type OPTIONAL the SQL datatype name, or constant, or null.
857 * @return mixed An SQL-safe quoted value (or string of separated values).
858 */
859 public function quote($value, $type = null)
860 {
861 $this->_connect();
862
863 if ($value instanceof Zend_Db_Select) {
864 return '(' . $value->assemble() . ')';
865 }
866
867 if ($value instanceof Zend_Db_Expr) {
868 return $value->__toString();
869 }
870
871 if (is_array($value)) {
872 foreach ($value as &$val) {
873 $val = $this->quote($val, $type);
874 }
875 return implode(', ', $value);
876 }
877
878 if ($type !== null && array_key_exists($type = strtoupper($type), $this->_numericDataTypes)) {
879 $quotedValue = '0';
880 switch ($this->_numericDataTypes[$type]) {
881 case Zend_Db::INT_TYPE: // 32-bit integer
882 $quotedValue = (string) intval($value);
883 break;
884 case Zend_Db::BIGINT_TYPE: // 64-bit integer
885 // ANSI SQL-style hex literals (e.g. x'[\dA-F]+')
886 // are not supported here, because these are string
887 // literals, not numeric literals.
888 if (preg_match('/^(
889 [+-]? # optional sign
890 (?:
891 0[Xx][\da-fA-F]+ # ODBC-style hexadecimal
892 |\d+ # decimal or octal, or MySQL ZEROFILL decimal
893 (?:[eE][+-]?\d+)? # optional exponent on decimals or octals
894 )
895 )/x',
896 (string) $value, $matches)) {
897 $quotedValue = $matches[1];
898 }
899 break;
900 case Zend_Db::FLOAT_TYPE: // float or decimal
901 $quotedValue = sprintf('%F', $value);
902 }
903 return $quotedValue;
904 }
905
906 return $this->_quote($value);
907 }
908
909 /**
910 * Quotes a value and places into a piece of text at a placeholder.
911 *
912 * The placeholder is a question-mark; all placeholders will be replaced
913 * with the quoted value. For example:
914 *
915 * <code>
916 * $text = "WHERE date < ?";
917 * $date = "2005-01-02";
918 * $safe = $sql->quoteInto($text, $date);
919 * // $safe = "WHERE date < '2005-01-02'"
920 * </code>
921 *
922 * @param string $text The text with a placeholder.
923 * @param mixed $value The value to quote.
924 * @param string $type OPTIONAL SQL datatype
925 * @param integer $count OPTIONAL count of placeholders to replace
926 * @return string An SQL-safe quoted value placed into the original text.
927 */
928 public function quoteInto($text, $value, $type = null, $count = null)
929 {
930 if ($count === null) {
931 return str_replace('?', $this->quote($value, $type), $text);
932 } else {
933 return implode($this->quote($value, $type), explode('?', $text, $count + 1));
934 }
935 }
936
937 /**
938 * Quotes an identifier.
939 *
940 * Accepts a string representing a qualified indentifier. For Example:
941 * <code>
942 * $adapter->quoteIdentifier('myschema.mytable')
943 * </code>
944 * Returns: "myschema"."mytable"
945 *
946 * Or, an array of one or more identifiers that may form a qualified identifier:
947 * <code>
948 * $adapter->quoteIdentifier(array('myschema','my.table'))
949 * </code>
950 * Returns: "myschema"."my.table"
951 *
952 * The actual quote character surrounding the identifiers may vary depending on
953 * the adapter.
954 *
955 * @param string|array|Zend_Db_Expr $ident The identifier.
956 * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
957 * @return string The quoted identifier.
958 */
959 public function quoteIdentifier($ident, $auto=false)
960 {
961 return $this->_quoteIdentifierAs($ident, null, $auto);
962 }
963
964 /**
965 * Quote a column identifier and alias.
966 *
967 * @param string|array|Zend_Db_Expr $ident The identifier or expression.
968 * @param string $alias An alias for the column.
969 * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
970 * @return string The quoted identifier and alias.
971 */
972 public function quoteColumnAs($ident, $alias, $auto=false)
973 {
974 return $this->_quoteIdentifierAs($ident, $alias, $auto);
975 }
976
977 /**
978 * Quote a table identifier and alias.
979 *
980 * @param string|array|Zend_Db_Expr $ident The identifier or expression.
981 * @param string $alias An alias for the table.
982 * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
983 * @return string The quoted identifier and alias.
984 */
985 public function quoteTableAs($ident, $alias = null, $auto = false)
986 {
987 return $this->_quoteIdentifierAs($ident, $alias, $auto);
988 }
989
990 /**
991 * Quote an identifier and an optional alias.
992 *
993 * @param string|array|Zend_Db_Expr $ident The identifier or expression.
994 * @param string $alias An optional alias.
995 * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
996 * @param string $as The string to add between the identifier/expression and the alias.
997 * @return string The quoted identifier and alias.
998 */
999 protected function _quoteIdentifierAs($ident, $alias = null, $auto = false, $as = ' AS ')
1000 {
1001 if ($ident instanceof Zend_Db_Expr) {
1002 $quoted = $ident->__toString();
1003 } elseif ($ident instanceof Zend_Db_Select) {
1004 $quoted = '(' . $ident->assemble() . ')';
1005 } else {
1006 if (is_string($ident)) {
1007 $ident = explode('.', $ident);
1008 }
1009 if (is_array($ident)) {
1010 $segments = array();
1011 foreach ($ident as $segment) {
1012 if ($segment instanceof Zend_Db_Expr) {
1013 $segments[] = $segment->__toString();
1014 } else {
1015 $segments[] = $this->_quoteIdentifier($segment, $auto);
1016 }
1017 }
1018 if ($alias !== null && end($ident) == $alias) {
1019 $alias = null;
1020 }
1021 $quoted = implode('.', $segments);
1022 } else {
1023 $quoted = $this->_quoteIdentifier($ident, $auto);
1024 }
1025 }
1026 if ($alias !== null) {
1027 $quoted .= $as . $this->_quoteIdentifier($alias, $auto);
1028 }
1029 return $quoted;
1030 }
1031
1032 /**
1033 * Quote an identifier.
1034 *
1035 * @param string $value The identifier or expression.
1036 * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
1037 * @return string The quoted identifier and alias.
1038 */
1039 protected function _quoteIdentifier($value, $auto=false)
1040 {
1041 if ($auto === false || $this->_autoQuoteIdentifiers === true) {
1042 $q = $this->getQuoteIdentifierSymbol();
1043 return ($q . str_replace("$q", "$q$q", $value) . $q);
1044 }
1045 return $value;
1046 }
1047
1048 /**
1049 * Returns the symbol the adapter uses for delimited identifiers.
1050 *
1051 * @return string
1052 */
1053 public function getQuoteIdentifierSymbol()
1054 {
1055 return '"';
1056 }
1057
1058 /**
1059 * Return the most recent value from the specified sequence in the database.
1060 * This is supported only on RDBMS brands that support sequences
1061 * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
1062 *
1063 * @param string $sequenceName
1064 * @return string
1065 */
1066 public function lastSequenceId($sequenceName)
1067 {
1068 return null;
1069 }
1070
1071 /**
1072 * Generate a new value from the specified sequence in the database, and return it.
1073 * This is supported only on RDBMS brands that support sequences
1074 * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
1075 *
1076 * @param string $sequenceName
1077 * @return string
1078 */
1079 public function nextSequenceId($sequenceName)
1080 {
1081 return null;
1082 }
1083
1084 /**
1085 * Helper method to change the case of the strings used
1086 * when returning result sets in FETCH_ASSOC and FETCH_BOTH
1087 * modes.
1088 *
1089 * This is not intended to be used by application code,
1090 * but the method must be public so the Statement class
1091 * can invoke it.
1092 *
1093 * @param string $key
1094 * @return string
1095 */
1096 public function foldCase($key)
1097 {
1098 switch ($this->_caseFolding) {
1099 case Zend_Db::CASE_LOWER:
1100 $value = strtolower((string) $key);
1101 break;
1102 case Zend_Db::CASE_UPPER:
1103 $value = strtoupper((string) $key);
1104 break;
1105 case Zend_Db::CASE_NATURAL:
1106 default:
1107 $value = (string) $key;
1108 }
1109 return $value;
1110 }
1111
1112 /**
1113 * called when object is getting serialized
1114 * This disconnects the DB object that cant be serialized
1115 *
1116 * @throws Zend_Db_Adapter_Exception
1117 * @return array
1118 */
1119 public function __sleep()
1120 {
1121 if ($this->_allowSerialization == false) {
1122 /** @see Zend_Db_Adapter_Exception */
1123 require_once 'Zend/Db/Adapter/Exception.php';
1124 throw new Zend_Db_Adapter_Exception(
1125 get_class($this) . ' is not allowed to be serialized'
1126 );
1127 }
1128 $this->_connection = null;
1129
1130 return array_keys(
1131 array_diff_key(get_object_vars($this), array('_connection' => null))
1132 );
1133 }
1134
1135 /**
1136 * called when object is getting unserialized
1137 *
1138 * @return void
1139 */
1140 public function __wakeup()
1141 {
1142 if ($this->_autoReconnectOnUnserialize == true) {
1143 $this->getConnection();
1144 }
1145 }
1146
1147 /**
1148 * Abstract Methods
1149 */
1150
1151 /**
1152 * Returns a list of the tables in the database.
1153 *
1154 * @return array
1155 */
1156 abstract public function listTables();
1157
1158 /**
1159 * Returns the column descriptions for a table.
1160 *
1161 * The return value is an associative array keyed by the column name,
1162 * as returned by the RDBMS.
1163 *
1164 * The value of each array element is an associative array
1165 * with the following keys:
1166 *
1167 * SCHEMA_NAME => string; name of database or schema
1168 * TABLE_NAME => string;
1169 * COLUMN_NAME => string; column name
1170 * COLUMN_POSITION => number; ordinal position of column in table
1171 * DATA_TYPE => string; SQL datatype name of column
1172 * DEFAULT => string; default expression of column, null if none
1173 * NULLABLE => boolean; true if column can have nulls
1174 * LENGTH => number; length of CHAR/VARCHAR
1175 * SCALE => number; scale of NUMERIC/DECIMAL
1176 * PRECISION => number; precision of NUMERIC/DECIMAL
1177 * UNSIGNED => boolean; unsigned property of an integer type
1178 * PRIMARY => boolean; true if column is part of the primary key
1179 * PRIMARY_POSITION => integer; position of column in primary key
1180 *
1181 * @param string $tableName
1182 * @param string $schemaName OPTIONAL
1183 * @return array
1184 */
1185 abstract public function describeTable($tableName, $schemaName = null);
1186
1187 /**
1188 * Creates a connection to the database.
1189 *
1190 * @return void
1191 */
1192 abstract protected function _connect();
1193
1194 /**
1195 * Test if a connection is active
1196 *
1197 * @return boolean
1198 */
1199 abstract public function isConnected();
1200
1201 /**
1202 * Force the connection to close.
1203 *
1204 * @return void
1205 */
1206 abstract public function closeConnection();
1207
1208 /**
1209 * Prepare a statement and return a PDOStatement-like object.
1210 *
1211 * @param string|Zend_Db_Select $sql SQL query
1212 * @return Zend_Db_Statement|PDOStatement
1213 */
1214 abstract public function prepare($sql);
1215
1216 /**
1217 * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
1218 *
1219 * As a convention, on RDBMS brands that support sequences
1220 * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
1221 * from the arguments and returns the last id generated by that sequence.
1222 * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
1223 * returns the last value generated for such a column, and the table name
1224 * argument is disregarded.
1225 *
1226 * @param string $tableName OPTIONAL Name of table.
1227 * @param string $primaryKey OPTIONAL Name of primary key column.
1228 * @return string
1229 */
1230 abstract public function lastInsertId($tableName = null, $primaryKey = null);
1231
1232 /**
1233 * Begin a transaction.
1234 */
1235 abstract protected function _beginTransaction();
1236
1237 /**
1238 * Commit a transaction.
1239 */
1240 abstract protected function _commit();
1241
1242 /**
1243 * Roll-back a transaction.
1244 */
1245 abstract protected function _rollBack();
1246
1247 /**
1248 * Set the fetch mode.
1249 *
1250 * @param integer $mode
1251 * @return void
1252 * @throws Zend_Db_Adapter_Exception
1253 */
1254 abstract public function setFetchMode($mode);
1255
1256 /**
1257 * Adds an adapter-specific LIMIT clause to the SELECT statement.
1258 *
1259 * @param mixed $sql
1260 * @param integer $count
1261 * @param integer $offset
1262 * @return string
1263 */
1264 abstract public function limit($sql, $count, $offset = 0);
1265
1266 /**
1267 * Check if the adapter supports real SQL parameters.
1268 *
1269 * @param string $type 'positional' or 'named'
1270 * @return bool
1271 */
1272 abstract public function supportsParameters($type);
1273
1274 /**
1275 * Retrieve server version in PHP style
1276 *
1277 * @return string
1278 */
1279 abstract public function getServerVersion();
1280 }
1281
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php
Zend_Db_Adapter_Abstract->query('SELECT `co`.* FROM `comentarios` AS `co` WHERE (co.aprobado=1) AND (co.modelo_id=?) ORDER BY `co`.`fecha` DESC LIMIT 15', Array(0))
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 Adapter
18 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
21 */
22
23
24 /**
25 * @see Zend_Db_Adapter_Abstract
26 */
27 require_once 'Zend/Db/Adapter/Abstract.php';
28
29
30 /**
31 * @see Zend_Db_Statement_Pdo
32 */
33 require_once 'Zend/Db/Statement/Pdo.php';
34
35
36 /**
37 * Class for connecting to SQL databases and performing common operations using PDO.
38 *
39 * @category Zend
40 * @package Zend_Db
41 * @subpackage Adapter
42 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
43 * @license http://framework.zend.com/license/new-bsd New BSD License
44 */
45 abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
46 {
47
48 /**
49 * Default class name for a DB statement.
50 *
51 * @var string
52 */
53 protected $_defaultStmtClass = 'Zend_Db_Statement_Pdo';
54
55 /**
56 * Creates a PDO DSN for the adapter from $this->_config settings.
57 *
58 * @return string
59 */
60 protected function _dsn()
61 {
62 // baseline of DSN parts
63 $dsn = $this->_config;
64
65 // don't pass the username, password, charset, persistent and driver_options in the DSN
66 unset($dsn['username']);
67 unset($dsn['password']);
68 unset($dsn['options']);
69 unset($dsn['charset']);
70 unset($dsn['persistent']);
71 unset($dsn['driver_options']);
72
73 // use all remaining parts in the DSN
74 foreach ($dsn as $key => $val) {
75 $dsn[$key] = "$key=$val";
76 }
77
78 return $this->_pdoType . ':' . implode(';', $dsn);
79 }
80
81 /**
82 * Creates a PDO object and connects to the database.
83 *
84 * @return void
85 * @throws Zend_Db_Adapter_Exception
86 */
87 protected function _connect()
88 {
89 // if we already have a PDO object, no need to re-connect.
90 if ($this->_connection) {
91 return;
92 }
93
94 // get the dsn first, because some adapters alter the $_pdoType
95 $dsn = $this->_dsn();
96
97 // check for PDO extension
98 if (!extension_loaded('pdo')) {
99 /**
100 * @see Zend_Db_Adapter_Exception
101 */
102 require_once 'Zend/Db/Adapter/Exception.php';
103 throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
104 }
105
106 // check the PDO driver is available
107 if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
108 /**
109 * @see Zend_Db_Adapter_Exception
110 */
111 require_once 'Zend/Db/Adapter/Exception.php';
112 throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
113 }
114
115 // create PDO connection
116 $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);
117
118 // add the persistence flag if we find it in our config array
119 if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {
120 $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
121 }
122
123 try {
124 $this->_connection = new PDO(
125 $dsn,
126 $this->_config['username'],
127 $this->_config['password'],
128 $this->_config['driver_options']
129 );
130
131 $this->_profiler->queryEnd($q);
132
133 // set the PDO connection to perform case-folding on array keys, or not
134 $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
135
136 // always use exceptions.
137 $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
138
139 } catch (PDOException $e) {
140 /**
141 * @see Zend_Db_Adapter_Exception
142 */
143 require_once 'Zend/Db/Adapter/Exception.php';
144 throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
145 }
146
147 }
148
149 /**
150 * Test if a connection is active
151 *
152 * @return boolean
153 */
154 public function isConnected()
155 {
156 return ((bool) ($this->_connection instanceof PDO));
157 }
158
159 /**
160 * Force the connection to close.
161 *
162 * @return void
163 */
164 public function closeConnection()
165 {
166 $this->_connection = null;
167 }
168
169 /**
170 * Prepares an SQL statement.
171 *
172 * @param string $sql The SQL statement with placeholders.
173 * @param array $bind An array of data to bind to the placeholders.
174 * @return PDOStatement
175 */
176 public function prepare($sql)
177 {
178 $this->_connect();
179 $stmtClass = $this->_defaultStmtClass;
180 if (!class_exists($stmtClass)) {
181 require_once 'Zend/Loader.php';
182 Zend_Loader::loadClass($stmtClass);
183 }
184 $stmt = new $stmtClass($this, $sql);
185 $stmt->setFetchMode($this->_fetchMode);
186 return $stmt;
187 }
188
189 /**
190 * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
191 *
192 * As a convention, on RDBMS brands that support sequences
193 * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
194 * from the arguments and returns the last id generated by that sequence.
195 * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
196 * returns the last value generated for such a column, and the table name
197 * argument is disregarded.
198 *
199 * On RDBMS brands that don't support sequences, $tableName and $primaryKey
200 * are ignored.
201 *
202 * @param string $tableName OPTIONAL Name of table.
203 * @param string $primaryKey OPTIONAL Name of primary key column.
204 * @return string
205 */
206 public function lastInsertId($tableName = null, $primaryKey = null)
207 {
208 $this->_connect();
209 return $this->_connection->lastInsertId();
210 }
211
212 /**
213 * Special handling for PDO query().
214 * All bind parameter names must begin with ':'
215 *
216 * @param string|Zend_Db_Select $sql The SQL statement with placeholders.
217 * @param array $bind An array of data to bind to the placeholders.
218 * @return Zend_Db_Statement_Pdo
219 * @throws Zend_Db_Adapter_Exception To re-throw PDOException.
220 */
221 public function query($sql, $bind = array())
222 {
223 if (empty($bind) && $sql instanceof Zend_Db_Select) {
224 $bind = $sql->getBind();
225 }
226
227 if (is_array($bind)) {
228 foreach ($bind as $name => $value) {
229 if (!is_int($name) && !preg_match('/^:/', $name)) {
230 $newName = ":$name";
231 unset($bind[$name]);
232 $bind[$newName] = $value;
233 }
234 }
235 }
236
237 try {
238 return parent::query($sql, $bind);
239 } catch (PDOException $e) {
240 /**
241 * @see Zend_Db_Statement_Exception
242 */
243 require_once 'Zend/Db/Statement/Exception.php';
244 throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
245 }
246 }
247
248 /**
249 * Executes an SQL statement and return the number of affected rows
250 *
251 * @param mixed $sql The SQL statement with placeholders.
252 * May be a string or Zend_Db_Select.
253 * @return integer Number of rows that were modified
254 * or deleted by the SQL statement
255 */
256 public function exec($sql)
257 {
258 if ($sql instanceof Zend_Db_Select) {
259 $sql = $sql->assemble();
260 }
261
262 try {
263 $affected = $this->getConnection()->exec($sql);
264
265 if ($affected === false) {
266 $errorInfo = $this->getConnection()->errorInfo();
267 /**
268 * @see Zend_Db_Adapter_Exception
269 */
270 require_once 'Zend/Db/Adapter/Exception.php';
271 throw new Zend_Db_Adapter_Exception($errorInfo[2]);
272 }
273
274 return $affected;
275 } catch (PDOException $e) {
276 /**
277 * @see Zend_Db_Adapter_Exception
278 */
279 require_once 'Zend/Db/Adapter/Exception.php';
280 throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
281 }
282 }
283
284 /**
285 * Quote a raw string.
286 *
287 * @param string $value Raw string
288 * @return string Quoted string
289 */
290 protected function _quote($value)
291 {
292 if (is_int($value) || is_float($value)) {
293 return $value;
294 }
295 $this->_connect();
296 return $this->_connection->quote($value);
297 }
298
299 /**
300 * Begin a transaction.
301 */
302 protected function _beginTransaction()
303 {
304 $this->_connect();
305 $this->_connection->beginTransaction();
306 }
307
308 /**
309 * Commit a transaction.
310 */
311 protected function _commit()
312 {
313 $this->_connect();
314 $this->_connection->commit();
315 }
316
317 /**
318 * Roll-back a transaction.
319 */
320 protected function _rollBack() {
321 $this->_connect();
322 $this->_connection->rollBack();
323 }
324
325 /**
326 * Set the PDO fetch mode.
327 *
328 * @todo Support FETCH_CLASS and FETCH_INTO.
329 *
330 * @param int $mode A PDO fetch mode.
331 * @return void
332 * @throws Zend_Db_Adapter_Exception
333 */
334 public function setFetchMode($mode)
335 {
336 //check for PDO extension
337 if (!extension_loaded('pdo')) {
338 /**
339 * @see Zend_Db_Adapter_Exception
340 */
341 require_once 'Zend/Db/Adapter/Exception.php';
342 throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
343 }
344 switch ($mode) {
345 case PDO::FETCH_LAZY:
346 case PDO::FETCH_ASSOC:
347 case PDO::FETCH_NUM:
348 case PDO::FETCH_BOTH:
349 case PDO::FETCH_NAMED:
350 case PDO::FETCH_OBJ:
351 $this->_fetchMode = $mode;
352 break;
353 default:
354 /**
355 * @see Zend_Db_Adapter_Exception
356 */
357 require_once 'Zend/Db/Adapter/Exception.php';
358 throw new Zend_Db_Adapter_Exception("Invalid fetch mode '$mode' specified");
359 break;
360 }
361 }
362
363 /**
364 * Check if the adapter supports real SQL parameters.
365 *
366 * @param string $type 'positional' or 'named'
367 * @return bool
368 */
369 public function supportsParameters($type)
370 {
371 switch ($type) {
372 case 'positional':
373 case 'named':
374 default:
375 return true;
376 }
377 }
378
379 /**
380 * Retrieve server version in PHP style
381 *
382 * @return string
383 */
384 public function getServerVersion()
385 {
386 $this->_connect();
387 try {
388 $version = $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
389 } catch (PDOException $e) {
390 // In case of the driver doesn't support getting attributes
391 return null;
392 }
393 $matches = null;
394 if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $version, $matches)) {
395 return $matches[1];
396 } else {
397 return null;
398 }
399 }
400 }
401
402
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Db/Table/Abstract.php
Zend_Db_Adapter_Pdo_Abstract->query(Zend_Db_Table_Select)
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
21 */
22
23 /**
24 * @see Zend_Db_Adapter_Abstract
25 */
26 require_once 'Zend/Db/Adapter/Abstract.php';
27
28 /**
29 * @see Zend_Db_Adapter_Abstract
30 */
31 require_once 'Zend/Db/Select.php';
32
33 /**
34 * @see Zend_Db
35 */
36 require_once 'Zend/Db.php';
37
38 /**
39 * Class for SQL table interface.
40 *
41 * @category Zend
42 * @package Zend_Db
43 * @subpackage Table
44 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
45 * @license http://framework.zend.com/license/new-bsd New BSD License
46 */
47 abstract class Zend_Db_Table_Abstract
48 {
49
50 const ADAPTER = 'db';
51 const DEFINITION = 'definition';
52 const DEFINITION_CONFIG_NAME = 'definitionConfigName';
53 const SCHEMA = 'schema';
54 const NAME = 'name';
55 const PRIMARY = 'primary';
56 const COLS = 'cols';
57 const METADATA = 'metadata';
58 const METADATA_CACHE = 'metadataCache';
59 const METADATA_CACHE_IN_CLASS = 'metadataCacheInClass';
60 const ROW_CLASS = 'rowClass';
61 const ROWSET_CLASS = 'rowsetClass';
62 const REFERENCE_MAP = 'referenceMap';
63 const DEPENDENT_TABLES = 'dependentTables';
64 const SEQUENCE = 'sequence';
65
66 const COLUMNS = 'columns';
67 const REF_TABLE_CLASS = 'refTableClass';
68 const REF_COLUMNS = 'refColumns';
69 const ON_DELETE = 'onDelete';
70 const ON_UPDATE = 'onUpdate';
71
72 const CASCADE = 'cascade';
73 const CASCADE_RECURSE = 'cascadeRecurse';
74 const RESTRICT = 'restrict';
75 const SET_NULL = 'setNull';
76
77 const DEFAULT_NONE = 'defaultNone';
78 const DEFAULT_CLASS = 'defaultClass';
79 const DEFAULT_DB = 'defaultDb';
80
81 const SELECT_WITH_FROM_PART = true;
82 const SELECT_WITHOUT_FROM_PART = false;
83
84 /**
85 * Default Zend_Db_Adapter_Abstract object.
86 *
87 * @var Zend_Db_Adapter_Abstract
88 */
89 protected static $_defaultDb;
90
91 /**
92 * Optional Zend_Db_Table_Definition object
93 *
94 * @var unknown_type
95 */
96 protected $_definition = null;
97
98 /**
99 * Optional definition config name used in concrete implementation
100 *
101 * @var string
102 */
103 protected $_definitionConfigName = null;
104
105 /**
106 * Default cache for information provided by the adapter's describeTable() method.
107 *
108 * @var Zend_Cache_Core
109 */
110 protected static $_defaultMetadataCache = null;
111
112 /**
113 * Zend_Db_Adapter_Abstract object.
114 *
115 * @var Zend_Db_Adapter_Abstract
116 */
117 protected $_db;
118
119 /**
120 * The schema name (default null means current schema)
121 *
122 * @var array
123 */
124 protected $_schema = null;
125
126 /**
127 * The table name.
128 *
129 * @var string
130 */
131 protected $_name = null;
132
133 /**
134 * The table column names derived from Zend_Db_Adapter_Abstract::describeTable().
135 *
136 * @var array
137 */
138 protected $_cols;
139
140 /**
141 * The primary key column or columns.
142 * A compound key should be declared as an array.
143 * You may declare a single-column primary key
144 * as a string.
145 *
146 * @var mixed
147 */
148 protected $_primary = null;
149
150 /**
151 * If your primary key is a compound key, and one of the columns uses
152 * an auto-increment or sequence-generated value, set _identity
153 * to the ordinal index in the $_primary array for that column.
154 * Note this index is the position of the column in the primary key,
155 * not the position of the column in the table. The primary key
156 * array is 1-based.
157 *
158 * @var integer
159 */
160 protected $_identity = 1;
161
162 /**
163 * Define the logic for new values in the primary key.
164 * May be a string, boolean true, or boolean false.
165 *
166 * @var mixed
167 */
168 protected $_sequence = true;
169
170 /**
171 * Information provided by the adapter's describeTable() method.
172 *
173 * @var array
174 */
175 protected $_metadata = array();
176
177 /**
178 * Cache for information provided by the adapter's describeTable() method.
179 *
180 * @var Zend_Cache_Core
181 */
182 protected $_metadataCache = null;
183
184 /**
185 * Flag: whether or not to cache metadata in the class
186 * @var bool
187 */
188 protected $_metadataCacheInClass = true;
189
190 /**
191 * Classname for row
192 *
193 * @var string
194 */
195 protected $_rowClass = 'Zend_Db_Table_Row';
196
197 /**
198 * Classname for rowset
199 *
200 * @var string
201 */
202 protected $_rowsetClass = 'Zend_Db_Table_Rowset';
203
204 /**
205 * Associative array map of declarative referential integrity rules.
206 * This array has one entry per foreign key in the current table.
207 * Each key is a mnemonic name for one reference rule.
208 *
209 * Each value is also an associative array, with the following keys:
210 * - columns = array of names of column(s) in the child table.
211 * - refTableClass = class name of the parent table.
212 * - refColumns = array of names of column(s) in the parent table,
213 * in the same order as those in the 'columns' entry.
214 * - onDelete = "cascade" means that a delete in the parent table also
215 * causes a delete of referencing rows in the child table.
216 * - onUpdate = "cascade" means that an update of primary key values in
217 * the parent table also causes an update of referencing
218 * rows in the child table.
219 *
220 * @var array
221 */
222 protected $_referenceMap = array();
223
224 /**
225 * Simple array of class names of tables that are "children" of the current
226 * table, in other words tables that contain a foreign key to this one.
227 * Array elements are not table names; they are class names of classes that
228 * extend Zend_Db_Table_Abstract.
229 *
230 * @var array
231 */
232 protected $_dependentTables = array();
233
234
235 protected $_defaultSource = self::DEFAULT_NONE;
236 protected $_defaultValues = array();
237
238 /**
239 * Constructor.
240 *
241 * Supported params for $config are:
242 * - db = user-supplied instance of database connector,
243 * or key name of registry instance.
244 * - name = table name.
245 * - primary = string or array of primary key(s).
246 * - rowClass = row class name.
247 * - rowsetClass = rowset class name.
248 * - referenceMap = array structure to declare relationship
249 * to parent tables.
250 * - dependentTables = array of child tables.
251 * - metadataCache = cache for information from adapter describeTable().
252 *
253 * @param mixed $config Array of user-specified config options, or just the Db Adapter.
254 * @return void
255 */
256 public function __construct($config = array())
257 {
258 /**
259 * Allow a scalar argument to be the Adapter object or Registry key.
260 */
261 if (!is_array($config)) {
262 $config = array(self::ADAPTER => $config);
263 }
264
265 if ($config) {
266 $this->setOptions($config);
267 }
268
269 $this->_setup();
270 $this->init();
271 }
272
273 /**
274 * setOptions()
275 *
276 * @param array $options
277 * @return Zend_Db_Table_Abstract
278 */
279 public function setOptions(Array $options)
280 {
281 foreach ($options as $key => $value) {
282 switch ($key) {
283 case self::ADAPTER:
284 $this->_setAdapter($value);
285 break;
286 case self::DEFINITION:
287 $this->setDefinition($value);
288 break;
289 case self::DEFINITION_CONFIG_NAME:
290 $this->setDefinitionConfigName($value);
291 break;
292 case self::SCHEMA:
293 $this->_schema = (string) $value;
294 break;
295 case self::NAME:
296 $this->_name = (string) $value;
297 break;
298 case self::PRIMARY:
299 $this->_primary = (array) $value;
300 break;
301 case self::ROW_CLASS:
302 $this->setRowClass($value);
303 break;
304 case self::ROWSET_CLASS:
305 $this->setRowsetClass($value);
306 break;
307 case self::REFERENCE_MAP:
308 $this->setReferences($value);
309 break;
310 case self::DEPENDENT_TABLES:
311 $this->setDependentTables($value);
312 break;
313 case self::METADATA_CACHE:
314 $this->_setMetadataCache($value);
315 break;
316 case self::METADATA_CACHE_IN_CLASS:
317 $this->setMetadataCacheInClass($value);
318 break;
319 case self::SEQUENCE:
320 $this->_setSequence($value);
321 break;
322 default:
323 // ignore unrecognized configuration directive
324 break;
325 }
326 }
327
328 return $this;
329 }
330
331 /**
332 * setDefinition()
333 *
334 * @param Zend_Db_Table_Definition $definition
335 * @return Zend_Db_Table_Abstract
336 */
337 public function setDefinition(Zend_Db_Table_Definition $definition)
338 {
339 $this->_definition = $definition;
340 return $this;
341 }
342
343 /**
344 * getDefinition()
345 *
346 * @return Zend_Db_Table_Definition|null
347 */
348 public function getDefinition()
349 {
350 return $this->_definition;
351 }
352
353 /**
354 * setDefinitionConfigName()
355 *
356 * @param string $definition
357 * @return Zend_Db_Table_Abstract
358 */
359 public function setDefinitionConfigName($definitionConfigName)
360 {
361 $this->_definitionConfigName = $definitionConfigName;
362 return $this;
363 }
364
365 /**
366 * getDefinitionConfigName()
367 *
368 * @return string
369 */
370 public function getDefinitionConfigName()
371 {
372 return $this->_definitionConfigName;
373 }
374
375 /**
376 * @param string $classname
377 * @return Zend_Db_Table_Abstract Provides a fluent interface
378 */
379 public function setRowClass($classname)
380 {
381 $this->_rowClass = (string) $classname;
382
383 return $this;
384 }
385
386 /**
387 * @return string
388 */
389 public function getRowClass()
390 {
391 return $this->_rowClass;
392 }
393
394 /**
395 * @param string $classname
396 * @return Zend_Db_Table_Abstract Provides a fluent interface
397 */
398 public function setRowsetClass($classname)
399 {
400 $this->_rowsetClass = (string) $classname;
401
402 return $this;
403 }
404
405 /**
406 * @return string
407 */
408 public function getRowsetClass()
409 {
410 return $this->_rowsetClass;
411 }
412
413 /**
414 * Add a reference to the reference map
415 *
416 * @param string $ruleKey
417 * @param string|array $columns
418 * @param string $refTableClass
419 * @param string|array $refColumns
420 * @param string $onDelete
421 * @param string $onUpdate
422 * @return Zend_Db_Table_Abstract
423 */
424 public function addReference($ruleKey, $columns, $refTableClass, $refColumns,
425 $onDelete = null, $onUpdate = null)
426 {
427 $reference = array(self::COLUMNS => (array) $columns,
428 self::REF_TABLE_CLASS => $refTableClass,
429 self::REF_COLUMNS => (array) $refColumns);
430
431 if (!empty($onDelete)) {
432 $reference[self::ON_DELETE] = $onDelete;
433 }
434
435 if (!empty($onUpdate)) {
436 $reference[self::ON_UPDATE] = $onUpdate;
437 }
438
439 $this->_referenceMap[$ruleKey] = $reference;
440
441 return $this;
442 }
443
444 /**
445 * @param array $referenceMap
446 * @return Zend_Db_Table_Abstract Provides a fluent interface
447 */
448 public function setReferences(array $referenceMap)
449 {
450 $this->_referenceMap = $referenceMap;
451
452 return $this;
453 }
454
455 /**
456 * @param string $tableClassname
457 * @param string $ruleKey OPTIONAL
458 * @return array
459 * @throws Zend_Db_Table_Exception
460 */
461 public function getReference($tableClassname, $ruleKey = null)
462 {
463 $thisClass = get_class($this);
464 if ($thisClass === 'Zend_Db_Table') {
465 $thisClass = $this->_definitionConfigName;
466 }
467 $refMap = $this->_getReferenceMapNormalized();
468 if ($ruleKey !== null) {
469 if (!isset($refMap[$ruleKey])) {
470 require_once "Zend/Db/Table/Exception.php";
471 throw new Zend_Db_Table_Exception("No reference rule \"$ruleKey\" from table $thisClass to table $tableClassname");
472 }
473 if ($refMap[$ruleKey][self::REF_TABLE_CLASS] != $tableClassname) {
474 require_once "Zend/Db/Table/Exception.php";
475 throw new Zend_Db_Table_Exception("Reference rule \"$ruleKey\" does not reference table $tableClassname");
476 }
477 return $refMap[$ruleKey];
478 }
479 foreach ($refMap as $reference) {
480 if ($reference[self::REF_TABLE_CLASS] == $tableClassname) {
481 return $reference;
482 }
483 }
484 require_once "Zend/Db/Table/Exception.php";
485 throw new Zend_Db_Table_Exception("No reference from table $thisClass to table $tableClassname");
486 }
487
488 /**
489 * @param array $dependentTables
490 * @return Zend_Db_Table_Abstract Provides a fluent interface
491 */
492 public function setDependentTables(array $dependentTables)
493 {
494 $this->_dependentTables = $dependentTables;
495
496 return $this;
497 }
498
499 /**
500 * @return array
501 */
502 public function getDependentTables()
503 {
504 return $this->_dependentTables;
505 }
506
507 /**
508 * set the defaultSource property - this tells the table class where to find default values
509 *
510 * @param string $defaultSource
511 * @return Zend_Db_Table_Abstract
512 */
513 public function setDefaultSource($defaultSource = self::DEFAULT_NONE)
514 {
515 if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
516 $defaultSource = self::DEFAULT_NONE;
517 }
518
519 $this->_defaultSource = $defaultSource;
520 return $this;
521 }
522
523 /**
524 * returns the default source flag that determines where defaultSources come from
525 *
526 * @return unknown
527 */
528 public function getDefaultSource()
529 {
530 return $this->_defaultSource;
531 }
532
533 /**
534 * set the default values for the table class
535 *
536 * @param array $defaultValues
537 * @return Zend_Db_Table_Abstract
538 */
539 public function setDefaultValues(Array $defaultValues)
540 {
541 foreach ($defaultValues as $defaultName => $defaultValue) {
542 if (array_key_exists($defaultName, $this->_metadata)) {
543 $this->_defaultValues[$defaultName] = $defaultValue;
544 }
545 }
546 return $this;
547 }
548
549 public function getDefaultValues()
550 {
551 return $this->_defaultValues;
552 }
553
554
555 /**
556 * Sets the default Zend_Db_Adapter_Abstract for all Zend_Db_Table objects.
557 *
558 * @param mixed $db Either an Adapter object, or a string naming a Registry key
559 * @return void
560 */
561 public static function setDefaultAdapter($db = null)
562 {
563 self::$_defaultDb = self::_setupAdapter($db);
564 }
565
566 /**
567 * Gets the default Zend_Db_Adapter_Abstract for all Zend_Db_Table objects.
568 *
569 * @return Zend_Db_Adapter_Abstract or null
570 */
571 public static function getDefaultAdapter()
572 {
573 return self::$_defaultDb;
574 }
575
576 /**
577 * @param mixed $db Either an Adapter object, or a string naming a Registry key
578 * @return Zend_Db_Table_Abstract Provides a fluent interface
579 */
580 protected function _setAdapter($db)
581 {
582 $this->_db = self::_setupAdapter($db);
583 return $this;
584 }
585
586 /**
587 * Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Table object.
588 *
589 * @return Zend_Db_Adapter_Abstract
590 */
591 public function getAdapter()
592 {
593 return $this->_db;
594 }
595
596 /**
597 * @param mixed $db Either an Adapter object, or a string naming a Registry key
598 * @return Zend_Db_Adapter_Abstract
599 * @throws Zend_Db_Table_Exception
600 */
601 protected static function _setupAdapter($db)
602 {
603 if ($db === null) {
604 return null;
605 }
606 if (is_string($db)) {
607 require_once 'Zend/Registry.php';
608 $db = Zend_Registry::get($db);
609 }
610 if (!$db instanceof Zend_Db_Adapter_Abstract) {
611 require_once 'Zend/Db/Table/Exception.php';
612 throw new Zend_Db_Table_Exception('Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored');
613 }
614 return $db;
615 }
616
617 /**
618 * Sets the default metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
619 *
620 * If $defaultMetadataCache is null, then no metadata cache is used by default.
621 *
622 * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
623 * @return void
624 */
625 public static function setDefaultMetadataCache($metadataCache = null)
626 {
627 self::$_defaultMetadataCache = self::_setupMetadataCache($metadataCache);
628 }
629
630 /**
631 * Gets the default metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
632 *
633 * @return Zend_Cache_Core or null
634 */
635 public static function getDefaultMetadataCache()
636 {
637 return self::$_defaultMetadataCache;
638 }
639
640 /**
641 * Sets the metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
642 *
643 * If $metadataCache is null, then no metadata cache is used. Since there is no opportunity to reload metadata
644 * after instantiation, this method need not be public, particularly because that it would have no effect
645 * results in unnecessary API complexity. To configure the metadata cache, use the metadataCache configuration
646 * option for the class constructor upon instantiation.
647 *
648 * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
649 * @return Zend_Db_Table_Abstract Provides a fluent interface
650 */
651 protected function _setMetadataCache($metadataCache)
652 {
653 $this->_metadataCache = self::_setupMetadataCache($metadataCache);
654 return $this;
655 }
656
657 /**
658 * Gets the metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
659 *
660 * @return Zend_Cache_Core or null
661 */
662 public function getMetadataCache()
663 {
664 return $this->_metadataCache;
665 }
666
667 /**
668 * Indicate whether metadata should be cached in the class for the duration
669 * of the instance
670 *
671 * @param bool $flag
672 * @return Zend_Db_Table_Abstract
673 */
674 public function setMetadataCacheInClass($flag)
675 {
676 $this->_metadataCacheInClass = (bool) $flag;
677 return $this;
678 }
679
680 /**
681 * Retrieve flag indicating if metadata should be cached for duration of
682 * instance
683 *
684 * @return bool
685 */
686 public function metadataCacheInClass()
687 {
688 return $this->_metadataCacheInClass;
689 }
690
691 /**
692 * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
693 * @return Zend_Cache_Core
694 * @throws Zend_Db_Table_Exception
695 */
696 protected static function _setupMetadataCache($metadataCache)
697 {
698 if ($metadataCache === null) {
699 return null;
700 }
701 if (is_string($metadataCache)) {
702 require_once 'Zend/Registry.php';
703 $metadataCache = Zend_Registry::get($metadataCache);
704 }
705 if (!$metadataCache instanceof Zend_Cache_Core) {
706 require_once 'Zend/Db/Table/Exception.php';
707 throw new Zend_Db_Table_Exception('Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored');
708 }
709 return $metadataCache;
710 }
711
712 /**
713 * Sets the sequence member, which defines the behavior for generating
714 * primary key values in new rows.
715 * - If this is a string, then the string names the sequence object.
716 * - If this is boolean true, then the key uses an auto-incrementing
717 * or identity mechanism.
718 * - If this is boolean false, then the key is user-defined.
719 * Use this for natural keys, for example.
720 *
721 * @param mixed $sequence
722 * @return Zend_Db_Table_Adapter_Abstract Provides a fluent interface
723 */
724 protected function _setSequence($sequence)
725 {
726 $this->_sequence = $sequence;
727
728 return $this;
729 }
730
731 /**
732 * Turnkey for initialization of a table object.
733 * Calls other protected methods for individual tasks, to make it easier
734 * for a subclass to override part of the setup logic.
735 *
736 * @return void
737 */
738 protected function _setup()
739 {
740 $this->_setupDatabaseAdapter();
741 $this->_setupTableName();
742 }
743
744 /**
745 * Initialize database adapter.
746 *
747 * @return void
748 * @throws Zend_Db_Table_Exception
749 */
750 protected function _setupDatabaseAdapter()
751 {
752 if (! $this->_db) {
753 $this->_db = self::getDefaultAdapter();
754 if (!$this->_db instanceof Zend_Db_Adapter_Abstract) {
755 require_once 'Zend/Db/Table/Exception.php';
756 throw new Zend_Db_Table_Exception('No adapter found for ' . get_class($this));
757 }
758 }
759 }
760
761 /**
762 * Initialize table and schema names.
763 *
764 * If the table name is not set in the class definition,
765 * use the class name itself as the table name.
766 *
767 * A schema name provided with the table name (e.g., "schema.table") overrides
768 * any existing value for $this->_schema.
769 *
770 * @return void
771 */
772 protected function _setupTableName()
773 {
774 if (! $this->_name) {
775 $this->_name = get_class($this);
776 } else if (strpos($this->_name, '.')) {
777 list($this->_schema, $this->_name) = explode('.', $this->_name);
778 }
779 }
780
781 /**
782 * Initializes metadata.
783 *
784 * If metadata cannot be loaded from cache, adapter's describeTable() method is called to discover metadata
785 * information. Returns true if and only if the metadata are loaded from cache.
786 *
787 * @return boolean
788 * @throws Zend_Db_Table_Exception
789 */
790 protected function _setupMetadata()
791 {
792 if ($this->metadataCacheInClass() && (count($this->_metadata) > 0)) {
793 return true;
794 }
795
796 // Assume that metadata will be loaded from cache
797 $isMetadataFromCache = true;
798
799 // If $this has no metadata cache but the class has a default metadata cache
800 if (null === $this->_metadataCache && null !== self::$_defaultMetadataCache) {
801 // Make $this use the default metadata cache of the class
802 $this->_setMetadataCache(self::$_defaultMetadataCache);
803 }
804
805 // If $this has a metadata cache
806 if (null !== $this->_metadataCache) {
807 // Define the cache identifier where the metadata are saved
808
809 //get db configuration
810 $dbConfig = $this->_db->getConfig();
811
812 $port = isset($dbConfig['options']['port'])
813 ? ':'.$dbConfig['options']['port']
814 : (isset($dbConfig['port'])
815 ? ':'.$dbConfig['port']
816 : null);
817
818 $host = isset($dbConfig['options']['host'])
819 ? ':'.$dbConfig['options']['host']
820 : (isset($dbConfig['host'])
821 ? ':'.$dbConfig['host']
822 : null);
823
824 // Define the cache identifier where the metadata are saved
825 $cacheId = md5( // port:host/dbname:schema.table (based on availabilty)
826 $port . $host . '/'. $dbConfig['dbname'] . ':'
827 . $this->_schema. '.' . $this->_name
828 );
829 }
830
831 // If $this has no metadata cache or metadata cache misses
832 if (null === $this->_metadataCache || !($metadata = $this->_metadataCache->load($cacheId))) {
833 // Metadata are not loaded from cache
834 $isMetadataFromCache = false;
835 // Fetch metadata from the adapter's describeTable() method
836 $metadata = $this->_db->describeTable($this->_name, $this->_schema);
837 // If $this has a metadata cache, then cache the metadata
838 if (null !== $this->_metadataCache && !$this->_metadataCache->save($metadata, $cacheId)) {
839 trigger_error('Failed saving metadata to metadataCache', E_USER_NOTICE);
840 }
841 }
842
843 // Assign the metadata to $this
844 $this->_metadata = $metadata;
845
846 // Return whether the metadata were loaded from cache
847 return $isMetadataFromCache;
848 }
849
850 /**
851 * Retrieve table columns
852 *
853 * @return array
854 */
855 protected function _getCols()
856 {
857 if (null === $this->_cols) {
858 $this->_setupMetadata();
859 $this->_cols = array_keys($this->_metadata);
860 }
861 return $this->_cols;
862 }
863
864 /**
865 * Initialize primary key from metadata.
866 * If $_primary is not defined, discover primary keys
867 * from the information returned by describeTable().
868 *
869 * @return void
870 * @throws Zend_Db_Table_Exception
871 */
872 protected function _setupPrimaryKey()
873 {
874 if (!$this->_primary) {
875 $this->_setupMetadata();
876 $this->_primary = array();
877 foreach ($this->_metadata as $col) {
878 if ($col['PRIMARY']) {
879 $this->_primary[ $col['PRIMARY_POSITION'] ] = $col['COLUMN_NAME'];
880 if ($col['IDENTITY']) {
881 $this->_identity = $col['PRIMARY_POSITION'];
882 }
883 }
884 }
885 // if no primary key was specified and none was found in the metadata
886 // then throw an exception.
887 if (empty($this->_primary)) {
888 require_once 'Zend/Db/Table/Exception.php';
889 throw new Zend_Db_Table_Exception("A table must have a primary key, but none was found for table '{$this->_name}'");
890 }
891 } else if (!is_array($this->_primary)) {
892 $this->_primary = array(1 => $this->_primary);
893 } else if (isset($this->_primary[0])) {
894 array_unshift($this->_primary, null);
895 unset($this->_primary[0]);
896 }
897
898 $cols = $this->_getCols();
899 if (! array_intersect((array) $this->_primary, $cols) == (array) $this->_primary) {
900 require_once 'Zend/Db/Table/Exception.php';
901 throw new Zend_Db_Table_Exception("Primary key column(s) ("
902 . implode(',', (array) $this->_primary)
903 . ") are not columns in this table ("
904 . implode(',', $cols)
905 . ")");
906 }
907
908 $primary = (array) $this->_primary;
909 $pkIdentity = $primary[(int) $this->_identity];
910
911 /**
912 * Special case for PostgreSQL: a SERIAL key implicitly uses a sequence
913 * object whose name is "<table>_<column>_seq".
914 */
915 if ($this->_sequence === true && $this->_db instanceof Zend_Db_Adapter_Pdo_Pgsql) {
916 $this->_sequence = $this->_db->quoteIdentifier("{$this->_name}_{$pkIdentity}_seq");
917 if ($this->_schema) {
918 $this->_sequence = $this->_db->quoteIdentifier($this->_schema) . '.' . $this->_sequence;
919 }
920 }
921 }
922
923 /**
924 * Returns a normalized version of the reference map
925 *
926 * @return array
927 */
928 protected function _getReferenceMapNormalized()
929 {
930 $referenceMapNormalized = array();
931
932 foreach ($this->_referenceMap as $rule => $map) {
933
934 $referenceMapNormalized[$rule] = array();
935
936 foreach ($map as $key => $value) {
937 switch ($key) {
938
939 // normalize COLUMNS and REF_COLUMNS to arrays
940 case self::COLUMNS:
941 case self::REF_COLUMNS:
942 if (!is_array($value)) {
943 $referenceMapNormalized[$rule][$key] = array($value);
944 } else {
945 $referenceMapNormalized[$rule][$key] = $value;
946 }
947 break;
948
949 // other values are copied as-is
950 default:
951 $referenceMapNormalized[$rule][$key] = $value;
952 break;
953 }
954 }
955 }
956
957 return $referenceMapNormalized;
958 }
959
960 /**
961 * Initialize object
962 *
963 * Called from {@link __construct()} as final step of object instantiation.
964 *
965 * @return void
966 */
967 public function init()
968 {
969 }
970
971 /**
972 * Returns table information.
973 *
974 * You can elect to return only a part of this information by supplying its key name,
975 * otherwise all information is returned as an array.
976 *
977 * @param string $key The specific info part to return OPTIONAL
978 * @return mixed
979 * @throws Zend_Db_Table_Exception
980 */
981 public function info($key = null)
982 {
983 $this->_setupPrimaryKey();
984
985 $info = array(
986 self::SCHEMA => $this->_schema,
987 self::NAME => $this->_name,
988 self::COLS => $this->_getCols(),
989 self::PRIMARY => (array) $this->_primary,
990 self::METADATA => $this->_metadata,
991 self::ROW_CLASS => $this->getRowClass(),
992 self::ROWSET_CLASS => $this->getRowsetClass(),
993 self::REFERENCE_MAP => $this->_referenceMap,
994 self::DEPENDENT_TABLES => $this->_dependentTables,
995 self::SEQUENCE => $this->_sequence
996 );
997
998 if ($key === null) {
999 return $info;
1000 }
1001
1002 if (!array_key_exists($key, $info)) {
1003 require_once 'Zend/Db/Table/Exception.php';
1004 throw new Zend_Db_Table_Exception('There is no table information for the key "' . $key . '"');
1005 }
1006
1007 return $info[$key];
1008 }
1009
1010 /**
1011 * Returns an instance of a Zend_Db_Table_Select object.
1012 *
1013 * @param bool $withFromPart Whether or not to include the from part of the select based on the table
1014 * @return Zend_Db_Table_Select
1015 */
1016 public function select($withFromPart = self::SELECT_WITHOUT_FROM_PART)
1017 {
1018 require_once 'Zend/Db/Table/Select.php';
1019 $select = new Zend_Db_Table_Select($this);
1020 if ($withFromPart == self::SELECT_WITH_FROM_PART) {
1021 $select->from($this->info(self::NAME), Zend_Db_Table_Select::SQL_WILDCARD, $this->info(self::SCHEMA));
1022 }
1023 return $select;
1024 }
1025
1026 /**
1027 * Inserts a new row.
1028 *
1029 * @param array $data Column-value pairs.
1030 * @return mixed The primary key of the row inserted.
1031 */
1032 public function insert(array $data)
1033 {
1034 $this->_setupPrimaryKey();
1035
1036 /**
1037 * Zend_Db_Table assumes that if you have a compound primary key
1038 * and one of the columns in the key uses a sequence,
1039 * it's the _first_ column in the compound key.
1040 */
1041 $primary = (array) $this->_primary;
1042 $pkIdentity = $primary[(int)$this->_identity];
1043
1044
1045 /**
1046 * If the primary key can be generated automatically, and no value was
1047 * specified in the user-supplied data, then omit it from the tuple.
1048 *
1049 * Note: this checks for sensible values in the supplied primary key
1050 * position of the data. The following values are considered empty:
1051 * null, false, true, '', array()
1052 */
1053 if (array_key_exists($pkIdentity, $data)) {
1054 if ($data[$pkIdentity] === null // null
1055 || $data[$pkIdentity] === '' // empty string
1056 || is_bool($data[$pkIdentity]) // boolean
1057 || (is_array($data[$pkIdentity]) && empty($data[$pkIdentity]))) { // empty array
1058 unset($data[$pkIdentity]);
1059 }
1060 }
1061
1062 /**
1063 * If this table uses a database sequence object and the data does not
1064 * specify a value, then get the next ID from the sequence and add it
1065 * to the row. We assume that only the first column in a compound
1066 * primary key takes a value from a sequence.
1067 */
1068 if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
1069 $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
1070 }
1071
1072 /**
1073 * INSERT the new row.
1074 */
1075 $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1076 $this->_db->insert($tableSpec, $data);
1077
1078 /**
1079 * Fetch the most recent ID generated by an auto-increment
1080 * or IDENTITY column, unless the user has specified a value,
1081 * overriding the auto-increment mechanism.
1082 */
1083 if ($this->_sequence === true && !isset($data[$pkIdentity])) {
1084 $data[$pkIdentity] = $this->_db->lastInsertId();
1085 }
1086
1087 /**
1088 * Return the primary key value if the PK is a single column,
1089 * else return an associative array of the PK column/value pairs.
1090 */
1091 $pkData = array_intersect_key($data, array_flip($primary));
1092 if (count($primary) == 1) {
1093 reset($pkData);
1094 return current($pkData);
1095 }
1096
1097 return $pkData;
1098 }
1099
1100 /**
1101 * Check if the provided column is an identity of the table
1102 *
1103 * @param string $column
1104 * @throws Zend_Db_Table_Exception
1105 * @return boolean
1106 */
1107 public function isIdentity($column)
1108 {
1109 $this->_setupPrimaryKey();
1110
1111 if (!isset($this->_metadata[$column])) {
1112 /**
1113 * @see Zend_Db_Table_Exception
1114 */
1115 require_once 'Zend/Db/Table/Exception.php';
1116
1117 throw new Zend_Db_Table_Exception('Column "' . $column . '" not found in table.');
1118 }
1119
1120 return (bool) $this->_metadata[$column]['IDENTITY'];
1121 }
1122
1123 /**
1124 * Updates existing rows.
1125 *
1126 * @param array $data Column-value pairs.
1127 * @param array|string $where An SQL WHERE clause, or an array of SQL WHERE clauses.
1128 * @return int The number of rows updated.
1129 */
1130 public function update(array $data, $where)
1131 {
1132 $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1133 return $this->_db->update($tableSpec, $data, $where);
1134 }
1135
1136 /**
1137 * Called by a row object for the parent table's class during save() method.
1138 *
1139 * @param string $parentTableClassname
1140 * @param array $oldPrimaryKey
1141 * @param array $newPrimaryKey
1142 * @return int
1143 */
1144 public function _cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey)
1145 {
1146 $this->_setupMetadata();
1147 $rowsAffected = 0;
1148 foreach ($this->_getReferenceMapNormalized() as $map) {
1149 if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_UPDATE])) {
1150 switch ($map[self::ON_UPDATE]) {
1151 case self::CASCADE:
1152 $newRefs = array();
1153 $where = array();
1154 for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
1155 $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
1156 $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
1157 if (array_key_exists($refCol, $newPrimaryKey)) {
1158 $newRefs[$col] = $newPrimaryKey[$refCol];
1159 }
1160 $type = $this->_metadata[$col]['DATA_TYPE'];
1161 $where[] = $this->_db->quoteInto(
1162 $this->_db->quoteIdentifier($col, true) . ' = ?',
1163 $oldPrimaryKey[$refCol], $type);
1164 }
1165 $rowsAffected += $this->update($newRefs, $where);
1166 break;
1167 default:
1168 // no action
1169 break;
1170 }
1171 }
1172 }
1173 return $rowsAffected;
1174 }
1175
1176 /**
1177 * Deletes existing rows.
1178 *
1179 * @param array|string $where SQL WHERE clause(s).
1180 * @return int The number of rows deleted.
1181 */
1182 public function delete($where)
1183 {
1184 $depTables = $this->getDependentTables();
1185 if (!empty($depTables)) {
1186 $resultSet = $this->fetchAll($where);
1187 if (count($resultSet) > 0 ) {
1188 foreach ($resultSet as $row) {
1189 /**
1190 * Execute cascading deletes against dependent tables
1191 */
1192 foreach ($depTables as $tableClass) {
1193 $t = self::getTableFromString($tableClass, $this);
1194 $t->_cascadeDelete(
1195 get_class($this), $row->getPrimaryKey()
1196 );
1197 }
1198 }
1199 }
1200 }
1201
1202 $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1203 return $this->_db->delete($tableSpec, $where);
1204 }
1205
1206 /**
1207 * Called by parent table's class during delete() method.
1208 *
1209 * @param string $parentTableClassname
1210 * @param array $primaryKey
1211 * @return int Number of affected rows
1212 */
1213 public function _cascadeDelete($parentTableClassname, array $primaryKey)
1214 {
1215 // setup metadata
1216 $this->_setupMetadata();
1217
1218 // get this class name
1219 $thisClass = get_class($this);
1220 if ($thisClass === 'Zend_Db_Table') {
1221 $thisClass = $this->_definitionConfigName;
1222 }
1223
1224 $rowsAffected = 0;
1225
1226 foreach ($this->_getReferenceMapNormalized() as $map) {
1227 if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {
1228
1229 $where = array();
1230
1231 // CASCADE or CASCADE_RECURSE
1232 if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
1233 for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
1234 $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
1235 $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
1236 $type = $this->_metadata[$col]['DATA_TYPE'];
1237 $where[] = $this->_db->quoteInto(
1238 $this->_db->quoteIdentifier($col, true) . ' = ?',
1239 $primaryKey[$refCol], $type);
1240 }
1241 }
1242
1243 // CASCADE_RECURSE
1244 if ($map[self::ON_DELETE] == self::CASCADE_RECURSE) {
1245
1246 /**
1247 * Execute cascading deletes against dependent tables
1248 */
1249 $depTables = $this->getDependentTables();
1250 if (!empty($depTables)) {
1251 foreach ($depTables as $tableClass) {
1252 $t = self::getTableFromString($tableClass, $this);
1253 foreach ($this->fetchAll($where) as $depRow) {
1254 $rowsAffected += $t->_cascadeDelete($thisClass, $depRow->getPrimaryKey());
1255 }
1256 }
1257 }
1258 }
1259
1260 // CASCADE or CASCADE_RECURSE
1261 if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
1262 $rowsAffected += $this->delete($where);
1263 }
1264
1265 }
1266 }
1267 return $rowsAffected;
1268 }
1269
1270 /**
1271 * Fetches rows by primary key. The argument specifies one or more primary
1272 * key value(s). To find multiple rows by primary key, the argument must
1273 * be an array.
1274 *
1275 * This method accepts a variable number of arguments. If the table has a
1276 * multi-column primary key, the number of arguments must be the same as
1277 * the number of columns in the primary key. To find multiple rows in a
1278 * table with a multi-column primary key, each argument must be an array
1279 * with the same number of elements.
1280 *
1281 * The find() method always returns a Rowset object, even if only one row
1282 * was found.
1283 *
1284 * @param mixed $key The value(s) of the primary keys.
1285 * @return Zend_Db_Table_Rowset_Abstract Row(s) matching the criteria.
1286 * @throws Zend_Db_Table_Exception
1287 */
1288 public function find()
1289 {
1290 $this->_setupPrimaryKey();
1291 $args = func_get_args();
1292 $keyNames = array_values((array) $this->_primary);
1293
1294 if (count($args) < count($keyNames)) {
1295 require_once 'Zend/Db/Table/Exception.php';
1296 throw new Zend_Db_Table_Exception("Too few columns for the primary key");
1297 }
1298
1299 if (count($args) > count($keyNames)) {
1300 require_once 'Zend/Db/Table/Exception.php';
1301 throw new Zend_Db_Table_Exception("Too many columns for the primary key");
1302 }
1303
1304 $whereList = array();
1305 $numberTerms = 0;
1306 foreach ($args as $keyPosition => $keyValues) {
1307 $keyValuesCount = count($keyValues);
1308 // Coerce the values to an array.
1309 // Don't simply typecast to array, because the values
1310 // might be Zend_Db_Expr objects.
1311 if (!is_array($keyValues)) {
1312 $keyValues = array($keyValues);
1313 }
1314 if ($numberTerms == 0) {
1315 $numberTerms = $keyValuesCount;
1316 } else if ($keyValuesCount != $numberTerms) {
1317 require_once 'Zend/Db/Table/Exception.php';
1318 throw new Zend_Db_Table_Exception("Missing value(s) for the primary key");
1319 }
1320 $keyValues = array_values($keyValues);
1321 for ($i = 0; $i < $keyValuesCount; ++$i) {
1322 if (!isset($whereList[$i])) {
1323 $whereList[$i] = array();
1324 }
1325 $whereList[$i][$keyPosition] = $keyValues[$i];
1326 }
1327 }
1328
1329 $whereClause = null;
1330 if (count($whereList)) {
1331 $whereOrTerms = array();
1332 $tableName = $this->_db->quoteTableAs($this->_name, null, true);
1333 foreach ($whereList as $keyValueSets) {
1334 $whereAndTerms = array();
1335 foreach ($keyValueSets as $keyPosition => $keyValue) {
1336 $type = $this->_metadata[$keyNames[$keyPosition]]['DATA_TYPE'];
1337 $columnName = $this->_db->quoteIdentifier($keyNames[$keyPosition], true);
1338 $whereAndTerms[] = $this->_db->quoteInto(
1339 $tableName . '.' . $columnName . ' = ?',
1340 $keyValue, $type);
1341 }
1342 $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')';
1343 }
1344 $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')';
1345 }
1346
1347 // issue ZF-5775 (empty where clause should return empty rowset)
1348 if ($whereClause == null) {
1349 $rowsetClass = $this->getRowsetClass();
1350 if (!class_exists($rowsetClass)) {
1351 require_once 'Zend/Loader.php';
1352 Zend_Loader::loadClass($rowsetClass);
1353 }
1354 return new $rowsetClass(array('table' => $this, 'rowClass' => $this->getRowClass(), 'stored' => true));
1355 }
1356
1357 return $this->fetchAll($whereClause);
1358 }
1359
1360 /**
1361 * Fetches all rows.
1362 *
1363 * Honors the Zend_Db_Adapter fetch mode.
1364 *
1365 * @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
1366 * @param string|array $order OPTIONAL An SQL ORDER clause.
1367 * @param int $count OPTIONAL An SQL LIMIT count.
1368 * @param int $offset OPTIONAL An SQL LIMIT offset.
1369 * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
1370 */
1371 public function fetchAll($where = null, $order = null, $count = null, $offset = null)
1372 {
1373 if (!($where instanceof Zend_Db_Table_Select)) {
1374 $select = $this->select();
1375
1376 if ($where !== null) {
1377 $this->_where($select, $where);
1378 }
1379
1380 if ($order !== null) {
1381 $this->_order($select, $order);
1382 }
1383
1384 if ($count !== null || $offset !== null) {
1385 $select->limit($count, $offset);
1386 }
1387
1388 } else {
1389 $select = $where;
1390 }
1391
1392 $rows = $this->_fetch($select);
1393
1394 $data = array(
1395 'table' => $this,
1396 'data' => $rows,
1397 'readOnly' => $select->isReadOnly(),
1398 'rowClass' => $this->getRowClass(),
1399 'stored' => true
1400 );
1401
1402 $rowsetClass = $this->getRowsetClass();
1403 if (!class_exists($rowsetClass)) {
1404 require_once 'Zend/Loader.php';
1405 Zend_Loader::loadClass($rowsetClass);
1406 }
1407 return new $rowsetClass($data);
1408 }
1409
1410 /**
1411 * Fetches one row in an object of type Zend_Db_Table_Row_Abstract,
1412 * or returns null if no row matches the specified criteria.
1413 *
1414 * @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
1415 * @param string|array $order OPTIONAL An SQL ORDER clause.
1416 * @param int $offset OPTIONAL An SQL OFFSET value.
1417 * @return Zend_Db_Table_Row_Abstract|null The row results per the
1418 * Zend_Db_Adapter fetch mode, or null if no row found.
1419 */
1420 public function fetchRow($where = null, $order = null, $offset = null)
1421 {
1422 if (!($where instanceof Zend_Db_Table_Select)) {
1423 $select = $this->select();
1424
1425 if ($where !== null) {
1426 $this->_where($select, $where);
1427 }
1428
1429 if ($order !== null) {
1430 $this->_order($select, $order);
1431 }
1432
1433 $select->limit(1, ((is_numeric($offset)) ? (int) $offset : null));
1434
1435 } else {
1436 $select = $where->limit(1, $where->getPart(Zend_Db_Select::LIMIT_OFFSET));
1437 }
1438
1439 $rows = $this->_fetch($select);
1440
1441 if (count($rows) == 0) {
1442 return null;
1443 }
1444
1445 $data = array(
1446 'table' => $this,
1447 'data' => $rows[0],
1448 'readOnly' => $select->isReadOnly(),
1449 'stored' => true
1450 );
1451
1452 $rowClass = $this->getRowClass();
1453 if (!class_exists($rowClass)) {
1454 require_once 'Zend/Loader.php';
1455 Zend_Loader::loadClass($rowClass);
1456 }
1457 return new $rowClass($data);
1458 }
1459
1460 /**
1461 * Fetches a new blank row (not from the database).
1462 *
1463 * @return Zend_Db_Table_Row_Abstract
1464 * @deprecated since 0.9.3 - use createRow() instead.
1465 */
1466 public function fetchNew()
1467 {
1468 return $this->createRow();
1469 }
1470
1471 /**
1472 * Fetches a new blank row (not from the database).
1473 *
1474 * @param array $data OPTIONAL data to populate in the new row.
1475 * @param string $defaultSource OPTIONAL flag to force default values into new row
1476 * @return Zend_Db_Table_Row_Abstract
1477 */
1478 public function createRow(array $data = array(), $defaultSource = null)
1479 {
1480 $cols = $this->_getCols();
1481 $defaults = array_combine($cols, array_fill(0, count($cols), null));
1482
1483 // nothing provided at call-time, take the class value
1484 if ($defaultSource == null) {
1485 $defaultSource = $this->_defaultSource;
1486 }
1487
1488 if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
1489 $defaultSource = self::DEFAULT_NONE;
1490 }
1491
1492 if ($defaultSource == self::DEFAULT_DB) {
1493 foreach ($this->_metadata as $metadataName => $metadata) {
1494 if (($metadata['DEFAULT'] != null) &&
1495 ($metadata['NULLABLE'] !== true || ($metadata['NULLABLE'] === true && isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] === true)) &&
1496 (!(isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] === false))) {
1497 $defaults[$metadataName] = $metadata['DEFAULT'];
1498 }
1499 }
1500 } elseif ($defaultSource == self::DEFAULT_CLASS && $this->_defaultValues) {
1501 foreach ($this->_defaultValues as $defaultName => $defaultValue) {
1502 if (array_key_exists($defaultName, $defaults)) {
1503 $defaults[$defaultName] = $defaultValue;
1504 }
1505 }
1506 }
1507
1508 $config = array(
1509 'table' => $this,
1510 'data' => $defaults,
1511 'readOnly' => false,
1512 'stored' => false
1513 );
1514
1515 $rowClass = $this->getRowClass();
1516 if (!class_exists($rowClass)) {
1517 require_once 'Zend/Loader.php';
1518 Zend_Loader::loadClass($rowClass);
1519 }
1520 $row = new $rowClass($config);
1521 $row->setFromArray($data);
1522 return $row;
1523 }
1524
1525 /**
1526 * Generate WHERE clause from user-supplied string or array
1527 *
1528 * @param string|array $where OPTIONAL An SQL WHERE clause.
1529 * @return Zend_Db_Table_Select
1530 */
1531 protected function _where(Zend_Db_Table_Select $select, $where)
1532 {
1533 $where = (array) $where;
1534
1535 foreach ($where as $key => $val) {
1536 // is $key an int?
1537 if (is_int($key)) {
1538 // $val is the full condition
1539 $select->where($val);
1540 } else {
1541 // $key is the condition with placeholder,
1542 // and $val is quoted into the condition
1543 $select->where($key, $val);
1544 }
1545 }
1546
1547 return $select;
1548 }
1549
1550 /**
1551 * Generate ORDER clause from user-supplied string or array
1552 *
1553 * @param string|array $order OPTIONAL An SQL ORDER clause.
1554 * @return Zend_Db_Table_Select
1555 */
1556 protected function _order(Zend_Db_Table_Select $select, $order)
1557 {
1558 if (!is_array($order)) {
1559 $order = array($order);
1560 }
1561
1562 foreach ($order as $val) {
1563 $select->order($val);
1564 }
1565
1566 return $select;
1567 }
1568
1569 /**
1570 * Support method for fetching rows.
1571 *
1572 * @param Zend_Db_Table_Select $select query options.
1573 * @return array An array containing the row results in FETCH_ASSOC mode.
1574 */
1575 protected function _fetch(Zend_Db_Table_Select $select)
1576 {
1577 $stmt = $this->_db->query($select);
1578 $data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
1579 return $data;
1580 }
1581
1582 /**
1583 * Get table gateway object from string
1584 *
1585 * @param string $tableName
1586 * @param Zend_Db_Table_Abstract $referenceTable
1587 * @throws Zend_Db_Table_Row_Exception
1588 * @return Zend_Db_Table_Abstract
1589 */
1590 public static function getTableFromString($tableName, Zend_Db_Table_Abstract $referenceTable = null)
1591 {
1592 if ($referenceTable instanceof Zend_Db_Table_Abstract) {
1593 $tableDefinition = $referenceTable->getDefinition();
1594
1595 if ($tableDefinition !== null && $tableDefinition->hasTableConfig($tableName)) {
1596 return new Zend_Db_Table($tableName, $tableDefinition);
1597 }
1598 }
1599
1600 // assume the tableName is the class name
1601 if (!class_exists($tableName)) {
1602 try {
1603 require_once 'Zend/Loader.php';
1604 Zend_Loader::loadClass($tableName);
1605 } catch (Zend_Exception $e) {
1606 require_once 'Zend/Db/Table/Row/Exception.php';
1607 throw new Zend_Db_Table_Row_Exception($e->getMessage(), $e->getCode(), $e);
1608 }
1609 }
1610
1611 $options = array();
1612
1613 if ($referenceTable instanceof Zend_Db_Table_Abstract) {
1614 $options['db'] = $referenceTable->getAdapter();
1615 }
1616
1617 if (isset($tableDefinition) && $tableDefinition !== null) {
1618 $options[Zend_Db_Table_Abstract::DEFINITION] = $tableDefinition;
1619 }
1620
1621 return new $tableName($options);
1622 }
1623
1624 }
1625
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Db/Table/Abstract.php
Zend_Db_Table_Abstract->_fetch(Zend_Db_Table_Select)
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
21 */
22
23 /**
24 * @see Zend_Db_Adapter_Abstract
25 */
26 require_once 'Zend/Db/Adapter/Abstract.php';
27
28 /**
29 * @see Zend_Db_Adapter_Abstract
30 */
31 require_once 'Zend/Db/Select.php';
32
33 /**
34 * @see Zend_Db
35 */
36 require_once 'Zend/Db.php';
37
38 /**
39 * Class for SQL table interface.
40 *
41 * @category Zend
42 * @package Zend_Db
43 * @subpackage Table
44 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
45 * @license http://framework.zend.com/license/new-bsd New BSD License
46 */
47 abstract class Zend_Db_Table_Abstract
48 {
49
50 const ADAPTER = 'db';
51 const DEFINITION = 'definition';
52 const DEFINITION_CONFIG_NAME = 'definitionConfigName';
53 const SCHEMA = 'schema';
54 const NAME = 'name';
55 const PRIMARY = 'primary';
56 const COLS = 'cols';
57 const METADATA = 'metadata';
58 const METADATA_CACHE = 'metadataCache';
59 const METADATA_CACHE_IN_CLASS = 'metadataCacheInClass';
60 const ROW_CLASS = 'rowClass';
61 const ROWSET_CLASS = 'rowsetClass';
62 const REFERENCE_MAP = 'referenceMap';
63 const DEPENDENT_TABLES = 'dependentTables';
64 const SEQUENCE = 'sequence';
65
66 const COLUMNS = 'columns';
67 const REF_TABLE_CLASS = 'refTableClass';
68 const REF_COLUMNS = 'refColumns';
69 const ON_DELETE = 'onDelete';
70 const ON_UPDATE = 'onUpdate';
71
72 const CASCADE = 'cascade';
73 const CASCADE_RECURSE = 'cascadeRecurse';
74 const RESTRICT = 'restrict';
75 const SET_NULL = 'setNull';
76
77 const DEFAULT_NONE = 'defaultNone';
78 const DEFAULT_CLASS = 'defaultClass';
79 const DEFAULT_DB = 'defaultDb';
80
81 const SELECT_WITH_FROM_PART = true;
82 const SELECT_WITHOUT_FROM_PART = false;
83
84 /**
85 * Default Zend_Db_Adapter_Abstract object.
86 *
87 * @var Zend_Db_Adapter_Abstract
88 */
89 protected static $_defaultDb;
90
91 /**
92 * Optional Zend_Db_Table_Definition object
93 *
94 * @var unknown_type
95 */
96 protected $_definition = null;
97
98 /**
99 * Optional definition config name used in concrete implementation
100 *
101 * @var string
102 */
103 protected $_definitionConfigName = null;
104
105 /**
106 * Default cache for information provided by the adapter's describeTable() method.
107 *
108 * @var Zend_Cache_Core
109 */
110 protected static $_defaultMetadataCache = null;
111
112 /**
113 * Zend_Db_Adapter_Abstract object.
114 *
115 * @var Zend_Db_Adapter_Abstract
116 */
117 protected $_db;
118
119 /**
120 * The schema name (default null means current schema)
121 *
122 * @var array
123 */
124 protected $_schema = null;
125
126 /**
127 * The table name.
128 *
129 * @var string
130 */
131 protected $_name = null;
132
133 /**
134 * The table column names derived from Zend_Db_Adapter_Abstract::describeTable().
135 *
136 * @var array
137 */
138 protected $_cols;
139
140 /**
141 * The primary key column or columns.
142 * A compound key should be declared as an array.
143 * You may declare a single-column primary key
144 * as a string.
145 *
146 * @var mixed
147 */
148 protected $_primary = null;
149
150 /**
151 * If your primary key is a compound key, and one of the columns uses
152 * an auto-increment or sequence-generated value, set _identity
153 * to the ordinal index in the $_primary array for that column.
154 * Note this index is the position of the column in the primary key,
155 * not the position of the column in the table. The primary key
156 * array is 1-based.
157 *
158 * @var integer
159 */
160 protected $_identity = 1;
161
162 /**
163 * Define the logic for new values in the primary key.
164 * May be a string, boolean true, or boolean false.
165 *
166 * @var mixed
167 */
168 protected $_sequence = true;
169
170 /**
171 * Information provided by the adapter's describeTable() method.
172 *
173 * @var array
174 */
175 protected $_metadata = array();
176
177 /**
178 * Cache for information provided by the adapter's describeTable() method.
179 *
180 * @var Zend_Cache_Core
181 */
182 protected $_metadataCache = null;
183
184 /**
185 * Flag: whether or not to cache metadata in the class
186 * @var bool
187 */
188 protected $_metadataCacheInClass = true;
189
190 /**
191 * Classname for row
192 *
193 * @var string
194 */
195 protected $_rowClass = 'Zend_Db_Table_Row';
196
197 /**
198 * Classname for rowset
199 *
200 * @var string
201 */
202 protected $_rowsetClass = 'Zend_Db_Table_Rowset';
203
204 /**
205 * Associative array map of declarative referential integrity rules.
206 * This array has one entry per foreign key in the current table.
207 * Each key is a mnemonic name for one reference rule.
208 *
209 * Each value is also an associative array, with the following keys:
210 * - columns = array of names of column(s) in the child table.
211 * - refTableClass = class name of the parent table.
212 * - refColumns = array of names of column(s) in the parent table,
213 * in the same order as those in the 'columns' entry.
214 * - onDelete = "cascade" means that a delete in the parent table also
215 * causes a delete of referencing rows in the child table.
216 * - onUpdate = "cascade" means that an update of primary key values in
217 * the parent table also causes an update of referencing
218 * rows in the child table.
219 *
220 * @var array
221 */
222 protected $_referenceMap = array();
223
224 /**
225 * Simple array of class names of tables that are "children" of the current
226 * table, in other words tables that contain a foreign key to this one.
227 * Array elements are not table names; they are class names of classes that
228 * extend Zend_Db_Table_Abstract.
229 *
230 * @var array
231 */
232 protected $_dependentTables = array();
233
234
235 protected $_defaultSource = self::DEFAULT_NONE;
236 protected $_defaultValues = array();
237
238 /**
239 * Constructor.
240 *
241 * Supported params for $config are:
242 * - db = user-supplied instance of database connector,
243 * or key name of registry instance.
244 * - name = table name.
245 * - primary = string or array of primary key(s).
246 * - rowClass = row class name.
247 * - rowsetClass = rowset class name.
248 * - referenceMap = array structure to declare relationship
249 * to parent tables.
250 * - dependentTables = array of child tables.
251 * - metadataCache = cache for information from adapter describeTable().
252 *
253 * @param mixed $config Array of user-specified config options, or just the Db Adapter.
254 * @return void
255 */
256 public function __construct($config = array())
257 {
258 /**
259 * Allow a scalar argument to be the Adapter object or Registry key.
260 */
261 if (!is_array($config)) {
262 $config = array(self::ADAPTER => $config);
263 }
264
265 if ($config) {
266 $this->setOptions($config);
267 }
268
269 $this->_setup();
270 $this->init();
271 }
272
273 /**
274 * setOptions()
275 *
276 * @param array $options
277 * @return Zend_Db_Table_Abstract
278 */
279 public function setOptions(Array $options)
280 {
281 foreach ($options as $key => $value) {
282 switch ($key) {
283 case self::ADAPTER:
284 $this->_setAdapter($value);
285 break;
286 case self::DEFINITION:
287 $this->setDefinition($value);
288 break;
289 case self::DEFINITION_CONFIG_NAME:
290 $this->setDefinitionConfigName($value);
291 break;
292 case self::SCHEMA:
293 $this->_schema = (string) $value;
294 break;
295 case self::NAME:
296 $this->_name = (string) $value;
297 break;
298 case self::PRIMARY:
299 $this->_primary = (array) $value;
300 break;
301 case self::ROW_CLASS:
302 $this->setRowClass($value);
303 break;
304 case self::ROWSET_CLASS:
305 $this->setRowsetClass($value);
306 break;
307 case self::REFERENCE_MAP:
308 $this->setReferences($value);
309 break;
310 case self::DEPENDENT_TABLES:
311 $this->setDependentTables($value);
312 break;
313 case self::METADATA_CACHE:
314 $this->_setMetadataCache($value);
315 break;
316 case self::METADATA_CACHE_IN_CLASS:
317 $this->setMetadataCacheInClass($value);
318 break;
319 case self::SEQUENCE:
320 $this->_setSequence($value);
321 break;
322 default:
323 // ignore unrecognized configuration directive
324 break;
325 }
326 }
327
328 return $this;
329 }
330
331 /**
332 * setDefinition()
333 *
334 * @param Zend_Db_Table_Definition $definition
335 * @return Zend_Db_Table_Abstract
336 */
337 public function setDefinition(Zend_Db_Table_Definition $definition)
338 {
339 $this->_definition = $definition;
340 return $this;
341 }
342
343 /**
344 * getDefinition()
345 *
346 * @return Zend_Db_Table_Definition|null
347 */
348 public function getDefinition()
349 {
350 return $this->_definition;
351 }
352
353 /**
354 * setDefinitionConfigName()
355 *
356 * @param string $definition
357 * @return Zend_Db_Table_Abstract
358 */
359 public function setDefinitionConfigName($definitionConfigName)
360 {
361 $this->_definitionConfigName = $definitionConfigName;
362 return $this;
363 }
364
365 /**
366 * getDefinitionConfigName()
367 *
368 * @return string
369 */
370 public function getDefinitionConfigName()
371 {
372 return $this->_definitionConfigName;
373 }
374
375 /**
376 * @param string $classname
377 * @return Zend_Db_Table_Abstract Provides a fluent interface
378 */
379 public function setRowClass($classname)
380 {
381 $this->_rowClass = (string) $classname;
382
383 return $this;
384 }
385
386 /**
387 * @return string
388 */
389 public function getRowClass()
390 {
391 return $this->_rowClass;
392 }
393
394 /**
395 * @param string $classname
396 * @return Zend_Db_Table_Abstract Provides a fluent interface
397 */
398 public function setRowsetClass($classname)
399 {
400 $this->_rowsetClass = (string) $classname;
401
402 return $this;
403 }
404
405 /**
406 * @return string
407 */
408 public function getRowsetClass()
409 {
410 return $this->_rowsetClass;
411 }
412
413 /**
414 * Add a reference to the reference map
415 *
416 * @param string $ruleKey
417 * @param string|array $columns
418 * @param string $refTableClass
419 * @param string|array $refColumns
420 * @param string $onDelete
421 * @param string $onUpdate
422 * @return Zend_Db_Table_Abstract
423 */
424 public function addReference($ruleKey, $columns, $refTableClass, $refColumns,
425 $onDelete = null, $onUpdate = null)
426 {
427 $reference = array(self::COLUMNS => (array) $columns,
428 self::REF_TABLE_CLASS => $refTableClass,
429 self::REF_COLUMNS => (array) $refColumns);
430
431 if (!empty($onDelete)) {
432 $reference[self::ON_DELETE] = $onDelete;
433 }
434
435 if (!empty($onUpdate)) {
436 $reference[self::ON_UPDATE] = $onUpdate;
437 }
438
439 $this->_referenceMap[$ruleKey] = $reference;
440
441 return $this;
442 }
443
444 /**
445 * @param array $referenceMap
446 * @return Zend_Db_Table_Abstract Provides a fluent interface
447 */
448 public function setReferences(array $referenceMap)
449 {
450 $this->_referenceMap = $referenceMap;
451
452 return $this;
453 }
454
455 /**
456 * @param string $tableClassname
457 * @param string $ruleKey OPTIONAL
458 * @return array
459 * @throws Zend_Db_Table_Exception
460 */
461 public function getReference($tableClassname, $ruleKey = null)
462 {
463 $thisClass = get_class($this);
464 if ($thisClass === 'Zend_Db_Table') {
465 $thisClass = $this->_definitionConfigName;
466 }
467 $refMap = $this->_getReferenceMapNormalized();
468 if ($ruleKey !== null) {
469 if (!isset($refMap[$ruleKey])) {
470 require_once "Zend/Db/Table/Exception.php";
471 throw new Zend_Db_Table_Exception("No reference rule \"$ruleKey\" from table $thisClass to table $tableClassname");
472 }
473 if ($refMap[$ruleKey][self::REF_TABLE_CLASS] != $tableClassname) {
474 require_once "Zend/Db/Table/Exception.php";
475 throw new Zend_Db_Table_Exception("Reference rule \"$ruleKey\" does not reference table $tableClassname");
476 }
477 return $refMap[$ruleKey];
478 }
479 foreach ($refMap as $reference) {
480 if ($reference[self::REF_TABLE_CLASS] == $tableClassname) {
481 return $reference;
482 }
483 }
484 require_once "Zend/Db/Table/Exception.php";
485 throw new Zend_Db_Table_Exception("No reference from table $thisClass to table $tableClassname");
486 }
487
488 /**
489 * @param array $dependentTables
490 * @return Zend_Db_Table_Abstract Provides a fluent interface
491 */
492 public function setDependentTables(array $dependentTables)
493 {
494 $this->_dependentTables = $dependentTables;
495
496 return $this;
497 }
498
499 /**
500 * @return array
501 */
502 public function getDependentTables()
503 {
504 return $this->_dependentTables;
505 }
506
507 /**
508 * set the defaultSource property - this tells the table class where to find default values
509 *
510 * @param string $defaultSource
511 * @return Zend_Db_Table_Abstract
512 */
513 public function setDefaultSource($defaultSource = self::DEFAULT_NONE)
514 {
515 if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
516 $defaultSource = self::DEFAULT_NONE;
517 }
518
519 $this->_defaultSource = $defaultSource;
520 return $this;
521 }
522
523 /**
524 * returns the default source flag that determines where defaultSources come from
525 *
526 * @return unknown
527 */
528 public function getDefaultSource()
529 {
530 return $this->_defaultSource;
531 }
532
533 /**
534 * set the default values for the table class
535 *
536 * @param array $defaultValues
537 * @return Zend_Db_Table_Abstract
538 */
539 public function setDefaultValues(Array $defaultValues)
540 {
541 foreach ($defaultValues as $defaultName => $defaultValue) {
542 if (array_key_exists($defaultName, $this->_metadata)) {
543 $this->_defaultValues[$defaultName] = $defaultValue;
544 }
545 }
546 return $this;
547 }
548
549 public function getDefaultValues()
550 {
551 return $this->_defaultValues;
552 }
553
554
555 /**
556 * Sets the default Zend_Db_Adapter_Abstract for all Zend_Db_Table objects.
557 *
558 * @param mixed $db Either an Adapter object, or a string naming a Registry key
559 * @return void
560 */
561 public static function setDefaultAdapter($db = null)
562 {
563 self::$_defaultDb = self::_setupAdapter($db);
564 }
565
566 /**
567 * Gets the default Zend_Db_Adapter_Abstract for all Zend_Db_Table objects.
568 *
569 * @return Zend_Db_Adapter_Abstract or null
570 */
571 public static function getDefaultAdapter()
572 {
573 return self::$_defaultDb;
574 }
575
576 /**
577 * @param mixed $db Either an Adapter object, or a string naming a Registry key
578 * @return Zend_Db_Table_Abstract Provides a fluent interface
579 */
580 protected function _setAdapter($db)
581 {
582 $this->_db = self::_setupAdapter($db);
583 return $this;
584 }
585
586 /**
587 * Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Table object.
588 *
589 * @return Zend_Db_Adapter_Abstract
590 */
591 public function getAdapter()
592 {
593 return $this->_db;
594 }
595
596 /**
597 * @param mixed $db Either an Adapter object, or a string naming a Registry key
598 * @return Zend_Db_Adapter_Abstract
599 * @throws Zend_Db_Table_Exception
600 */
601 protected static function _setupAdapter($db)
602 {
603 if ($db === null) {
604 return null;
605 }
606 if (is_string($db)) {
607 require_once 'Zend/Registry.php';
608 $db = Zend_Registry::get($db);
609 }
610 if (!$db instanceof Zend_Db_Adapter_Abstract) {
611 require_once 'Zend/Db/Table/Exception.php';
612 throw new Zend_Db_Table_Exception('Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored');
613 }
614 return $db;
615 }
616
617 /**
618 * Sets the default metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
619 *
620 * If $defaultMetadataCache is null, then no metadata cache is used by default.
621 *
622 * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
623 * @return void
624 */
625 public static function setDefaultMetadataCache($metadataCache = null)
626 {
627 self::$_defaultMetadataCache = self::_setupMetadataCache($metadataCache);
628 }
629
630 /**
631 * Gets the default metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
632 *
633 * @return Zend_Cache_Core or null
634 */
635 public static function getDefaultMetadataCache()
636 {
637 return self::$_defaultMetadataCache;
638 }
639
640 /**
641 * Sets the metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
642 *
643 * If $metadataCache is null, then no metadata cache is used. Since there is no opportunity to reload metadata
644 * after instantiation, this method need not be public, particularly because that it would have no effect
645 * results in unnecessary API complexity. To configure the metadata cache, use the metadataCache configuration
646 * option for the class constructor upon instantiation.
647 *
648 * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
649 * @return Zend_Db_Table_Abstract Provides a fluent interface
650 */
651 protected function _setMetadataCache($metadataCache)
652 {
653 $this->_metadataCache = self::_setupMetadataCache($metadataCache);
654 return $this;
655 }
656
657 /**
658 * Gets the metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
659 *
660 * @return Zend_Cache_Core or null
661 */
662 public function getMetadataCache()
663 {
664 return $this->_metadataCache;
665 }
666
667 /**
668 * Indicate whether metadata should be cached in the class for the duration
669 * of the instance
670 *
671 * @param bool $flag
672 * @return Zend_Db_Table_Abstract
673 */
674 public function setMetadataCacheInClass($flag)
675 {
676 $this->_metadataCacheInClass = (bool) $flag;
677 return $this;
678 }
679
680 /**
681 * Retrieve flag indicating if metadata should be cached for duration of
682 * instance
683 *
684 * @return bool
685 */
686 public function metadataCacheInClass()
687 {
688 return $this->_metadataCacheInClass;
689 }
690
691 /**
692 * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
693 * @return Zend_Cache_Core
694 * @throws Zend_Db_Table_Exception
695 */
696 protected static function _setupMetadataCache($metadataCache)
697 {
698 if ($metadataCache === null) {
699 return null;
700 }
701 if (is_string($metadataCache)) {
702 require_once 'Zend/Registry.php';
703 $metadataCache = Zend_Registry::get($metadataCache);
704 }
705 if (!$metadataCache instanceof Zend_Cache_Core) {
706 require_once 'Zend/Db/Table/Exception.php';
707 throw new Zend_Db_Table_Exception('Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored');
708 }
709 return $metadataCache;
710 }
711
712 /**
713 * Sets the sequence member, which defines the behavior for generating
714 * primary key values in new rows.
715 * - If this is a string, then the string names the sequence object.
716 * - If this is boolean true, then the key uses an auto-incrementing
717 * or identity mechanism.
718 * - If this is boolean false, then the key is user-defined.
719 * Use this for natural keys, for example.
720 *
721 * @param mixed $sequence
722 * @return Zend_Db_Table_Adapter_Abstract Provides a fluent interface
723 */
724 protected function _setSequence($sequence)
725 {
726 $this->_sequence = $sequence;
727
728 return $this;
729 }
730
731 /**
732 * Turnkey for initialization of a table object.
733 * Calls other protected methods for individual tasks, to make it easier
734 * for a subclass to override part of the setup logic.
735 *
736 * @return void
737 */
738 protected function _setup()
739 {
740 $this->_setupDatabaseAdapter();
741 $this->_setupTableName();
742 }
743
744 /**
745 * Initialize database adapter.
746 *
747 * @return void
748 * @throws Zend_Db_Table_Exception
749 */
750 protected function _setupDatabaseAdapter()
751 {
752 if (! $this->_db) {
753 $this->_db = self::getDefaultAdapter();
754 if (!$this->_db instanceof Zend_Db_Adapter_Abstract) {
755 require_once 'Zend/Db/Table/Exception.php';
756 throw new Zend_Db_Table_Exception('No adapter found for ' . get_class($this));
757 }
758 }
759 }
760
761 /**
762 * Initialize table and schema names.
763 *
764 * If the table name is not set in the class definition,
765 * use the class name itself as the table name.
766 *
767 * A schema name provided with the table name (e.g., "schema.table") overrides
768 * any existing value for $this->_schema.
769 *
770 * @return void
771 */
772 protected function _setupTableName()
773 {
774 if (! $this->_name) {
775 $this->_name = get_class($this);
776 } else if (strpos($this->_name, '.')) {
777 list($this->_schema, $this->_name) = explode('.', $this->_name);
778 }
779 }
780
781 /**
782 * Initializes metadata.
783 *
784 * If metadata cannot be loaded from cache, adapter's describeTable() method is called to discover metadata
785 * information. Returns true if and only if the metadata are loaded from cache.
786 *
787 * @return boolean
788 * @throws Zend_Db_Table_Exception
789 */
790 protected function _setupMetadata()
791 {
792 if ($this->metadataCacheInClass() && (count($this->_metadata) > 0)) {
793 return true;
794 }
795
796 // Assume that metadata will be loaded from cache
797 $isMetadataFromCache = true;
798
799 // If $this has no metadata cache but the class has a default metadata cache
800 if (null === $this->_metadataCache && null !== self::$_defaultMetadataCache) {
801 // Make $this use the default metadata cache of the class
802 $this->_setMetadataCache(self::$_defaultMetadataCache);
803 }
804
805 // If $this has a metadata cache
806 if (null !== $this->_metadataCache) {
807 // Define the cache identifier where the metadata are saved
808
809 //get db configuration
810 $dbConfig = $this->_db->getConfig();
811
812 $port = isset($dbConfig['options']['port'])
813 ? ':'.$dbConfig['options']['port']
814 : (isset($dbConfig['port'])
815 ? ':'.$dbConfig['port']
816 : null);
817
818 $host = isset($dbConfig['options']['host'])
819 ? ':'.$dbConfig['options']['host']
820 : (isset($dbConfig['host'])
821 ? ':'.$dbConfig['host']
822 : null);
823
824 // Define the cache identifier where the metadata are saved
825 $cacheId = md5( // port:host/dbname:schema.table (based on availabilty)
826 $port . $host . '/'. $dbConfig['dbname'] . ':'
827 . $this->_schema. '.' . $this->_name
828 );
829 }
830
831 // If $this has no metadata cache or metadata cache misses
832 if (null === $this->_metadataCache || !($metadata = $this->_metadataCache->load($cacheId))) {
833 // Metadata are not loaded from cache
834 $isMetadataFromCache = false;
835 // Fetch metadata from the adapter's describeTable() method
836 $metadata = $this->_db->describeTable($this->_name, $this->_schema);
837 // If $this has a metadata cache, then cache the metadata
838 if (null !== $this->_metadataCache && !$this->_metadataCache->save($metadata, $cacheId)) {
839 trigger_error('Failed saving metadata to metadataCache', E_USER_NOTICE);
840 }
841 }
842
843 // Assign the metadata to $this
844 $this->_metadata = $metadata;
845
846 // Return whether the metadata were loaded from cache
847 return $isMetadataFromCache;
848 }
849
850 /**
851 * Retrieve table columns
852 *
853 * @return array
854 */
855 protected function _getCols()
856 {
857 if (null === $this->_cols) {
858 $this->_setupMetadata();
859 $this->_cols = array_keys($this->_metadata);
860 }
861 return $this->_cols;
862 }
863
864 /**
865 * Initialize primary key from metadata.
866 * If $_primary is not defined, discover primary keys
867 * from the information returned by describeTable().
868 *
869 * @return void
870 * @throws Zend_Db_Table_Exception
871 */
872 protected function _setupPrimaryKey()
873 {
874 if (!$this->_primary) {
875 $this->_setupMetadata();
876 $this->_primary = array();
877 foreach ($this->_metadata as $col) {
878 if ($col['PRIMARY']) {
879 $this->_primary[ $col['PRIMARY_POSITION'] ] = $col['COLUMN_NAME'];
880 if ($col['IDENTITY']) {
881 $this->_identity = $col['PRIMARY_POSITION'];
882 }
883 }
884 }
885 // if no primary key was specified and none was found in the metadata
886 // then throw an exception.
887 if (empty($this->_primary)) {
888 require_once 'Zend/Db/Table/Exception.php';
889 throw new Zend_Db_Table_Exception("A table must have a primary key, but none was found for table '{$this->_name}'");
890 }
891 } else if (!is_array($this->_primary)) {
892 $this->_primary = array(1 => $this->_primary);
893 } else if (isset($this->_primary[0])) {
894 array_unshift($this->_primary, null);
895 unset($this->_primary[0]);
896 }
897
898 $cols = $this->_getCols();
899 if (! array_intersect((array) $this->_primary, $cols) == (array) $this->_primary) {
900 require_once 'Zend/Db/Table/Exception.php';
901 throw new Zend_Db_Table_Exception("Primary key column(s) ("
902 . implode(',', (array) $this->_primary)
903 . ") are not columns in this table ("
904 . implode(',', $cols)
905 . ")");
906 }
907
908 $primary = (array) $this->_primary;
909 $pkIdentity = $primary[(int) $this->_identity];
910
911 /**
912 * Special case for PostgreSQL: a SERIAL key implicitly uses a sequence
913 * object whose name is "<table>_<column>_seq".
914 */
915 if ($this->_sequence === true && $this->_db instanceof Zend_Db_Adapter_Pdo_Pgsql) {
916 $this->_sequence = $this->_db->quoteIdentifier("{$this->_name}_{$pkIdentity}_seq");
917 if ($this->_schema) {
918 $this->_sequence = $this->_db->quoteIdentifier($this->_schema) . '.' . $this->_sequence;
919 }
920 }
921 }
922
923 /**
924 * Returns a normalized version of the reference map
925 *
926 * @return array
927 */
928 protected function _getReferenceMapNormalized()
929 {
930 $referenceMapNormalized = array();
931
932 foreach ($this->_referenceMap as $rule => $map) {
933
934 $referenceMapNormalized[$rule] = array();
935
936 foreach ($map as $key => $value) {
937 switch ($key) {
938
939 // normalize COLUMNS and REF_COLUMNS to arrays
940 case self::COLUMNS:
941 case self::REF_COLUMNS:
942 if (!is_array($value)) {
943 $referenceMapNormalized[$rule][$key] = array($value);
944 } else {
945 $referenceMapNormalized[$rule][$key] = $value;
946 }
947 break;
948
949 // other values are copied as-is
950 default:
951 $referenceMapNormalized[$rule][$key] = $value;
952 break;
953 }
954 }
955 }
956
957 return $referenceMapNormalized;
958 }
959
960 /**
961 * Initialize object
962 *
963 * Called from {@link __construct()} as final step of object instantiation.
964 *
965 * @return void
966 */
967 public function init()
968 {
969 }
970
971 /**
972 * Returns table information.
973 *
974 * You can elect to return only a part of this information by supplying its key name,
975 * otherwise all information is returned as an array.
976 *
977 * @param string $key The specific info part to return OPTIONAL
978 * @return mixed
979 * @throws Zend_Db_Table_Exception
980 */
981 public function info($key = null)
982 {
983 $this->_setupPrimaryKey();
984
985 $info = array(
986 self::SCHEMA => $this->_schema,
987 self::NAME => $this->_name,
988 self::COLS => $this->_getCols(),
989 self::PRIMARY => (array) $this->_primary,
990 self::METADATA => $this->_metadata,
991 self::ROW_CLASS => $this->getRowClass(),
992 self::ROWSET_CLASS => $this->getRowsetClass(),
993 self::REFERENCE_MAP => $this->_referenceMap,
994 self::DEPENDENT_TABLES => $this->_dependentTables,
995 self::SEQUENCE => $this->_sequence
996 );
997
998 if ($key === null) {
999 return $info;
1000 }
1001
1002 if (!array_key_exists($key, $info)) {
1003 require_once 'Zend/Db/Table/Exception.php';
1004 throw new Zend_Db_Table_Exception('There is no table information for the key "' . $key . '"');
1005 }
1006
1007 return $info[$key];
1008 }
1009
1010 /**
1011 * Returns an instance of a Zend_Db_Table_Select object.
1012 *
1013 * @param bool $withFromPart Whether or not to include the from part of the select based on the table
1014 * @return Zend_Db_Table_Select
1015 */
1016 public function select($withFromPart = self::SELECT_WITHOUT_FROM_PART)
1017 {
1018 require_once 'Zend/Db/Table/Select.php';
1019 $select = new Zend_Db_Table_Select($this);
1020 if ($withFromPart == self::SELECT_WITH_FROM_PART) {
1021 $select->from($this->info(self::NAME), Zend_Db_Table_Select::SQL_WILDCARD, $this->info(self::SCHEMA));
1022 }
1023 return $select;
1024 }
1025
1026 /**
1027 * Inserts a new row.
1028 *
1029 * @param array $data Column-value pairs.
1030 * @return mixed The primary key of the row inserted.
1031 */
1032 public function insert(array $data)
1033 {
1034 $this->_setupPrimaryKey();
1035
1036 /**
1037 * Zend_Db_Table assumes that if you have a compound primary key
1038 * and one of the columns in the key uses a sequence,
1039 * it's the _first_ column in the compound key.
1040 */
1041 $primary = (array) $this->_primary;
1042 $pkIdentity = $primary[(int)$this->_identity];
1043
1044
1045 /**
1046 * If the primary key can be generated automatically, and no value was
1047 * specified in the user-supplied data, then omit it from the tuple.
1048 *
1049 * Note: this checks for sensible values in the supplied primary key
1050 * position of the data. The following values are considered empty:
1051 * null, false, true, '', array()
1052 */
1053 if (array_key_exists($pkIdentity, $data)) {
1054 if ($data[$pkIdentity] === null // null
1055 || $data[$pkIdentity] === '' // empty string
1056 || is_bool($data[$pkIdentity]) // boolean
1057 || (is_array($data[$pkIdentity]) && empty($data[$pkIdentity]))) { // empty array
1058 unset($data[$pkIdentity]);
1059 }
1060 }
1061
1062 /**
1063 * If this table uses a database sequence object and the data does not
1064 * specify a value, then get the next ID from the sequence and add it
1065 * to the row. We assume that only the first column in a compound
1066 * primary key takes a value from a sequence.
1067 */
1068 if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
1069 $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
1070 }
1071
1072 /**
1073 * INSERT the new row.
1074 */
1075 $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1076 $this->_db->insert($tableSpec, $data);
1077
1078 /**
1079 * Fetch the most recent ID generated by an auto-increment
1080 * or IDENTITY column, unless the user has specified a value,
1081 * overriding the auto-increment mechanism.
1082 */
1083 if ($this->_sequence === true && !isset($data[$pkIdentity])) {
1084 $data[$pkIdentity] = $this->_db->lastInsertId();
1085 }
1086
1087 /**
1088 * Return the primary key value if the PK is a single column,
1089 * else return an associative array of the PK column/value pairs.
1090 */
1091 $pkData = array_intersect_key($data, array_flip($primary));
1092 if (count($primary) == 1) {
1093 reset($pkData);
1094 return current($pkData);
1095 }
1096
1097 return $pkData;
1098 }
1099
1100 /**
1101 * Check if the provided column is an identity of the table
1102 *
1103 * @param string $column
1104 * @throws Zend_Db_Table_Exception
1105 * @return boolean
1106 */
1107 public function isIdentity($column)
1108 {
1109 $this->_setupPrimaryKey();
1110
1111 if (!isset($this->_metadata[$column])) {
1112 /**
1113 * @see Zend_Db_Table_Exception
1114 */
1115 require_once 'Zend/Db/Table/Exception.php';
1116
1117 throw new Zend_Db_Table_Exception('Column "' . $column . '" not found in table.');
1118 }
1119
1120 return (bool) $this->_metadata[$column]['IDENTITY'];
1121 }
1122
1123 /**
1124 * Updates existing rows.
1125 *
1126 * @param array $data Column-value pairs.
1127 * @param array|string $where An SQL WHERE clause, or an array of SQL WHERE clauses.
1128 * @return int The number of rows updated.
1129 */
1130 public function update(array $data, $where)
1131 {
1132 $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1133 return $this->_db->update($tableSpec, $data, $where);
1134 }
1135
1136 /**
1137 * Called by a row object for the parent table's class during save() method.
1138 *
1139 * @param string $parentTableClassname
1140 * @param array $oldPrimaryKey
1141 * @param array $newPrimaryKey
1142 * @return int
1143 */
1144 public function _cascadeUpdate($parentTableClassname, array $oldPrimaryKey, array $newPrimaryKey)
1145 {
1146 $this->_setupMetadata();
1147 $rowsAffected = 0;
1148 foreach ($this->_getReferenceMapNormalized() as $map) {
1149 if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_UPDATE])) {
1150 switch ($map[self::ON_UPDATE]) {
1151 case self::CASCADE:
1152 $newRefs = array();
1153 $where = array();
1154 for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
1155 $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
1156 $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
1157 if (array_key_exists($refCol, $newPrimaryKey)) {
1158 $newRefs[$col] = $newPrimaryKey[$refCol];
1159 }
1160 $type = $this->_metadata[$col]['DATA_TYPE'];
1161 $where[] = $this->_db->quoteInto(
1162 $this->_db->quoteIdentifier($col, true) . ' = ?',
1163 $oldPrimaryKey[$refCol], $type);
1164 }
1165 $rowsAffected += $this->update($newRefs, $where);
1166 break;
1167 default:
1168 // no action
1169 break;
1170 }
1171 }
1172 }
1173 return $rowsAffected;
1174 }
1175
1176 /**
1177 * Deletes existing rows.
1178 *
1179 * @param array|string $where SQL WHERE clause(s).
1180 * @return int The number of rows deleted.
1181 */
1182 public function delete($where)
1183 {
1184 $depTables = $this->getDependentTables();
1185 if (!empty($depTables)) {
1186 $resultSet = $this->fetchAll($where);
1187 if (count($resultSet) > 0 ) {
1188 foreach ($resultSet as $row) {
1189 /**
1190 * Execute cascading deletes against dependent tables
1191 */
1192 foreach ($depTables as $tableClass) {
1193 $t = self::getTableFromString($tableClass, $this);
1194 $t->_cascadeDelete(
1195 get_class($this), $row->getPrimaryKey()
1196 );
1197 }
1198 }
1199 }
1200 }
1201
1202 $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
1203 return $this->_db->delete($tableSpec, $where);
1204 }
1205
1206 /**
1207 * Called by parent table's class during delete() method.
1208 *
1209 * @param string $parentTableClassname
1210 * @param array $primaryKey
1211 * @return int Number of affected rows
1212 */
1213 public function _cascadeDelete($parentTableClassname, array $primaryKey)
1214 {
1215 // setup metadata
1216 $this->_setupMetadata();
1217
1218 // get this class name
1219 $thisClass = get_class($this);
1220 if ($thisClass === 'Zend_Db_Table') {
1221 $thisClass = $this->_definitionConfigName;
1222 }
1223
1224 $rowsAffected = 0;
1225
1226 foreach ($this->_getReferenceMapNormalized() as $map) {
1227 if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {
1228
1229 $where = array();
1230
1231 // CASCADE or CASCADE_RECURSE
1232 if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
1233 for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
1234 $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
1235 $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
1236 $type = $this->_metadata[$col]['DATA_TYPE'];
1237 $where[] = $this->_db->quoteInto(
1238 $this->_db->quoteIdentifier($col, true) . ' = ?',
1239 $primaryKey[$refCol], $type);
1240 }
1241 }
1242
1243 // CASCADE_RECURSE
1244 if ($map[self::ON_DELETE] == self::CASCADE_RECURSE) {
1245
1246 /**
1247 * Execute cascading deletes against dependent tables
1248 */
1249 $depTables = $this->getDependentTables();
1250 if (!empty($depTables)) {
1251 foreach ($depTables as $tableClass) {
1252 $t = self::getTableFromString($tableClass, $this);
1253 foreach ($this->fetchAll($where) as $depRow) {
1254 $rowsAffected += $t->_cascadeDelete($thisClass, $depRow->getPrimaryKey());
1255 }
1256 }
1257 }
1258 }
1259
1260 // CASCADE or CASCADE_RECURSE
1261 if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
1262 $rowsAffected += $this->delete($where);
1263 }
1264
1265 }
1266 }
1267 return $rowsAffected;
1268 }
1269
1270 /**
1271 * Fetches rows by primary key. The argument specifies one or more primary
1272 * key value(s). To find multiple rows by primary key, the argument must
1273 * be an array.
1274 *
1275 * This method accepts a variable number of arguments. If the table has a
1276 * multi-column primary key, the number of arguments must be the same as
1277 * the number of columns in the primary key. To find multiple rows in a
1278 * table with a multi-column primary key, each argument must be an array
1279 * with the same number of elements.
1280 *
1281 * The find() method always returns a Rowset object, even if only one row
1282 * was found.
1283 *
1284 * @param mixed $key The value(s) of the primary keys.
1285 * @return Zend_Db_Table_Rowset_Abstract Row(s) matching the criteria.
1286 * @throws Zend_Db_Table_Exception
1287 */
1288 public function find()
1289 {
1290 $this->_setupPrimaryKey();
1291 $args = func_get_args();
1292 $keyNames = array_values((array) $this->_primary);
1293
1294 if (count($args) < count($keyNames)) {
1295 require_once 'Zend/Db/Table/Exception.php';
1296 throw new Zend_Db_Table_Exception("Too few columns for the primary key");
1297 }
1298
1299 if (count($args) > count($keyNames)) {
1300 require_once 'Zend/Db/Table/Exception.php';
1301 throw new Zend_Db_Table_Exception("Too many columns for the primary key");
1302 }
1303
1304 $whereList = array();
1305 $numberTerms = 0;
1306 foreach ($args as $keyPosition => $keyValues) {
1307 $keyValuesCount = count($keyValues);
1308 // Coerce the values to an array.
1309 // Don't simply typecast to array, because the values
1310 // might be Zend_Db_Expr objects.
1311 if (!is_array($keyValues)) {
1312 $keyValues = array($keyValues);
1313 }
1314 if ($numberTerms == 0) {
1315 $numberTerms = $keyValuesCount;
1316 } else if ($keyValuesCount != $numberTerms) {
1317 require_once 'Zend/Db/Table/Exception.php';
1318 throw new Zend_Db_Table_Exception("Missing value(s) for the primary key");
1319 }
1320 $keyValues = array_values($keyValues);
1321 for ($i = 0; $i < $keyValuesCount; ++$i) {
1322 if (!isset($whereList[$i])) {
1323 $whereList[$i] = array();
1324 }
1325 $whereList[$i][$keyPosition] = $keyValues[$i];
1326 }
1327 }
1328
1329 $whereClause = null;
1330 if (count($whereList)) {
1331 $whereOrTerms = array();
1332 $tableName = $this->_db->quoteTableAs($this->_name, null, true);
1333 foreach ($whereList as $keyValueSets) {
1334 $whereAndTerms = array();
1335 foreach ($keyValueSets as $keyPosition => $keyValue) {
1336 $type = $this->_metadata[$keyNames[$keyPosition]]['DATA_TYPE'];
1337 $columnName = $this->_db->quoteIdentifier($keyNames[$keyPosition], true);
1338 $whereAndTerms[] = $this->_db->quoteInto(
1339 $tableName . '.' . $columnName . ' = ?',
1340 $keyValue, $type);
1341 }
1342 $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')';
1343 }
1344 $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')';
1345 }
1346
1347 // issue ZF-5775 (empty where clause should return empty rowset)
1348 if ($whereClause == null) {
1349 $rowsetClass = $this->getRowsetClass();
1350 if (!class_exists($rowsetClass)) {
1351 require_once 'Zend/Loader.php';
1352 Zend_Loader::loadClass($rowsetClass);
1353 }
1354 return new $rowsetClass(array('table' => $this, 'rowClass' => $this->getRowClass(), 'stored' => true));
1355 }
1356
1357 return $this->fetchAll($whereClause);
1358 }
1359
1360 /**
1361 * Fetches all rows.
1362 *
1363 * Honors the Zend_Db_Adapter fetch mode.
1364 *
1365 * @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
1366 * @param string|array $order OPTIONAL An SQL ORDER clause.
1367 * @param int $count OPTIONAL An SQL LIMIT count.
1368 * @param int $offset OPTIONAL An SQL LIMIT offset.
1369 * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
1370 */
1371 public function fetchAll($where = null, $order = null, $count = null, $offset = null)
1372 {
1373 if (!($where instanceof Zend_Db_Table_Select)) {
1374 $select = $this->select();
1375
1376 if ($where !== null) {
1377 $this->_where($select, $where);
1378 }
1379
1380 if ($order !== null) {
1381 $this->_order($select, $order);
1382 }
1383
1384 if ($count !== null || $offset !== null) {
1385 $select->limit($count, $offset);
1386 }
1387
1388 } else {
1389 $select = $where;
1390 }
1391
1392 $rows = $this->_fetch($select);
1393
1394 $data = array(
1395 'table' => $this,
1396 'data' => $rows,
1397 'readOnly' => $select->isReadOnly(),
1398 'rowClass' => $this->getRowClass(),
1399 'stored' => true
1400 );
1401
1402 $rowsetClass = $this->getRowsetClass();
1403 if (!class_exists($rowsetClass)) {
1404 require_once 'Zend/Loader.php';
1405 Zend_Loader::loadClass($rowsetClass);
1406 }
1407 return new $rowsetClass($data);
1408 }
1409
1410 /**
1411 * Fetches one row in an object of type Zend_Db_Table_Row_Abstract,
1412 * or returns null if no row matches the specified criteria.
1413 *
1414 * @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
1415 * @param string|array $order OPTIONAL An SQL ORDER clause.
1416 * @param int $offset OPTIONAL An SQL OFFSET value.
1417 * @return Zend_Db_Table_Row_Abstract|null The row results per the
1418 * Zend_Db_Adapter fetch mode, or null if no row found.
1419 */
1420 public function fetchRow($where = null, $order = null, $offset = null)
1421 {
1422 if (!($where instanceof Zend_Db_Table_Select)) {
1423 $select = $this->select();
1424
1425 if ($where !== null) {
1426 $this->_where($select, $where);
1427 }
1428
1429 if ($order !== null) {
1430 $this->_order($select, $order);
1431 }
1432
1433 $select->limit(1, ((is_numeric($offset)) ? (int) $offset : null));
1434
1435 } else {
1436 $select = $where->limit(1, $where->getPart(Zend_Db_Select::LIMIT_OFFSET));
1437 }
1438
1439 $rows = $this->_fetch($select);
1440
1441 if (count($rows) == 0) {
1442 return null;
1443 }
1444
1445 $data = array(
1446 'table' => $this,
1447 'data' => $rows[0],
1448 'readOnly' => $select->isReadOnly(),
1449 'stored' => true
1450 );
1451
1452 $rowClass = $this->getRowClass();
1453 if (!class_exists($rowClass)) {
1454 require_once 'Zend/Loader.php';
1455 Zend_Loader::loadClass($rowClass);
1456 }
1457 return new $rowClass($data);
1458 }
1459
1460 /**
1461 * Fetches a new blank row (not from the database).
1462 *
1463 * @return Zend_Db_Table_Row_Abstract
1464 * @deprecated since 0.9.3 - use createRow() instead.
1465 */
1466 public function fetchNew()
1467 {
1468 return $this->createRow();
1469 }
1470
1471 /**
1472 * Fetches a new blank row (not from the database).
1473 *
1474 * @param array $data OPTIONAL data to populate in the new row.
1475 * @param string $defaultSource OPTIONAL flag to force default values into new row
1476 * @return Zend_Db_Table_Row_Abstract
1477 */
1478 public function createRow(array $data = array(), $defaultSource = null)
1479 {
1480 $cols = $this->_getCols();
1481 $defaults = array_combine($cols, array_fill(0, count($cols), null));
1482
1483 // nothing provided at call-time, take the class value
1484 if ($defaultSource == null) {
1485 $defaultSource = $this->_defaultSource;
1486 }
1487
1488 if (!in_array($defaultSource, array(self::DEFAULT_CLASS, self::DEFAULT_DB, self::DEFAULT_NONE))) {
1489 $defaultSource = self::DEFAULT_NONE;
1490 }
1491
1492 if ($defaultSource == self::DEFAULT_DB) {
1493 foreach ($this->_metadata as $metadataName => $metadata) {
1494 if (($metadata['DEFAULT'] != null) &&
1495 ($metadata['NULLABLE'] !== true || ($metadata['NULLABLE'] === true && isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] === true)) &&
1496 (!(isset($this->_defaultValues[$metadataName]) && $this->_defaultValues[$metadataName] === false))) {
1497 $defaults[$metadataName] = $metadata['DEFAULT'];
1498 }
1499 }
1500 } elseif ($defaultSource == self::DEFAULT_CLASS && $this->_defaultValues) {
1501 foreach ($this->_defaultValues as $defaultName => $defaultValue) {
1502 if (array_key_exists($defaultName, $defaults)) {
1503 $defaults[$defaultName] = $defaultValue;
1504 }
1505 }
1506 }
1507
1508 $config = array(
1509 'table' => $this,
1510 'data' => $defaults,
1511 'readOnly' => false,
1512 'stored' => false
1513 );
1514
1515 $rowClass = $this->getRowClass();
1516 if (!class_exists($rowClass)) {
1517 require_once 'Zend/Loader.php';
1518 Zend_Loader::loadClass($rowClass);
1519 }
1520 $row = new $rowClass($config);
1521 $row->setFromArray($data);
1522 return $row;
1523 }
1524
1525 /**
1526 * Generate WHERE clause from user-supplied string or array
1527 *
1528 * @param string|array $where OPTIONAL An SQL WHERE clause.
1529 * @return Zend_Db_Table_Select
1530 */
1531 protected function _where(Zend_Db_Table_Select $select, $where)
1532 {
1533 $where = (array) $where;
1534
1535 foreach ($where as $key => $val) {
1536 // is $key an int?
1537 if (is_int($key)) {
1538 // $val is the full condition
1539 $select->where($val);
1540 } else {
1541 // $key is the condition with placeholder,
1542 // and $val is quoted into the condition
1543 $select->where($key, $val);
1544 }
1545 }
1546
1547 return $select;
1548 }
1549
1550 /**
1551 * Generate ORDER clause from user-supplied string or array
1552 *
1553 * @param string|array $order OPTIONAL An SQL ORDER clause.
1554 * @return Zend_Db_Table_Select
1555 */
1556 protected function _order(Zend_Db_Table_Select $select, $order)
1557 {
1558 if (!is_array($order)) {
1559 $order = array($order);
1560 }
1561
1562 foreach ($order as $val) {
1563 $select->order($val);
1564 }
1565
1566 return $select;
1567 }
1568
1569 /**
1570 * Support method for fetching rows.
1571 *
1572 * @param Zend_Db_Table_Select $select query options.
1573 * @return array An array containing the row results in FETCH_ASSOC mode.
1574 */
1575 protected function _fetch(Zend_Db_Table_Select $select)
1576 {
1577 $stmt = $this->_db->query($select);
1578 $data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
1579 return $data;
1580 }
1581
1582 /**
1583 * Get table gateway object from string
1584 *
1585 * @param string $tableName
1586 * @param Zend_Db_Table_Abstract $referenceTable
1587 * @throws Zend_Db_Table_Row_Exception
1588 * @return Zend_Db_Table_Abstract
1589 */
1590 public static function getTableFromString($tableName, Zend_Db_Table_Abstract $referenceTable = null)
1591 {
1592 if ($referenceTable instanceof Zend_Db_Table_Abstract) {
1593 $tableDefinition = $referenceTable->getDefinition();
1594
1595 if ($tableDefinition !== null && $tableDefinition->hasTableConfig($tableName)) {
1596 return new Zend_Db_Table($tableName, $tableDefinition);
1597 }
1598 }
1599
1600 // assume the tableName is the class name
1601 if (!class_exists($tableName)) {
1602 try {
1603 require_once 'Zend/Loader.php';
1604 Zend_Loader::loadClass($tableName);
1605 } catch (Zend_Exception $e) {
1606 require_once 'Zend/Db/Table/Row/Exception.php';
1607 throw new Zend_Db_Table_Row_Exception($e->getMessage(), $e->getCode(), $e);
1608 }
1609 }
1610
1611 $options = array();
1612
1613 if ($referenceTable instanceof Zend_Db_Table_Abstract) {
1614 $options['db'] = $referenceTable->getAdapter();
1615 }
1616
1617 if (isset($tableDefinition) && $tableDefinition !== null) {
1618 $options[Zend_Db_Table_Abstract::DEFINITION] = $tableDefinition;
1619 }
1620
1621 return new $tableName($options);
1622 }
1623
1624 }
1625
-
open/home/peruvian/subdomains/m.peruvianvip.com/application/models/DbTable/Comment.php
Zend_Db_Table_Abstract->fetchAll(Zend_Db_Table_Select)
1
<?php
2 class Model_DbTable_Comment extends Core_Db_Table
3 {
4 public $_name = 'comentarios';
5 public $_primary = 'id';
6
7 public function getComments($idModel)
8 {
9 $select = $this->select()->setIntegrityCheck(false);
10 $select
11 ->from(array('co' => $this->_name), array('*'))
12 ->where('co.aprobado=?', 1)
13 ->where('co.modelo_id=?', $idModel)
14 ->order('co.fecha DESC')
15 ->limit(15);
16
17 $result = $this->fetchAll($select);
18 return ($result->count() > 0) ? $result : false;
19 }
20 }
-
open/home/peruvian/subdomains/m.peruvianvip.com/application/models/Model.php
Model_DbTable_Comment->getComments(NULL)
1
<?php
2 class Model_Model
3 {
4 private $_tableComment = null;
5 private $_tableModel = null;
6
7 public function __construct()
8 {
9 $this->_tableComment = new Model_DbTable_Comment();
10 $this->_tableModel = new Model_DbTable_Model();
11 }
12
13 public function getFeatured($limit=false)
14 {
15 return $this->_tableModel->getFeatured($limit);
16 }
17
18 public function getOneByPermalink($permalink=false)
19 {
20 return $this->_tableModel->getOneByPermalink($permalink);
21 }
22
23 public function getAllEscorts($new=false, $hostess=false)
24 {
25 return $this->_tableModel->getAllEscorts($new, $hostess);
26 }
27
28 public function getAllEscortsByCity($city=false, $hostess=false)
29 {
30 return $this->_tableModel->getAllEscortsByCity($city, $hostess);
31 }
32
33 public function getComments($idModel)
34 {
35 return $this->_tableComment->getComments($idModel);
36 }
37 }
-
open/home/peruvian/subdomains/m.peruvianvip.com/application/modules/default/controllers/ModelController.php
Model_Model->getComments(NULL)
1
<?php
2 class ModelController extends App_Controller_Action
3 {
4 private $_modelModel = null;
5 private $_newSection = null;
6
7 public function preDispatch()
8 {
9 $this->_newSection = (bool)$this->_request->getParam('new', false);
10 if ($this->_newSection === true) {
11 $this->_newModelsClass = 'selected';
12 }
13
14 parent::preDispatch();
15 }
16
17 public function init()
18 {
19 $this->_modelModel = new Model_Model();
20
21 parent::init();
22 }
23
24 public function listAction()
25 {
26 $city = $this->_request->getParam('city', false);
27
28 $escortsHostess = null;
29 $escortsModels = null;
30
31 $newModelsClass = '';
32
33 if ($city !== false AND !empty($city)) {
34 if ($city == 6) {
35 $escortsHostess = $this->_modelModel->getAllEscorts(false, Model_DbTable_Model::TYPE_HOSTESS);
36 $escortsModels = $this->_modelModel->getAllEscorts(false, Model_DbTable_Model::TYPE_MODEL);
37 } else {
38 $escortsHostess = $this->_modelModel->getAllEscortsByCity($city, Model_DbTable_Model::TYPE_HOSTESS);
39 $escortsModels = $this->_modelModel->getAllEscortsByCity($city, Model_DbTable_Model::TYPE_MODEL);
40 }
41 } else {
42 if ($this->_newSection !== false) {
43 $escortsHostess = $this->_modelModel->getAllEscorts(true, Model_DbTable_Model::TYPE_HOSTESS);
44 $escortsModels = $this->_modelModel->getAllEscorts(true, Model_DbTable_Model::TYPE_MODEL);
45 } else {
46 $escort = (bool)$this->_request->getParam('escort', false);
47 $hostess = (bool)$this->_request->getParam('hostess', false);
48
49 if ($escort === false && $hostess === false) {
50 $escortsHostess = $this->_modelModel->getAllEscorts(false, Model_DbTable_Model::TYPE_HOSTESS);
51 $escortsModels = $this->_modelModel->getAllEscorts(false, Model_DbTable_Model::TYPE_MODEL);
52 } else if ($escort === true) {
53 $escortsModels = $this->_modelModel->getAllEscorts(false, Model_DbTable_Model::TYPE_MODEL);
54 } else if ($hostess === true) {
55 $escortsHostess = $this->_modelModel->getAllEscorts(false, Model_DbTable_Model::TYPE_HOSTESS);
56 }
57 }
58 }
59
60 $this->view->escortsHostess = $escortsHostess;
61 $this->view->escortsModels = $escortsModels;
62 }
63
64 public function detailAction()
65 {
66 $permalink = $this->_request->getParam('permalink', false);
67 if ($permalink === false) {
68 $this->_redirect('/');
69 }
70
71 $this->view->model = $this->_modelModel->getOneByPermalink($permalink);
72 }
73
74 public function galleryAction()
75 {
76 $permalink = $this->_request->getParam('permalink', false);
77 if ($permalink === false) {
78 $this->_redirect('/');
79 }
80
81 $model = $this->_modelModel->getOneByPermalink($permalink);
82
83 $this->view->permalink = $model->permalink;
84
85 $modelGallery = new Model_Gallery();
86 $this->view->photos = $modelGallery->getPhotos($model->gallery);
87
88 $this->view->comments = $this->_modelModel->getComments($model->id);
89 }
90
91 public function photoAction()
92 {
93 $permalink = $this->_request->getParam('permalink', false);
94 if ($permalink === false) {
95 $this->_redirect('/');
96 }
97
98 $photo = $this->_request->getParam('photo', false);
99 if ($photo === false) {
100 $this->_redirect('/');
101 }
102
103 //obtener modelo
104 $model = $this->_modelModel->getOneByPermalink($permalink);
105 $this->view->permalink = $model->permalink;
106
107 //obtener fotos
108 $modelGallery = new Model_Gallery();
109 $photos = $modelGallery->getPhotos($model->gallery);
110
111 //verificar Ãndice actual
112 foreach ($photos as $idx => $objPhoto) {
113 if ($objPhoto->filename === $photo) {
114 break;
115 }
116 }
117
118 $this->view->photos = $photos;
119 $this->view->photoIdx = $idx;
120 }
121 }
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Controller/Action.php
ModelController->galleryAction()
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_Controller
17 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
20 */
21
22 /**
23 * @see Zend_Controller_Action_HelperBroker
24 */
25 require_once 'Zend/Controller/Action/HelperBroker.php';
26
27 /**
28 * @see Zend_Controller_Action_Interface
29 */
30 require_once 'Zend/Controller/Action/Interface.php';
31
32 /**
33 * @see Zend_Controller_Front
34 */
35 require_once 'Zend/Controller/Front.php';
36
37 /**
38 * @category Zend
39 * @package Zend_Controller
40 * @copyright Copyright (c) 2005-2015 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_Controller_Action implements Zend_Controller_Action_Interface
44 {
45 /**
46 * @var array of existing class methods
47 */
48 protected $_classMethods;
49
50 /**
51 * Word delimiters (used for normalizing view script paths)
52 * @var array
53 */
54 protected $_delimiters;
55
56 /**
57 * Array of arguments provided to the constructor, minus the
58 * {@link $_request Request object}.
59 * @var array
60 */
61 protected $_invokeArgs = array();
62
63 /**
64 * Front controller instance
65 * @var Zend_Controller_Front
66 */
67 protected $_frontController;
68
69 /**
70 * Zend_Controller_Request_Abstract object wrapping the request environment
71 * @var Zend_Controller_Request_Abstract
72 */
73 protected $_request = null;
74
75 /**
76 * Zend_Controller_Response_Abstract object wrapping the response
77 * @var Zend_Controller_Response_Abstract
78 */
79 protected $_response = null;
80
81 /**
82 * View script suffix; defaults to 'phtml'
83 * @see {render()}
84 * @var string
85 */
86 public $viewSuffix = 'phtml';
87
88 /**
89 * View object
90 * @var Zend_View_Interface
91 */
92 public $view;
93
94 /**
95 * Helper Broker to assist in routing help requests to the proper object
96 *
97 * @var Zend_Controller_Action_HelperBroker
98 */
99 protected $_helper = null;
100
101 /**
102 * Class constructor
103 *
104 * The request and response objects should be registered with the
105 * controller, as should be any additional optional arguments; these will be
106 * available via {@link getRequest()}, {@link getResponse()}, and
107 * {@link getInvokeArgs()}, respectively.
108 *
109 * When overriding the constructor, please consider this usage as a best
110 * practice and ensure that each is registered appropriately; the easiest
111 * way to do so is to simply call parent::__construct($request, $response,
112 * $invokeArgs).
113 *
114 * After the request, response, and invokeArgs are set, the
115 * {@link $_helper helper broker} is initialized.
116 *
117 * Finally, {@link init()} is called as the final action of
118 * instantiation, and may be safely overridden to perform initialization
119 * tasks; as a general rule, override {@link init()} instead of the
120 * constructor to customize an action controller's instantiation.
121 *
122 * @param Zend_Controller_Request_Abstract $request
123 * @param Zend_Controller_Response_Abstract $response
124 * @param array $invokeArgs Any additional invocation arguments
125 * @return void
126 */
127 public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
128 {
129 $this->setRequest($request)
130 ->setResponse($response)
131 ->_setInvokeArgs($invokeArgs);
132 $this->_helper = new Zend_Controller_Action_HelperBroker($this);
133 $this->init();
134 }
135
136 /**
137 * Initialize object
138 *
139 * Called from {@link __construct()} as final step of object instantiation.
140 *
141 * @return void
142 */
143 public function init()
144 {
145 }
146
147 /**
148 * Initialize View object
149 *
150 * Initializes {@link $view} if not otherwise a Zend_View_Interface.
151 *
152 * If {@link $view} is not otherwise set, instantiates a new Zend_View
153 * object, using the 'views' subdirectory at the same level as the
154 * controller directory for the current module as the base directory.
155 * It uses this to set the following:
156 * - script path = views/scripts/
157 * - helper path = views/helpers/
158 * - filter path = views/filters/
159 *
160 * @return Zend_View_Interface
161 * @throws Zend_Controller_Exception if base view directory does not exist
162 */
163 public function initView()
164 {
165 if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
166 return $this->view;
167 }
168
169 require_once 'Zend/View/Interface.php';
170 if (isset($this->view) && ($this->view instanceof Zend_View_Interface)) {
171 return $this->view;
172 }
173
174 $request = $this->getRequest();
175 $module = $request->getModuleName();
176 $dirs = $this->getFrontController()->getControllerDirectory();
177 if (empty($module) || !isset($dirs[$module])) {
178 $module = $this->getFrontController()->getDispatcher()->getDefaultModule();
179 }
180 $baseDir = dirname($dirs[$module]) . DIRECTORY_SEPARATOR . 'views';
181 if (!file_exists($baseDir) || !is_dir($baseDir)) {
182 require_once 'Zend/Controller/Exception.php';
183 throw new Zend_Controller_Exception('Missing base view directory ("' . $baseDir . '")');
184 }
185
186 require_once 'Zend/View.php';
187 $this->view = new Zend_View(array('basePath' => $baseDir));
188
189 return $this->view;
190 }
191
192 /**
193 * Render a view
194 *
195 * Renders a view. By default, views are found in the view script path as
196 * <controller>/<action>.phtml. You may change the script suffix by
197 * resetting {@link $viewSuffix}. You may omit the controller directory
198 * prefix by specifying boolean true for $noController.
199 *
200 * By default, the rendered contents are appended to the response. You may
201 * specify the named body content segment to set by specifying a $name.
202 *
203 * @see Zend_Controller_Response_Abstract::appendBody()
204 * @param string|null $action Defaults to action registered in request object
205 * @param string|null $name Response object named path segment to use; defaults to null
206 * @param bool $noController Defaults to false; i.e. use controller name as subdir in which to search for view script
207 * @return void
208 */
209 public function render($action = null, $name = null, $noController = false)
210 {
211 if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
212 return $this->_helper->viewRenderer->render($action, $name, $noController);
213 }
214
215 $view = $this->initView();
216 $script = $this->getViewScript($action, $noController);
217
218 $this->getResponse()->appendBody(
219 $view->render($script),
220 $name
221 );
222 }
223
224 /**
225 * Render a given view script
226 *
227 * Similar to {@link render()}, this method renders a view script. Unlike render(),
228 * however, it does not autodetermine the view script via {@link getViewScript()},
229 * but instead renders the script passed to it. Use this if you know the
230 * exact view script name and path you wish to use, or if using paths that do not
231 * conform to the spec defined with getViewScript().
232 *
233 * By default, the rendered contents are appended to the response. You may
234 * specify the named body content segment to set by specifying a $name.
235 *
236 * @param string $script
237 * @param string $name
238 * @return void
239 */
240 public function renderScript($script, $name = null)
241 {
242 if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
243 return $this->_helper->viewRenderer->renderScript($script, $name);
244 }
245
246 $view = $this->initView();
247 $this->getResponse()->appendBody(
248 $view->render($script),
249 $name
250 );
251 }
252
253 /**
254 * Construct view script path
255 *
256 * Used by render() to determine the path to the view script.
257 *
258 * @param string $action Defaults to action registered in request object
259 * @param bool $noController Defaults to false; i.e. use controller name as subdir in which to search for view script
260 * @return string
261 * @throws Zend_Controller_Exception with bad $action
262 */
263 public function getViewScript($action = null, $noController = null)
264 {
265 if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
266 $viewRenderer = $this->_helper->getHelper('viewRenderer');
267 if (null !== $noController) {
268 $viewRenderer->setNoController($noController);
269 }
270 return $viewRenderer->getViewScript($action);
271 }
272
273 $request = $this->getRequest();
274 if (null === $action) {
275 $action = $request->getActionName();
276 } elseif (!is_string($action)) {
277 require_once 'Zend/Controller/Exception.php';
278 throw new Zend_Controller_Exception('Invalid action specifier for view render');
279 }
280
281 if (null === $this->_delimiters) {
282 $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
283 $wordDelimiters = $dispatcher->getWordDelimiter();
284 $pathDelimiters = $dispatcher->getPathDelimiter();
285 $this->_delimiters = array_unique(array_merge($wordDelimiters, (array) $pathDelimiters));
286 }
287
288 $action = str_replace($this->_delimiters, '-', $action);
289 $script = $action . '.' . $this->viewSuffix;
290
291 if (!$noController) {
292 $controller = $request->getControllerName();
293 $controller = str_replace($this->_delimiters, '-', $controller);
294 $script = $controller . DIRECTORY_SEPARATOR . $script;
295 }
296
297 return $script;
298 }
299
300 /**
301 * Return the Request object
302 *
303 * @return Zend_Controller_Request_Abstract
304 */
305 public function getRequest()
306 {
307 return $this->_request;
308 }
309
310 /**
311 * Set the Request object
312 *
313 * @param Zend_Controller_Request_Abstract $request
314 * @return Zend_Controller_Action
315 */
316 public function setRequest(Zend_Controller_Request_Abstract $request)
317 {
318 $this->_request = $request;
319 return $this;
320 }
321
322 /**
323 * Return the Response object
324 *
325 * @return Zend_Controller_Response_Abstract
326 */
327 public function getResponse()
328 {
329 return $this->_response;
330 }
331
332 /**
333 * Set the Response object
334 *
335 * @param Zend_Controller_Response_Abstract $response
336 * @return Zend_Controller_Action
337 */
338 public function setResponse(Zend_Controller_Response_Abstract $response)
339 {
340 $this->_response = $response;
341 return $this;
342 }
343
344 /**
345 * Set invocation arguments
346 *
347 * @param array $args
348 * @return Zend_Controller_Action
349 */
350 protected function _setInvokeArgs(array $args = array())
351 {
352 $this->_invokeArgs = $args;
353 return $this;
354 }
355
356 /**
357 * Return the array of constructor arguments (minus the Request object)
358 *
359 * @return array
360 */
361 public function getInvokeArgs()
362 {
363 return $this->_invokeArgs;
364 }
365
366 /**
367 * Return a single invocation argument
368 *
369 * @param string $key
370 * @return mixed
371 */
372 public function getInvokeArg($key)
373 {
374 if (isset($this->_invokeArgs[$key])) {
375 return $this->_invokeArgs[$key];
376 }
377
378 return null;
379 }
380
381 /**
382 * Get a helper by name
383 *
384 * @param string $helperName
385 * @return Zend_Controller_Action_Helper_Abstract
386 */
387 public function getHelper($helperName)
388 {
389 return $this->_helper->{$helperName};
390 }
391
392 /**
393 * Get a clone of a helper by name
394 *
395 * @param string $helperName
396 * @return Zend_Controller_Action_Helper_Abstract
397 */
398 public function getHelperCopy($helperName)
399 {
400 return clone $this->_helper->{$helperName};
401 }
402
403 /**
404 * Set the front controller instance
405 *
406 * @param Zend_Controller_Front $front
407 * @return Zend_Controller_Action
408 */
409 public function setFrontController(Zend_Controller_Front $front)
410 {
411 $this->_frontController = $front;
412 return $this;
413 }
414
415 /**
416 * Retrieve Front Controller
417 *
418 * @return Zend_Controller_Front
419 */
420 public function getFrontController()
421 {
422 // Used cache version if found
423 if (null !== $this->_frontController) {
424 return $this->_frontController;
425 }
426
427 // Grab singleton instance, if class has been loaded
428 if (class_exists('Zend_Controller_Front')) {
429 $this->_frontController = Zend_Controller_Front::getInstance();
430 return $this->_frontController;
431 }
432
433 // Throw exception in all other cases
434 require_once 'Zend/Controller/Exception.php';
435 throw new Zend_Controller_Exception('Front controller class has not been loaded');
436 }
437
438 /**
439 * Pre-dispatch routines
440 *
441 * Called before action method. If using class with
442 * {@link Zend_Controller_Front}, it may modify the
443 * {@link $_request Request object} and reset its dispatched flag in order
444 * to skip processing the current action.
445 *
446 * @return void
447 */
448 public function preDispatch()
449 {
450 }
451
452 /**
453 * Post-dispatch routines
454 *
455 * Called after action method execution. If using class with
456 * {@link Zend_Controller_Front}, it may modify the
457 * {@link $_request Request object} and reset its dispatched flag in order
458 * to process an additional action.
459 *
460 * Common usages for postDispatch() include rendering content in a sitewide
461 * template, link url correction, setting headers, etc.
462 *
463 * @return void
464 */
465 public function postDispatch()
466 {
467 }
468
469 /**
470 * Proxy for undefined methods. Default behavior is to throw an
471 * exception on undefined methods, however this function can be
472 * overridden to implement magic (dynamic) actions, or provide run-time
473 * dispatching.
474 *
475 * @param string $methodName
476 * @param array $args
477 * @return void
478 * @throws Zend_Controller_Action_Exception
479 */
480 public function __call($methodName, $args)
481 {
482 require_once 'Zend/Controller/Action/Exception.php';
483 if ('Action' == substr($methodName, -6)) {
484 $action = substr($methodName, 0, strlen($methodName) - 6);
485 throw new Zend_Controller_Action_Exception(sprintf('Action "%s" does not exist and was not trapped in __call()', $action), 404);
486 }
487
488 throw new Zend_Controller_Action_Exception(sprintf('Method "%s" does not exist and was not trapped in __call()', $methodName), 500);
489 }
490
491 /**
492 * Dispatch the requested action
493 *
494 * @param string $action Method name of action
495 * @return void
496 */
497 public function dispatch($action)
498 {
499 // Notify helpers of action preDispatch state
500 $this->_helper->notifyPreDispatch();
501
502 $this->preDispatch();
503 if ($this->getRequest()->isDispatched()) {
504 if (null === $this->_classMethods) {
505 $this->_classMethods = get_class_methods($this);
506 }
507
508 // If pre-dispatch hooks introduced a redirect then stop dispatch
509 // @see ZF-7496
510 if (!($this->getResponse()->isRedirect())) {
511 // preDispatch() didn't change the action, so we can continue
512 if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
513 if ($this->getInvokeArg('useCaseSensitiveActions')) {
514 trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
515 }
516 $this->$action();
517 } else {
518 $this->__call($action, array());
519 }
520 }
521 $this->postDispatch();
522 }
523
524 // whats actually important here is that this action controller is
525 // shutting down, regardless of dispatching; notify the helpers of this
526 // state
527 $this->_helper->notifyPostDispatch();
528 }
529
530 /**
531 * Call the action specified in the request object, and return a response
532 *
533 * Not used in the Action Controller implementation, but left for usage in
534 * Page Controller implementations. Dispatches a method based on the
535 * request.
536 *
537 * Returns a Zend_Controller_Response_Abstract object, instantiating one
538 * prior to execution if none exists in the controller.
539 *
540 * {@link preDispatch()} is called prior to the action,
541 * {@link postDispatch()} is called following it.
542 *
543 * @param null|Zend_Controller_Request_Abstract $request Optional request
544 * object to use
545 * @param null|Zend_Controller_Response_Abstract $response Optional response
546 * object to use
547 * @return Zend_Controller_Response_Abstract
548 */
549 public function run(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
550 {
551 if (null !== $request) {
552 $this->setRequest($request);
553 } else {
554 $request = $this->getRequest();
555 }
556
557 if (null !== $response) {
558 $this->setResponse($response);
559 }
560
561 $action = $request->getActionName();
562 if (empty($action)) {
563 $action = 'index';
564 }
565 $action = $action . 'Action';
566
567 $request->setDispatched(true);
568 $this->dispatch($action);
569
570 return $this->getResponse();
571 }
572
573 /**
574 * Gets a parameter from the {@link $_request Request object}. If the
575 * parameter does not exist, NULL will be returned.
576 *
577 * If the parameter does not exist and $default is set, then
578 * $default will be returned instead of NULL.
579 *
580 * @param string $paramName
581 * @param mixed $default
582 * @return mixed
583 */
584 protected function _getParam($paramName, $default = null)
585 {
586 return $this->getParam($paramName, $default);
587 }
588
589 /**
590 * Gets a parameter from the {@link $_request Request object}. If the
591 * parameter does not exist, NULL will be returned.
592 *
593 * If the parameter does not exist and $default is set, then
594 * $default will be returned instead of NULL.
595 *
596 * @param string $paramName
597 * @param mixed $default
598 * @return mixed
599 */
600 public function getParam($paramName, $default = null)
601 {
602 $value = $this->getRequest()->getParam($paramName);
603 if ((null === $value || '' === $value) && (null !== $default)) {
604 $value = $default;
605 }
606
607 return $value;
608 }
609
610 /**
611 * Set a parameter in the {@link $_request Request object}.
612 *
613 * @param string $paramName
614 * @param mixed $value
615 * @return Zend_Controller_Action
616 * @deprecated Deprecated as of Zend Framework 1.7. Use
617 * setParam() instead.
618 */
619 protected function _setParam($paramName, $value)
620 {
621 return $this->setParam($paramName, $value);
622 }
623
624 /**
625 * Set a parameter in the {@link $_request Request object}.
626 *
627 * @param string $paramName
628 * @param mixed $value
629 * @return Zend_Controller_Action
630 */
631 public function setParam($paramName, $value)
632 {
633 $this->getRequest()->setParam($paramName, $value);
634
635 return $this;
636 }
637
638 /**
639 * Determine whether a given parameter exists in the
640 * {@link $_request Request object}.
641 *
642 * @param string $paramName
643 * @return boolean
644 * @deprecated Deprecated as of Zend Framework 1.7. Use
645 * hasParam() instead.
646 */
647 protected function _hasParam($paramName)
648 {
649 return $this->hasParam($paramName);
650 }
651
652 /**
653 * Determine whether a given parameter exists in the
654 * {@link $_request Request object}.
655 *
656 * @param string $paramName
657 * @return boolean
658 */
659 public function hasParam($paramName)
660 {
661 return null !== $this->getRequest()->getParam($paramName);
662 }
663
664 /**
665 * Return all parameters in the {@link $_request Request object}
666 * as an associative array.
667 *
668 * @return array
669 * @deprecated Deprecated as of Zend Framework 1.7. Use
670 * getAllParams() instead.
671 */
672 protected function _getAllParams()
673 {
674 return $this->getAllParams();
675 }
676
677 /**
678 * Return all parameters in the {@link $_request Request object}
679 * as an associative array.
680 *
681 * @return array
682 */
683 public function getAllParams()
684 {
685 return $this->getRequest()->getParams();
686 }
687
688
689 /**
690 * Forward to another controller/action.
691 *
692 * It is important to supply the unformatted names, i.e. "article"
693 * rather than "ArticleController". The dispatcher will do the
694 * appropriate formatting when the request is received.
695 *
696 * If only an action name is provided, forwards to that action in this
697 * controller.
698 *
699 * If an action and controller are specified, forwards to that action and
700 * controller in this module.
701 *
702 * Specifying an action, controller, and module is the most specific way to
703 * forward.
704 *
705 * A fourth argument, $params, will be used to set the request parameters.
706 * If either the controller or module are unnecessary for forwarding,
707 * simply pass null values for them before specifying the parameters.
708 *
709 * @param string $action
710 * @param string $controller
711 * @param string $module
712 * @param array $params
713 * @return void
714 * @deprecated Deprecated as of Zend Framework 1.7. Use
715 * forward() instead.
716 */
717 final protected function _forward($action, $controller = null, $module = null, array $params = null)
718 {
719 $this->forward($action, $controller, $module, $params);
720 }
721
722 /**
723 * Forward to another controller/action.
724 *
725 * It is important to supply the unformatted names, i.e. "article"
726 * rather than "ArticleController". The dispatcher will do the
727 * appropriate formatting when the request is received.
728 *
729 * If only an action name is provided, forwards to that action in this
730 * controller.
731 *
732 * If an action and controller are specified, forwards to that action and
733 * controller in this module.
734 *
735 * Specifying an action, controller, and module is the most specific way to
736 * forward.
737 *
738 * A fourth argument, $params, will be used to set the request parameters.
739 * If either the controller or module are unnecessary for forwarding,
740 * simply pass null values for them before specifying the parameters.
741 *
742 * @param string $action
743 * @param string $controller
744 * @param string $module
745 * @param array $params
746 * @return void
747 */
748 final public function forward($action, $controller = null, $module = null, array $params = null)
749 {
750 $request = $this->getRequest();
751
752 if (null !== $params) {
753 $request->setParams($params);
754 }
755
756 if (null !== $controller) {
757 $request->setControllerName($controller);
758
759 // Module should only be reset if controller has been specified
760 if (null !== $module) {
761 $request->setModuleName($module);
762 }
763 }
764
765 $request->setActionName($action)
766 ->setDispatched(false);
767 }
768
769 /**
770 * Redirect to another URL
771 *
772 * Proxies to {@link Zend_Controller_Action_Helper_Redirector::gotoUrl()}.
773 *
774 * @param string $url
775 * @param array $options Options to be used when redirecting
776 * @return void
777 * @deprecated Deprecated as of Zend Framework 1.7. Use
778 * redirect() instead.
779 */
780 protected function _redirect($url, array $options = array())
781 {
782 $this->redirect($url, $options);
783 }
784
785 /**
786 * Redirect to another URL
787 *
788 * Proxies to {@link Zend_Controller_Action_Helper_Redirector::gotoUrl()}.
789 *
790 * @param string $url
791 * @param array $options Options to be used when redirecting
792 * @return void
793 */
794 public function redirect($url, array $options = array())
795 {
796 $this->_helper->redirector->gotoUrl($url, $options);
797 }
798 }
799
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Controller/Dispatcher/Standard.php
Zend_Controller_Action->dispatch('galleryAction')
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_Controller
17 * @subpackage Dispatcher
18 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
21 */
22
23 /** Zend_Loader */
24 require_once 'Zend/Loader.php';
25
26 /** Zend_Controller_Dispatcher_Abstract */
27 require_once 'Zend/Controller/Dispatcher/Abstract.php';
28
29 /**
30 * @category Zend
31 * @package Zend_Controller
32 * @subpackage Dispatcher
33 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
35 */
36 class Zend_Controller_Dispatcher_Standard extends Zend_Controller_Dispatcher_Abstract
37 {
38 /**
39 * Current dispatchable directory
40 * @var string
41 */
42 protected $_curDirectory;
43
44 /**
45 * Current module (formatted)
46 * @var string
47 */
48 protected $_curModule;
49
50 /**
51 * Controller directory(ies)
52 * @var array
53 */
54 protected $_controllerDirectory = array();
55
56 /**
57 * Constructor: Set current module to default value
58 *
59 * @param array $params
60 * @return void
61 */
62 public function __construct(array $params = array())
63 {
64 parent::__construct($params);
65 $this->_curModule = $this->getDefaultModule();
66 }
67
68 /**
69 * Add a single path to the controller directory stack
70 *
71 * @param string $path
72 * @param string $module
73 * @return Zend_Controller_Dispatcher_Standard
74 */
75 public function addControllerDirectory($path, $module = null)
76 {
77 if (null === $module) {
78 $module = $this->_defaultModule;
79 }
80
81 $module = (string) $module;
82 $path = rtrim((string) $path, '/\\');
83
84 $this->_controllerDirectory[$module] = $path;
85 return $this;
86 }
87
88 /**
89 * Set controller directory
90 *
91 * @param array|string $directory
92 * @return Zend_Controller_Dispatcher_Standard
93 */
94 public function setControllerDirectory($directory, $module = null)
95 {
96 $this->_controllerDirectory = array();
97
98 if (is_string($directory)) {
99 $this->addControllerDirectory($directory, $module);
100 } elseif (is_array($directory)) {
101 foreach ((array) $directory as $module => $path) {
102 $this->addControllerDirectory($path, $module);
103 }
104 } else {
105 require_once 'Zend/Controller/Exception.php';
106 throw new Zend_Controller_Exception('Controller directory spec must be either a string or an array');
107 }
108
109 return $this;
110 }
111
112 /**
113 * Return the currently set directories for Zend_Controller_Action class
114 * lookup
115 *
116 * If a module is specified, returns just that directory.
117 *
118 * @param string $module Module name
119 * @return array|string Returns array of all directories by default, single
120 * module directory if module argument provided
121 */
122 public function getControllerDirectory($module = null)
123 {
124 if (null === $module) {
125 return $this->_controllerDirectory;
126 }
127
128 $module = (string) $module;
129 if (array_key_exists($module, $this->_controllerDirectory)) {
130 return $this->_controllerDirectory[$module];
131 }
132
133 return null;
134 }
135
136 /**
137 * Remove a controller directory by module name
138 *
139 * @param string $module
140 * @return bool
141 */
142 public function removeControllerDirectory($module)
143 {
144 $module = (string) $module;
145 if (array_key_exists($module, $this->_controllerDirectory)) {
146 unset($this->_controllerDirectory[$module]);
147 return true;
148 }
149 return false;
150 }
151
152 /**
153 * Format the module name.
154 *
155 * @param string $unformatted
156 * @return string
157 */
158 public function formatModuleName($unformatted)
159 {
160 if (($this->_defaultModule == $unformatted) && !$this->getParam('prefixDefaultModule')) {
161 return $unformatted;
162 }
163
164 return ucfirst($this->_formatName($unformatted));
165 }
166
167 /**
168 * Format action class name
169 *
170 * @param string $moduleName Name of the current module
171 * @param string $className Name of the action class
172 * @return string Formatted class name
173 */
174 public function formatClassName($moduleName, $className)
175 {
176 return $this->formatModuleName($moduleName) . '_' . $className;
177 }
178
179 /**
180 * Convert a class name to a filename
181 *
182 * @param string $class
183 * @return string
184 */
185 public function classToFilename($class)
186 {
187 return str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
188 }
189
190 /**
191 * Returns TRUE if the Zend_Controller_Request_Abstract object can be
192 * dispatched to a controller.
193 *
194 * Use this method wisely. By default, the dispatcher will fall back to the
195 * default controller (either in the module specified or the global default)
196 * if a given controller does not exist. This method returning false does
197 * not necessarily indicate the dispatcher will not still dispatch the call.
198 *
199 * @param Zend_Controller_Request_Abstract $action
200 * @return boolean
201 */
202 public function isDispatchable(Zend_Controller_Request_Abstract $request)
203 {
204 $className = $this->getControllerClass($request);
205 if (!$className) {
206 return false;
207 }
208
209 $finalClass = $className;
210 if (($this->_defaultModule != $this->_curModule)
211 || $this->getParam('prefixDefaultModule'))
212 {
213 $finalClass = $this->formatClassName($this->_curModule, $className);
214 }
215 if (class_exists($finalClass, false)) {
216 return true;
217 }
218
219 $fileSpec = $this->classToFilename($className);
220 $dispatchDir = $this->getDispatchDirectory();
221 $test = $dispatchDir . DIRECTORY_SEPARATOR . $fileSpec;
222 return Zend_Loader::isReadable($test);
223 }
224
225 /**
226 * Dispatch to a controller/action
227 *
228 * By default, if a controller is not dispatchable, dispatch() will throw
229 * an exception. If you wish to use the default controller instead, set the
230 * param 'useDefaultControllerAlways' via {@link setParam()}.
231 *
232 * @param Zend_Controller_Request_Abstract $request
233 * @param Zend_Controller_Response_Abstract $response
234 * @return void
235 * @throws Zend_Controller_Dispatcher_Exception
236 */
237 public function dispatch(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response)
238 {
239 $this->setResponse($response);
240
241 /**
242 * Get controller class
243 */
244 if (!$this->isDispatchable($request)) {
245 $controller = $request->getControllerName();
246 if (!$this->getParam('useDefaultControllerAlways') && !empty($controller)) {
247 require_once 'Zend/Controller/Dispatcher/Exception.php';
248 throw new Zend_Controller_Dispatcher_Exception('Invalid controller specified (' . $request->getControllerName() . ')');
249 }
250
251 $className = $this->getDefaultControllerClass($request);
252 } else {
253 $className = $this->getControllerClass($request);
254 if (!$className) {
255 $className = $this->getDefaultControllerClass($request);
256 }
257 }
258
259 /**
260 * If we're in a module or prefixDefaultModule is on, we must add the module name
261 * prefix to the contents of $className, as getControllerClass does not do that automatically.
262 * We must keep a separate variable because modules are not strictly PSR-0: We need the no-module-prefix
263 * class name to do the class->file mapping, but the full class name to insantiate the controller
264 */
265 $moduleClassName = $className;
266 if (($this->_defaultModule != $this->_curModule)
267 || $this->getParam('prefixDefaultModule'))
268 {
269 $moduleClassName = $this->formatClassName($this->_curModule, $className);
270 }
271
272 /**
273 * Load the controller class file
274 */
275 $className = $this->loadClass($className);
276
277 /**
278 * Instantiate controller with request, response, and invocation
279 * arguments; throw exception if it's not an action controller
280 */
281 $controller = new $moduleClassName($request, $this->getResponse(), $this->getParams());
282 if (!($controller instanceof Zend_Controller_Action_Interface) &&
283 !($controller instanceof Zend_Controller_Action)) {
284 require_once 'Zend/Controller/Dispatcher/Exception.php';
285 throw new Zend_Controller_Dispatcher_Exception(
286 'Controller "' . $moduleClassName . '" is not an instance of Zend_Controller_Action_Interface'
287 );
288 }
289
290 /**
291 * Retrieve the action name
292 */
293 $action = $this->getActionMethod($request);
294
295 /**
296 * Dispatch the method call
297 */
298 $request->setDispatched(true);
299
300 // by default, buffer output
301 $disableOb = $this->getParam('disableOutputBuffering');
302 $obLevel = ob_get_level();
303 if (empty($disableOb)) {
304 ob_start();
305 }
306
307 try {
308 $controller->dispatch($action);
309 } catch (Exception $e) {
310 // Clean output buffer on error
311 $curObLevel = ob_get_level();
312 if ($curObLevel > $obLevel) {
313 do {
314 ob_get_clean();
315 $curObLevel = ob_get_level();
316 } while ($curObLevel > $obLevel);
317 }
318 throw $e;
319 }
320
321 if (empty($disableOb)) {
322 $content = ob_get_clean();
323 $response->appendBody($content);
324 }
325
326 // Destroy the page controller instance and reflection objects
327 $controller = null;
328 }
329
330 /**
331 * Load a controller class
332 *
333 * Attempts to load the controller class file from
334 * {@link getControllerDirectory()}. If the controller belongs to a
335 * module, looks for the module prefix to the controller class.
336 *
337 * @param string $className
338 * @return string Class name loaded
339 * @throws Zend_Controller_Dispatcher_Exception if class not loaded
340 */
341 public function loadClass($className)
342 {
343 $finalClass = $className;
344 if (($this->_defaultModule != $this->_curModule)
345 || $this->getParam('prefixDefaultModule'))
346 {
347 $finalClass = $this->formatClassName($this->_curModule, $className);
348 }
349 if (class_exists($finalClass, false)) {
350 return $finalClass;
351 }
352
353 $dispatchDir = $this->getDispatchDirectory();
354 $loadFile = $dispatchDir . DIRECTORY_SEPARATOR . $this->classToFilename($className);
355
356 if (Zend_Loader::isReadable($loadFile)) {
357 include_once $loadFile;
358 } else {
359 require_once 'Zend/Controller/Dispatcher/Exception.php';
360 throw new Zend_Controller_Dispatcher_Exception('Cannot load controller class "' . $className . '" from file "' . $loadFile . "'");
361 }
362
363 if (!class_exists($finalClass, false)) {
364 require_once 'Zend/Controller/Dispatcher/Exception.php';
365 throw new Zend_Controller_Dispatcher_Exception('Invalid controller class ("' . $finalClass . '")');
366 }
367
368 return $finalClass;
369 }
370
371 /**
372 * Get controller class name
373 *
374 * Try request first; if not found, try pulling from request parameter;
375 * if still not found, fallback to default
376 *
377 * @param Zend_Controller_Request_Abstract $request
378 * @return string|false Returns class name on success
379 */
380 public function getControllerClass(Zend_Controller_Request_Abstract $request)
381 {
382 $controllerName = $request->getControllerName();
383 if (empty($controllerName)) {
384 if (!$this->getParam('useDefaultControllerAlways')) {
385 return false;
386 }
387 $controllerName = $this->getDefaultControllerName();
388 $request->setControllerName($controllerName);
389 }
390
391 $className = $this->formatControllerName($controllerName);
392
393 $controllerDirs = $this->getControllerDirectory();
394 $module = $request->getModuleName();
395 if ($this->isValidModule($module)) {
396 $this->_curModule = $module;
397 $this->_curDirectory = $controllerDirs[$module];
398 } elseif ($this->isValidModule($this->_defaultModule)) {
399 $request->setModuleName($this->_defaultModule);
400 $this->_curModule = $this->_defaultModule;
401 $this->_curDirectory = $controllerDirs[$this->_defaultModule];
402 } else {
403 require_once 'Zend/Controller/Exception.php';
404 throw new Zend_Controller_Exception('No default module defined for this application');
405 }
406
407 return $className;
408 }
409
410 /**
411 * Determine if a given module is valid
412 *
413 * @param string $module
414 * @return bool
415 */
416 public function isValidModule($module)
417 {
418 if (!is_string($module)) {
419 return false;
420 }
421
422 $module = strtolower($module);
423 $controllerDir = $this->getControllerDirectory();
424 foreach (array_keys($controllerDir) as $moduleName) {
425 if ($module == strtolower($moduleName)) {
426 return true;
427 }
428 }
429
430 return false;
431 }
432
433 /**
434 * Retrieve default controller class
435 *
436 * Determines whether the default controller to use lies within the
437 * requested module, or if the global default should be used.
438 *
439 * By default, will only use the module default unless that controller does
440 * not exist; if this is the case, it falls back to the default controller
441 * in the default module.
442 *
443 * @param Zend_Controller_Request_Abstract $request
444 * @return string
445 */
446 public function getDefaultControllerClass(Zend_Controller_Request_Abstract $request)
447 {
448 $controller = $this->getDefaultControllerName();
449 $default = $this->formatControllerName($controller);
450 $request->setControllerName($controller)
451 ->setActionName(null);
452
453 $module = $request->getModuleName();
454 $controllerDirs = $this->getControllerDirectory();
455 $this->_curModule = $this->_defaultModule;
456 $this->_curDirectory = $controllerDirs[$this->_defaultModule];
457 if ($this->isValidModule($module)) {
458 $found = false;
459 if (class_exists($default, false)) {
460 $found = true;
461 } else {
462 $moduleDir = $controllerDirs[$module];
463 $fileSpec = $moduleDir . DIRECTORY_SEPARATOR . $this->classToFilename($default);
464 if (Zend_Loader::isReadable($fileSpec)) {
465 $found = true;
466 $this->_curDirectory = $moduleDir;
467 }
468 }
469 if ($found) {
470 $request->setModuleName($module);
471 $this->_curModule = $this->formatModuleName($module);
472 }
473 } else {
474 $request->setModuleName($this->_defaultModule);
475 }
476
477 return $default;
478 }
479
480 /**
481 * Return the value of the currently selected dispatch directory (as set by
482 * {@link getController()})
483 *
484 * @return string
485 */
486 public function getDispatchDirectory()
487 {
488 return $this->_curDirectory;
489 }
490
491 /**
492 * Determine the action name
493 *
494 * First attempt to retrieve from request; then from request params
495 * using action key; default to default action
496 *
497 * Returns formatted action name
498 *
499 * @param Zend_Controller_Request_Abstract $request
500 * @return string
501 */
502 public function getActionMethod(Zend_Controller_Request_Abstract $request)
503 {
504 $action = $request->getActionName();
505 if (empty($action)) {
506 $action = $this->getDefaultAction();
507 $request->setActionName($action);
508 }
509
510 return $this->formatActionName($action);
511 }
512 }
513
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Controller/Front.php
Zend_Controller_Dispatcher_Standard->dispatch(Zend_Controller_Request_Http, Zend_Controller_Response_Http)
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_Controller
17 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
20 */
21
22
23 /** Zend_Loader */
24 require_once 'Zend/Loader.php';
25
26 /** Zend_Controller_Action_HelperBroker */
27 require_once 'Zend/Controller/Action/HelperBroker.php';
28
29 /** Zend_Controller_Plugin_Broker */
30 require_once 'Zend/Controller/Plugin/Broker.php';
31
32 /**
33 * @category Zend
34 * @package Zend_Controller
35 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
36 * @license http://framework.zend.com/license/new-bsd New BSD License
37 */
38 class Zend_Controller_Front
39 {
40 /**
41 * Base URL
42 * @var string
43 */
44 protected $_baseUrl = null;
45
46 /**
47 * Directory|ies where controllers are stored
48 *
49 * @var string|array
50 */
51 protected $_controllerDir = null;
52
53 /**
54 * Instance of Zend_Controller_Dispatcher_Interface
55 * @var Zend_Controller_Dispatcher_Interface
56 */
57 protected $_dispatcher = null;
58
59 /**
60 * Singleton instance
61 *
62 * Marked only as protected to allow extension of the class. To extend,
63 * simply override {@link getInstance()}.
64 *
65 * @var Zend_Controller_Front
66 */
67 protected static $_instance = null;
68
69 /**
70 * Array of invocation parameters to use when instantiating action
71 * controllers
72 * @var array
73 */
74 protected $_invokeParams = array();
75
76 /**
77 * Subdirectory within a module containing controllers; defaults to 'controllers'
78 * @var string
79 */
80 protected $_moduleControllerDirectoryName = 'controllers';
81
82 /**
83 * Instance of Zend_Controller_Plugin_Broker
84 * @var Zend_Controller_Plugin_Broker
85 */
86 protected $_plugins = null;
87
88 /**
89 * Instance of Zend_Controller_Request_Abstract
90 * @var Zend_Controller_Request_Abstract
91 */
92 protected $_request = null;
93
94 /**
95 * Instance of Zend_Controller_Response_Abstract
96 * @var Zend_Controller_Response_Abstract
97 */
98 protected $_response = null;
99
100 /**
101 * Whether or not to return the response prior to rendering output while in
102 * {@link dispatch()}; default is to send headers and render output.
103 * @var boolean
104 */
105 protected $_returnResponse = false;
106
107 /**
108 * Instance of Zend_Controller_Router_Interface
109 * @var Zend_Controller_Router_Interface
110 */
111 protected $_router = null;
112
113 /**
114 * Whether or not exceptions encountered in {@link dispatch()} should be
115 * thrown or trapped in the response object
116 * @var boolean
117 */
118 protected $_throwExceptions = false;
119
120 /**
121 * Constructor
122 *
123 * Instantiate using {@link getInstance()}; front controller is a singleton
124 * object.
125 *
126 * Instantiates the plugin broker.
127 *
128 * @return void
129 */
130 protected function __construct()
131 {
132 $this->_plugins = new Zend_Controller_Plugin_Broker();
133 }
134
135 /**
136 * Enforce singleton; disallow cloning
137 *
138 * @return void
139 */
140 private function __clone()
141 {
142 }
143
144 /**
145 * Singleton instance
146 *
147 * @return Zend_Controller_Front
148 */
149 public static function getInstance()
150 {
151 if (null === self::$_instance) {
152 self::$_instance = new self();
153 }
154
155 return self::$_instance;
156 }
157
158 /**
159 * Resets all object properties of the singleton instance
160 *
161 * Primarily used for testing; could be used to chain front controllers.
162 *
163 * Also resets action helper broker, clearing all registered helpers.
164 *
165 * @return void
166 */
167 public function resetInstance()
168 {
169 $reflection = new ReflectionObject($this);
170 foreach ($reflection->getProperties() as $property) {
171 $name = $property->getName();
172 switch ($name) {
173 case '_instance':
174 break;
175 case '_controllerDir':
176 case '_invokeParams':
177 $this->{$name} = array();
178 break;
179 case '_plugins':
180 $this->{$name} = new Zend_Controller_Plugin_Broker();
181 break;
182 case '_throwExceptions':
183 case '_returnResponse':
184 $this->{$name} = false;
185 break;
186 case '_moduleControllerDirectoryName':
187 $this->{$name} = 'controllers';
188 break;
189 default:
190 $this->{$name} = null;
191 break;
192 }
193 }
194 Zend_Controller_Action_HelperBroker::resetHelpers();
195 }
196
197 /**
198 * Convenience feature, calls setControllerDirectory()->setRouter()->dispatch()
199 *
200 * In PHP 5.1.x, a call to a static method never populates $this -- so run()
201 * may actually be called after setting up your front controller.
202 *
203 * @param string|array $controllerDirectory Path to Zend_Controller_Action
204 * controller classes or array of such paths
205 * @return void
206 * @throws Zend_Controller_Exception if called from an object instance
207 */
208 public static function run($controllerDirectory)
209 {
210 self::getInstance()
211 ->setControllerDirectory($controllerDirectory)
212 ->dispatch();
213 }
214
215 /**
216 * Add a controller directory to the controller directory stack
217 *
218 * If $args is presented and is a string, uses it for the array key mapping
219 * to the directory specified.
220 *
221 * @param string $directory
222 * @param string $module Optional argument; module with which to associate directory. If none provided, assumes 'default'
223 * @return Zend_Controller_Front
224 * @throws Zend_Controller_Exception if directory not found or readable
225 */
226 public function addControllerDirectory($directory, $module = null)
227 {
228 $this->getDispatcher()->addControllerDirectory($directory, $module);
229 return $this;
230 }
231
232 /**
233 * Set controller directory
234 *
235 * Stores controller directory(ies) in dispatcher. May be an array of
236 * directories or a string containing a single directory.
237 *
238 * @param string|array $directory Path to Zend_Controller_Action controller
239 * classes or array of such paths
240 * @param string $module Optional module name to use with string $directory
241 * @return Zend_Controller_Front
242 */
243 public function setControllerDirectory($directory, $module = null)
244 {
245 $this->getDispatcher()->setControllerDirectory($directory, $module);
246 return $this;
247 }
248
249 /**
250 * Retrieve controller directory
251 *
252 * Retrieves:
253 * - Array of all controller directories if no $name passed
254 * - String path if $name passed and exists as a key in controller directory array
255 * - null if $name passed but does not exist in controller directory keys
256 *
257 * @param string $name Default null
258 * @return array|string|null
259 */
260 public function getControllerDirectory($name = null)
261 {
262 return $this->getDispatcher()->getControllerDirectory($name);
263 }
264
265 /**
266 * Remove a controller directory by module name
267 *
268 * @param string $module
269 * @return bool
270 */
271 public function removeControllerDirectory($module)
272 {
273 return $this->getDispatcher()->removeControllerDirectory($module);
274 }
275
276 /**
277 * Specify a directory as containing modules
278 *
279 * Iterates through the directory, adding any subdirectories as modules;
280 * the subdirectory within each module named after {@link $_moduleControllerDirectoryName}
281 * will be used as the controller directory path.
282 *
283 * @param string $path
284 * @return Zend_Controller_Front
285 */
286 public function addModuleDirectory($path)
287 {
288 try{
289 $dir = new DirectoryIterator($path);
290 } catch(Exception $e) {
291 require_once 'Zend/Controller/Exception.php';
292 throw new Zend_Controller_Exception("Directory $path not readable", 0, $e);
293 }
294 foreach ($dir as $file) {
295 if ($file->isDot() || !$file->isDir()) {
296 continue;
297 }
298
299 $module = $file->getFilename();
300
301 // Don't use SCCS directories as modules
302 if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) {
303 continue;
304 }
305
306 $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . $this->getModuleControllerDirectoryName();
307 $this->addControllerDirectory($moduleDir, $module);
308 }
309
310 return $this;
311 }
312
313 /**
314 * Return the path to a module directory (but not the controllers directory within)
315 *
316 * @param string $module
317 * @return string|null
318 */
319 public function getModuleDirectory($module = null)
320 {
321 if (null === $module) {
322 $request = $this->getRequest();
323 if (null !== $request) {
324 $module = $this->getRequest()->getModuleName();
325 }
326 if (empty($module)) {
327 $module = $this->getDispatcher()->getDefaultModule();
328 }
329 }
330
331 $controllerDir = $this->getControllerDirectory($module);
332
333 if ((null === $controllerDir) || !is_string($controllerDir)) {
334 return null;
335 }
336
337 return dirname($controllerDir);
338 }
339
340 /**
341 * Set the directory name within a module containing controllers
342 *
343 * @param string $name
344 * @return Zend_Controller_Front
345 */
346 public function setModuleControllerDirectoryName($name = 'controllers')
347 {
348 $this->_moduleControllerDirectoryName = (string) $name;
349
350 return $this;
351 }
352
353 /**
354 * Return the directory name within a module containing controllers
355 *
356 * @return string
357 */
358 public function getModuleControllerDirectoryName()
359 {
360 return $this->_moduleControllerDirectoryName;
361 }
362
363 /**
364 * Set the default controller (unformatted string)
365 *
366 * @param string $controller
367 * @return Zend_Controller_Front
368 */
369 public function setDefaultControllerName($controller)
370 {
371 $dispatcher = $this->getDispatcher();
372 $dispatcher->setDefaultControllerName($controller);
373 return $this;
374 }
375
376 /**
377 * Retrieve the default controller (unformatted string)
378 *
379 * @return string
380 */
381 public function getDefaultControllerName()
382 {
383 return $this->getDispatcher()->getDefaultControllerName();
384 }
385
386 /**
387 * Set the default action (unformatted string)
388 *
389 * @param string $action
390 * @return Zend_Controller_Front
391 */
392 public function setDefaultAction($action)
393 {
394 $dispatcher = $this->getDispatcher();
395 $dispatcher->setDefaultAction($action);
396 return $this;
397 }
398
399 /**
400 * Retrieve the default action (unformatted string)
401 *
402 * @return string
403 */
404 public function getDefaultAction()
405 {
406 return $this->getDispatcher()->getDefaultAction();
407 }
408
409 /**
410 * Set the default module name
411 *
412 * @param string $module
413 * @return Zend_Controller_Front
414 */
415 public function setDefaultModule($module)
416 {
417 $dispatcher = $this->getDispatcher();
418 $dispatcher->setDefaultModule($module);
419 return $this;
420 }
421
422 /**
423 * Retrieve the default module
424 *
425 * @return string
426 */
427 public function getDefaultModule()
428 {
429 return $this->getDispatcher()->getDefaultModule();
430 }
431
432 /**
433 * Set request class/object
434 *
435 * Set the request object. The request holds the request environment.
436 *
437 * If a class name is provided, it will instantiate it
438 *
439 * @param string|Zend_Controller_Request_Abstract $request
440 * @throws Zend_Controller_Exception if invalid request class
441 * @return Zend_Controller_Front
442 */
443 public function setRequest($request)
444 {
445 if (is_string($request)) {
446 if (!class_exists($request)) {
447 require_once 'Zend/Loader.php';
448 Zend_Loader::loadClass($request);
449 }
450 $request = new $request();
451 }
452 if (!$request instanceof Zend_Controller_Request_Abstract) {
453 require_once 'Zend/Controller/Exception.php';
454 throw new Zend_Controller_Exception('Invalid request class');
455 }
456
457 $this->_request = $request;
458
459 return $this;
460 }
461
462 /**
463 * Return the request object.
464 *
465 * @return null|Zend_Controller_Request_Abstract
466 */
467 public function getRequest()
468 {
469 return $this->_request;
470 }
471
472 /**
473 * Set router class/object
474 *
475 * Set the router object. The router is responsible for mapping
476 * the request to a controller and action.
477 *
478 * If a class name is provided, instantiates router with any parameters
479 * registered via {@link setParam()} or {@link setParams()}.
480 *
481 * @param string|Zend_Controller_Router_Interface $router
482 * @throws Zend_Controller_Exception if invalid router class
483 * @return Zend_Controller_Front
484 */
485 public function setRouter($router)
486 {
487 if (is_string($router)) {
488 if (!class_exists($router)) {
489 require_once 'Zend/Loader.php';
490 Zend_Loader::loadClass($router);
491 }
492 $router = new $router();
493 }
494
495 if (!$router instanceof Zend_Controller_Router_Interface) {
496 require_once 'Zend/Controller/Exception.php';
497 throw new Zend_Controller_Exception('Invalid router class');
498 }
499
500 $router->setFrontController($this);
501 $this->_router = $router;
502
503 return $this;
504 }
505
506 /**
507 * Return the router object.
508 *
509 * Instantiates a Zend_Controller_Router_Rewrite object if no router currently set.
510 *
511 * @return Zend_Controller_Router_Interface
512 */
513 public function getRouter()
514 {
515 if (null == $this->_router) {
516 require_once 'Zend/Controller/Router/Rewrite.php';
517 $this->setRouter(new Zend_Controller_Router_Rewrite());
518 }
519
520 return $this->_router;
521 }
522
523 /**
524 * Set the base URL used for requests
525 *
526 * Use to set the base URL segment of the REQUEST_URI to use when
527 * determining PATH_INFO, etc. Examples:
528 * - /admin
529 * - /myapp
530 * - /subdir/index.php
531 *
532 * Note that the URL should not include the full URI. Do not use:
533 * - http://example.com/admin
534 * - http://example.com/myapp
535 * - http://example.com/subdir/index.php
536 *
537 * If a null value is passed, this can be used as well for autodiscovery (default).
538 *
539 * @param string $base
540 * @return Zend_Controller_Front
541 * @throws Zend_Controller_Exception for non-string $base
542 */
543 public function setBaseUrl($base = null)
544 {
545 if (!is_string($base) && (null !== $base)) {
546 require_once 'Zend/Controller/Exception.php';
547 throw new Zend_Controller_Exception('Rewrite base must be a string');
548 }
549
550 $this->_baseUrl = $base;
551
552 if ((null !== ($request = $this->getRequest())) && (method_exists($request, 'setBaseUrl'))) {
553 $request->setBaseUrl($base);
554 }
555
556 return $this;
557 }
558
559 /**
560 * Retrieve the currently set base URL
561 *
562 * @return string
563 */
564 public function getBaseUrl()
565 {
566 $request = $this->getRequest();
567 if ((null !== $request) && method_exists($request, 'getBaseUrl')) {
568 return $request->getBaseUrl();
569 }
570
571 return $this->_baseUrl;
572 }
573
574 /**
575 * Set the dispatcher object. The dispatcher is responsible for
576 * taking a Zend_Controller_Dispatcher_Token object, instantiating the controller, and
577 * call the action method of the controller.
578 *
579 * @param Zend_Controller_Dispatcher_Interface $dispatcher
580 * @return Zend_Controller_Front
581 */
582 public function setDispatcher(Zend_Controller_Dispatcher_Interface $dispatcher)
583 {
584 $this->_dispatcher = $dispatcher;
585 return $this;
586 }
587
588 /**
589 * Return the dispatcher object.
590 *
591 * @return Zend_Controller_Dispatcher_Interface
592 */
593 public function getDispatcher()
594 {
595 /**
596 * Instantiate the default dispatcher if one was not set.
597 */
598 if (!$this->_dispatcher instanceof Zend_Controller_Dispatcher_Interface) {
599 require_once 'Zend/Controller/Dispatcher/Standard.php';
600 $this->_dispatcher = new Zend_Controller_Dispatcher_Standard();
601 }
602 return $this->_dispatcher;
603 }
604
605 /**
606 * Set response class/object
607 *
608 * Set the response object. The response is a container for action
609 * responses and headers. Usage is optional.
610 *
611 * If a class name is provided, instantiates a response object.
612 *
613 * @param string|Zend_Controller_Response_Abstract $response
614 * @throws Zend_Controller_Exception if invalid response class
615 * @return Zend_Controller_Front
616 */
617 public function setResponse($response)
618 {
619 if (is_string($response)) {
620 if (!class_exists($response)) {
621 require_once 'Zend/Loader.php';
622 Zend_Loader::loadClass($response);
623 }
624 $response = new $response();
625 }
626 if (!$response instanceof Zend_Controller_Response_Abstract) {
627 require_once 'Zend/Controller/Exception.php';
628 throw new Zend_Controller_Exception('Invalid response class');
629 }
630
631 $this->_response = $response;
632
633 return $this;
634 }
635
636 /**
637 * Return the response object.
638 *
639 * @return null|Zend_Controller_Response_Abstract
640 */
641 public function getResponse()
642 {
643 return $this->_response;
644 }
645
646 /**
647 * Add or modify a parameter to use when instantiating an action controller
648 *
649 * @param string $name
650 * @param mixed $value
651 * @return Zend_Controller_Front
652 */
653 public function setParam($name, $value)
654 {
655 $name = (string) $name;
656 $this->_invokeParams[$name] = $value;
657 return $this;
658 }
659
660 /**
661 * Set parameters to pass to action controller constructors
662 *
663 * @param array $params
664 * @return Zend_Controller_Front
665 */
666 public function setParams(array $params)
667 {
668 $this->_invokeParams = array_merge($this->_invokeParams, $params);
669 return $this;
670 }
671
672 /**
673 * Retrieve a single parameter from the controller parameter stack
674 *
675 * @param string $name
676 * @return mixed
677 */
678 public function getParam($name)
679 {
680 if(isset($this->_invokeParams[$name])) {
681 return $this->_invokeParams[$name];
682 }
683
684 return null;
685 }
686
687 /**
688 * Retrieve action controller instantiation parameters
689 *
690 * @return array
691 */
692 public function getParams()
693 {
694 return $this->_invokeParams;
695 }
696
697 /**
698 * Clear the controller parameter stack
699 *
700 * By default, clears all parameters. If a parameter name is given, clears
701 * only that parameter; if an array of parameter names is provided, clears
702 * each.
703 *
704 * @param null|string|array single key or array of keys for params to clear
705 * @return Zend_Controller_Front
706 */
707 public function clearParams($name = null)
708 {
709 if (null === $name) {
710 $this->_invokeParams = array();
711 } elseif (is_string($name) && isset($this->_invokeParams[$name])) {
712 unset($this->_invokeParams[$name]);
713 } elseif (is_array($name)) {
714 foreach ($name as $key) {
715 if (is_string($key) && isset($this->_invokeParams[$key])) {
716 unset($this->_invokeParams[$key]);
717 }
718 }
719 }
720
721 return $this;
722 }
723
724 /**
725 * Register a plugin.
726 *
727 * @param Zend_Controller_Plugin_Abstract $plugin
728 * @param int $stackIndex Optional; stack index for plugin
729 * @return Zend_Controller_Front
730 */
731 public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)
732 {
733 $this->_plugins->registerPlugin($plugin, $stackIndex);
734 return $this;
735 }
736
737 /**
738 * Unregister a plugin.
739 *
740 * @param string|Zend_Controller_Plugin_Abstract $plugin Plugin class or object to unregister
741 * @return Zend_Controller_Front
742 */
743 public function unregisterPlugin($plugin)
744 {
745 $this->_plugins->unregisterPlugin($plugin);
746 return $this;
747 }
748
749 /**
750 * Is a particular plugin registered?
751 *
752 * @param string $class
753 * @return bool
754 */
755 public function hasPlugin($class)
756 {
757 return $this->_plugins->hasPlugin($class);
758 }
759
760 /**
761 * Retrieve a plugin or plugins by class
762 *
763 * @param string $class
764 * @return false|Zend_Controller_Plugin_Abstract|array
765 */
766 public function getPlugin($class)
767 {
768 return $this->_plugins->getPlugin($class);
769 }
770
771 /**
772 * Retrieve all plugins
773 *
774 * @return array
775 */
776 public function getPlugins()
777 {
778 return $this->_plugins->getPlugins();
779 }
780
781 /**
782 * Set the throwExceptions flag and retrieve current status
783 *
784 * Set whether exceptions encounted in the dispatch loop should be thrown
785 * or caught and trapped in the response object.
786 *
787 * Default behaviour is to trap them in the response object; call this
788 * method to have them thrown.
789 *
790 * Passing no value will return the current value of the flag; passing a
791 * boolean true or false value will set the flag and return the current
792 * object instance.
793 *
794 * @param boolean $flag Defaults to null (return flag state)
795 * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
796 */
797 public function throwExceptions($flag = null)
798 {
799 if ($flag !== null) {
800 $this->_throwExceptions = (bool) $flag;
801 return $this;
802 }
803
804 return $this->_throwExceptions;
805 }
806
807 /**
808 * Set whether {@link dispatch()} should return the response without first
809 * rendering output. By default, output is rendered and dispatch() returns
810 * nothing.
811 *
812 * @param boolean $flag
813 * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
814 */
815 public function returnResponse($flag = null)
816 {
817 if (true === $flag) {
818 $this->_returnResponse = true;
819 return $this;
820 } elseif (false === $flag) {
821 $this->_returnResponse = false;
822 return $this;
823 }
824
825 return $this->_returnResponse;
826 }
827
828 /**
829 * Dispatch an HTTP request to a controller/action.
830 *
831 * @param Zend_Controller_Request_Abstract|null $request
832 * @param Zend_Controller_Response_Abstract|null $response
833 * @return void|Zend_Controller_Response_Abstract Returns response object if returnResponse() is true
834 */
835 public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
836 {
837 if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) {
838 // Register with stack index of 100
839 require_once 'Zend/Controller/Plugin/ErrorHandler.php';
840 $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
841 }
842
843 if (!$this->getParam('noViewRenderer') && !Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
844 require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
845 Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-80, new Zend_Controller_Action_Helper_ViewRenderer());
846 }
847
848 /**
849 * Instantiate default request object (HTTP version) if none provided
850 */
851 if (null !== $request) {
852 $this->setRequest($request);
853 } elseif ((null === $request) && (null === ($request = $this->getRequest()))) {
854 require_once 'Zend/Controller/Request/Http.php';
855 $request = new Zend_Controller_Request_Http();
856 $this->setRequest($request);
857 }
858
859 /**
860 * Set base URL of request object, if available
861 */
862 if (is_callable(array($this->_request, 'setBaseUrl'))) {
863 if (null !== $this->_baseUrl) {
864 $this->_request->setBaseUrl($this->_baseUrl);
865 }
866 }
867
868 /**
869 * Instantiate default response object (HTTP version) if none provided
870 */
871 if (null !== $response) {
872 $this->setResponse($response);
873 } elseif ((null === $this->_response) && (null === ($this->_response = $this->getResponse()))) {
874 require_once 'Zend/Controller/Response/Http.php';
875 $response = new Zend_Controller_Response_Http();
876 $this->setResponse($response);
877 }
878
879 /**
880 * Register request and response objects with plugin broker
881 */
882 $this->_plugins
883 ->setRequest($this->_request)
884 ->setResponse($this->_response);
885
886 /**
887 * Initialize router
888 */
889 $router = $this->getRouter();
890 $router->setParams($this->getParams());
891
892 /**
893 * Initialize dispatcher
894 */
895 $dispatcher = $this->getDispatcher();
896 $dispatcher->setParams($this->getParams())
897 ->setResponse($this->_response);
898
899 // Begin dispatch
900 try {
901 /**
902 * Route request to controller/action, if a router is provided
903 */
904
905 /**
906 * Notify plugins of router startup
907 */
908 $this->_plugins->routeStartup($this->_request);
909
910 try {
911 $router->route($this->_request);
912 } catch (Exception $e) {
913 if ($this->throwExceptions()) {
914 throw $e;
915 }
916
917 $this->_response->setException($e);
918 }
919
920 /**
921 * Notify plugins of router completion
922 */
923 $this->_plugins->routeShutdown($this->_request);
924
925 /**
926 * Notify plugins of dispatch loop startup
927 */
928 $this->_plugins->dispatchLoopStartup($this->_request);
929
930 /**
931 * Attempt to dispatch the controller/action. If the $this->_request
932 * indicates that it needs to be dispatched, move to the next
933 * action in the request.
934 */
935 do {
936 $this->_request->setDispatched(true);
937
938 /**
939 * Notify plugins of dispatch startup
940 */
941 $this->_plugins->preDispatch($this->_request);
942
943 /**
944 * Skip requested action if preDispatch() has reset it
945 */
946 if (!$this->_request->isDispatched()) {
947 continue;
948 }
949
950 /**
951 * Dispatch request
952 */
953 try {
954 $dispatcher->dispatch($this->_request, $this->_response);
955 } catch (Exception $e) {
956 if ($this->throwExceptions()) {
957 throw $e;
958 }
959 $this->_response->setException($e);
960 }
961
962 /**
963 * Notify plugins of dispatch completion
964 */
965 $this->_plugins->postDispatch($this->_request);
966 } while (!$this->_request->isDispatched());
967 } catch (Exception $e) {
968 if ($this->throwExceptions()) {
969 throw $e;
970 }
971
972 $this->_response->setException($e);
973 }
974
975 /**
976 * Notify plugins of dispatch loop completion
977 */
978 try {
979 $this->_plugins->dispatchLoopShutdown();
980 } catch (Exception $e) {
981 if ($this->throwExceptions()) {
982 throw $e;
983 }
984
985 $this->_response->setException($e);
986 }
987
988 if ($this->returnResponse()) {
989 return $this->_response;
990 }
991
992 $this->_response->sendResponse();
993 }
994 }
995
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Application/Bootstrap/Bootstrap.php
Zend_Controller_Front->dispatch()
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_Application
17 * @subpackage Bootstrap
18 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
21 */
22
23 /**
24 * @see Zend_Application_Bootstrap_BootstrapAbstract
25 */
26 require_once 'Zend/Application/Bootstrap/BootstrapAbstract.php';
27
28 /**
29 * Concrete base class for bootstrap classes
30 *
31 * Registers and utilizes Zend_Controller_Front by default.
32 *
33 * @uses Zend_Application_Bootstrap_Bootstrap
34 * @category Zend
35 * @package Zend_Application
36 * @subpackage Bootstrap
37 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
38 * @license http://framework.zend.com/license/new-bsd New BSD License
39 */
40 class Zend_Application_Bootstrap_Bootstrap
41 extends Zend_Application_Bootstrap_BootstrapAbstract
42 {
43 /**
44 * Application resource namespace
45 * @var false|string
46 */
47 protected $_appNamespace = false;
48
49 /**
50 * Application resource autoloader
51 * @var Zend_Loader_Autoloader_Resource
52 */
53 protected $_resourceLoader;
54
55 /**
56 * Constructor
57 *
58 * Ensure FrontController resource is registered
59 *
60 * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
61 */
62 public function __construct($application)
63 {
64 parent::__construct($application);
65
66 if ($application->hasOption('resourceloader')) {
67 $this->setOptions(
68 array(
69 'resourceloader' => $application->getOption(
70 'resourceloader'
71 )
72 )
73 );
74 }
75 $this->getResourceLoader();
76
77 if (!$this->hasPluginResource('FrontController')) {
78 $this->registerPluginResource('FrontController');
79 }
80 }
81
82 /**
83 * Run the application
84 *
85 * Checks to see that we have a default controller directory. If not, an
86 * exception is thrown.
87 *
88 * If so, it registers the bootstrap with the 'bootstrap' parameter of
89 * the front controller, and dispatches the front controller.
90 *
91 * @return mixed
92 * @throws Zend_Application_Bootstrap_Exception
93 */
94 public function run()
95 {
96 $front = $this->getResource('FrontController');
97 $default = $front->getDefaultModule();
98 if (null === $front->getControllerDirectory($default)) {
99 throw new Zend_Application_Bootstrap_Exception(
100 'No default controller directory registered with front controller'
101 );
102 }
103
104 $front->setParam('bootstrap', $this);
105 $response = $front->dispatch();
106 if ($front->returnResponse()) {
107 return $response;
108 }
109 }
110
111 /**
112 * Set module resource loader
113 *
114 * @param Zend_Loader_Autoloader_Resource $loader
115 * @return Zend_Application_Module_Bootstrap
116 */
117 public function setResourceLoader(Zend_Loader_Autoloader_Resource $loader)
118 {
119 $this->_resourceLoader = $loader;
120 return $this;
121 }
122
123 /**
124 * Retrieve module resource loader
125 *
126 * @return Zend_Loader_Autoloader_Resource
127 */
128 public function getResourceLoader()
129 {
130 if ((null === $this->_resourceLoader)
131 && (false !== ($namespace = $this->getAppNamespace()))
132 ) {
133 $r = new ReflectionClass($this);
134 $path = $r->getFileName();
135 $this->setResourceLoader(
136 new Zend_Application_Module_Autoloader(
137 array(
138 'namespace' => $namespace,
139 'basePath' => dirname($path),
140 )
141 )
142 );
143 }
144 return $this->_resourceLoader;
145 }
146
147 /**
148 * Get application namespace (used for module autoloading)
149 *
150 * @return string
151 */
152 public function getAppNamespace()
153 {
154 return $this->_appNamespace;
155 }
156
157 /**
158 * Set application namespace (for module autoloading)
159 *
160 * @param string
161 * @return Zend_Application_Bootstrap_Bootstrap
162 */
163 public function setAppNamespace($value)
164 {
165 $this->_appNamespace = (string) $value;
166 return $this;
167 }
168 }
169
-
open/home/peruvian/subdomains/m.peruvianvip.com/vendor/zendframework/zendframework1/library/Zend/Application.php
Zend_Application_Bootstrap_Bootstrap->run()
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_Application
17 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
20 */
21
22 /**
23 * @category Zend
24 * @package Zend_Application
25 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
26 * @license http://framework.zend.com/license/new-bsd New BSD License
27 */
28 class Zend_Application
29 {
30 /**
31 * Autoloader to use
32 *
33 * @var Zend_Loader_Autoloader
34 */
35 protected $_autoloader;
36
37 /**
38 * Bootstrap
39 *
40 * @var Zend_Application_Bootstrap_BootstrapAbstract
41 */
42 protected $_bootstrap;
43
44 /**
45 * Application environment
46 *
47 * @var string
48 */
49 protected $_environment;
50
51 /**
52 * Flattened (lowercase) option keys
53 *
54 * @var array
55 */
56 protected $_optionKeys = array();
57
58 /**
59 * Options for Zend_Application
60 *
61 * @var array
62 */
63 protected $_options = array();
64
65 /**
66 * Constructor
67 *
68 * Initialize application. Potentially initializes include_paths, PHP
69 * settings, and bootstrap class.
70 *
71 * @param string $environment
72 * @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
73 * @throws Zend_Application_Exception When invalid options are provided
74 * @return void
75 */
76 public function __construct($environment, $options = null)
77 {
78 $this->_environment = (string) $environment;
79
80 require_once 'Zend/Loader/Autoloader.php';
81 $this->_autoloader = Zend_Loader_Autoloader::getInstance();
82
83 if (null !== $options) {
84 if (is_string($options)) {
85 $options = $this->_loadConfig($options);
86 } elseif ($options instanceof Zend_Config) {
87 $options = $options->toArray();
88 } elseif (!is_array($options)) {
89 throw new Zend_Application_Exception(
90 'Invalid options provided; must be location of config file,'
91 . ' a config object, or an array'
92 );
93 }
94
95 $this->setOptions($options);
96 }
97 }
98
99 /**
100 * Retrieve current environment
101 *
102 * @return string
103 */
104 public function getEnvironment()
105 {
106 return $this->_environment;
107 }
108
109 /**
110 * Retrieve autoloader instance
111 *
112 * @return Zend_Loader_Autoloader
113 */
114 public function getAutoloader()
115 {
116 return $this->_autoloader;
117 }
118
119 /**
120 * Set application options
121 *
122 * @param array $options
123 * @throws Zend_Application_Exception When no bootstrap path is provided
124 * @throws Zend_Application_Exception When invalid bootstrap information are provided
125 * @return Zend_Application
126 */
127 public function setOptions(array $options)
128 {
129 if (!empty($options['config'])) {
130 if (is_array($options['config'])) {
131 $_options = array();
132 foreach ($options['config'] as $tmp) {
133 $_options = $this->mergeOptions(
134 $_options, $this->_loadConfig($tmp)
135 );
136 }
137 $options = $this->mergeOptions($_options, $options);
138 } else {
139 $options = $this->mergeOptions(
140 $this->_loadConfig($options['config']), $options
141 );
142 }
143 }
144
145 $this->_options = $options;
146
147 $options = array_change_key_case($options, CASE_LOWER);
148
149 $this->_optionKeys = array_keys($options);
150
151 if (!empty($options['phpsettings'])) {
152 $this->setPhpSettings($options['phpsettings']);
153 }
154
155 if (!empty($options['includepaths'])) {
156 $this->setIncludePaths($options['includepaths']);
157 }
158
159 if (!empty($options['autoloadernamespaces'])) {
160 $this->setAutoloaderNamespaces($options['autoloadernamespaces']);
161 }
162
163 if (!empty($options['autoloaderzfpath'])) {
164 $autoloader = $this->getAutoloader();
165 if (method_exists($autoloader, 'setZfPath')) {
166 $zfPath = $options['autoloaderzfpath'];
167 $zfVersion = !empty($options['autoloaderzfversion'])
168 ? $options['autoloaderzfversion']
169 : 'latest';
170 $autoloader->setZfPath($zfPath, $zfVersion);
171 }
172 }
173
174 if (!empty($options['bootstrap'])) {
175 $bootstrap = $options['bootstrap'];
176
177 if (is_string($bootstrap)) {
178 $this->setBootstrap($bootstrap);
179 } elseif (is_array($bootstrap)) {
180 if (empty($bootstrap['path'])) {
181 throw new Zend_Application_Exception(
182 'No bootstrap path provided'
183 );
184 }
185
186 $path = $bootstrap['path'];
187 $class = null;
188
189 if (!empty($bootstrap['class'])) {
190 $class = $bootstrap['class'];
191 }
192
193 $this->setBootstrap($path, $class);
194 } else {
195 throw new Zend_Application_Exception(
196 'Invalid bootstrap information provided'
197 );
198 }
199 }
200
201 return $this;
202 }
203
204 /**
205 * Retrieve application options (for caching)
206 *
207 * @return array
208 */
209 public function getOptions()
210 {
211 return $this->_options;
212 }
213
214 /**
215 * Is an option present?
216 *
217 * @param string $key
218 * @return bool
219 */
220 public function hasOption($key)
221 {
222 return in_array(strtolower($key), $this->_optionKeys);
223 }
224
225 /**
226 * Retrieve a single option
227 *
228 * @param string $key
229 * @return mixed
230 */
231 public function getOption($key)
232 {
233 if ($this->hasOption($key)) {
234 $options = $this->getOptions();
235 $options = array_change_key_case($options, CASE_LOWER);
236 return $options[strtolower($key)];
237 }
238 return null;
239 }
240
241 /**
242 * Merge options recursively
243 *
244 * @param array $array1
245 * @param mixed $array2
246 * @return array
247 */
248 public function mergeOptions(array $array1, $array2 = null)
249 {
250 if (is_array($array2)) {
251 foreach ($array2 as $key => $val) {
252 if (is_array($array2[$key])) {
253 $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
254 ? $this->mergeOptions($array1[$key], $array2[$key])
255 : $array2[$key];
256 } else {
257 $array1[$key] = $val;
258 }
259 }
260 }
261 return $array1;
262 }
263
264 /**
265 * Set PHP configuration settings
266 *
267 * @param array $settings
268 * @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)
269 * @return Zend_Application
270 */
271 public function setPhpSettings(array $settings, $prefix = '')
272 {
273 foreach ($settings as $key => $value) {
274 $key = empty($prefix) ? $key : $prefix . $key;
275 if (is_scalar($value)) {
276 ini_set($key, $value);
277 } elseif (is_array($value)) {
278 $this->setPhpSettings($value, $key . '.');
279 }
280 }
281
282 return $this;
283 }
284
285 /**
286 * Set include path
287 *
288 * @param array $paths
289 * @return Zend_Application
290 */
291 public function setIncludePaths(array $paths)
292 {
293 $path = implode(PATH_SEPARATOR, $paths);
294 set_include_path($path . PATH_SEPARATOR . get_include_path());
295 return $this;
296 }
297
298 /**
299 * Set autoloader namespaces
300 *
301 * @param array $namespaces
302 * @return Zend_Application
303 */
304 public function setAutoloaderNamespaces(array $namespaces)
305 {
306 $autoloader = $this->getAutoloader();
307
308 foreach ($namespaces as $namespace) {
309 $autoloader->registerNamespace($namespace);
310 }
311
312 return $this;
313 }
314
315 /**
316 * Set bootstrap path/class
317 *
318 * @param string $path
319 * @param string $class
320 * @return Zend_Application
321 */
322 public function setBootstrap($path, $class = null)
323 {
324 // setOptions() can potentially send a null value; specify default
325 // here
326 if (null === $class) {
327 $class = 'Bootstrap';
328 }
329
330 if (!class_exists($class, false)) {
331 require_once $path;
332 if (!class_exists($class, false)) {
333 throw new Zend_Application_Exception(
334 'Bootstrap class not found'
335 );
336 }
337 }
338 $this->_bootstrap = new $class($this);
339
340 if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
341 throw new Zend_Application_Exception(
342 'Bootstrap class does not implement'
343 . ' Zend_Application_Bootstrap_Bootstrapper'
344 );
345 }
346
347 return $this;
348 }
349
350 /**
351 * Get bootstrap object
352 *
353 * @return Zend_Application_Bootstrap_BootstrapAbstract
354 */
355 public function getBootstrap()
356 {
357 if (null === $this->_bootstrap) {
358 $this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
359 }
360 return $this->_bootstrap;
361 }
362
363 /**
364 * Bootstrap application
365 *
366 * @param null|string|array $resource
367 * @return Zend_Application
368 */
369 public function bootstrap($resource = null)
370 {
371 $this->getBootstrap()->bootstrap($resource);
372 return $this;
373 }
374
375 /**
376 * Run the application
377 *
378 * @return void
379 */
380 public function run()
381 {
382 $this->getBootstrap()->run();
383 }
384
385 /**
386 * Load configuration file of options
387 *
388 * @param string $file
389 * @throws Zend_Application_Exception When invalid configuration file is provided
390 * @return array
391 */
392 protected function _loadConfig($file)
393 {
394 $environment = $this->getEnvironment();
395 $suffix = pathinfo($file, PATHINFO_EXTENSION);
396 $suffix = ($suffix === 'dist')
397 ? pathinfo(basename($file, ".$suffix"), PATHINFO_EXTENSION)
398 : $suffix;
399
400 switch (strtolower($suffix)) {
401 case 'ini':
402 $config = new Zend_Config_Ini($file, $environment);
403 break;
404
405 case 'xml':
406 $config = new Zend_Config_Xml($file, $environment);
407 break;
408
409 case 'json':
410 $config = new Zend_Config_Json($file, $environment);
411 break;
412
413 case 'yaml':
414 case 'yml':
415 $config = new Zend_Config_Yaml($file, $environment);
416 break;
417
418 case 'php':
419 case 'inc':
420 $config = include $file;
421 if (!is_array($config)) {
422 throw new Zend_Application_Exception(
423 'Invalid configuration file provided; PHP file does not'
424 . ' return array value'
425 );
426 }
427 return $config;
428 break;
429
430 default:
431 throw new Zend_Application_Exception(
432 'Invalid configuration file provided; unknown config type'
433 );
434 }
435
436 return $config->toArray();
437 }
438 }
439
-
open/home/peruvian/subdomains/m.peruvianvip.com/public/index.php
Zend_Application->run()
1
<?php
2 define('DS', DIRECTORY_SEPARATOR);
3 define('PS', PATH_SEPARATOR);
4
5
6 //Define to base directory
7 defined('BASE_PATH')
8 || define('BASE_PATH', realpath(dirname(__FILE__) . DS . '..'));
9
10 // Define path to application directory
11 defined('APPLICATION_PATH')
12 || define('APPLICATION_PATH', BASE_PATH . DS . 'application');
13
14 // Define application domain
15 defined('APPLICATION_DOMAIN')
16 || define('APPLICATION_DOMAIN', $_SERVER['HTTP_HOST']);
17
18 // Define application environment
19 defined('APPLICATION_ENV')
20 || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
21
22 // Ensure library/ is on include_path
23 set_include_path(implode(PS, array(
24 realpath(BASE_PATH . DS . 'library')
25 )));
26
27 // load composer libraries
28 require_once realpath(BASE_PATH . DS . 'vendor/autoload.php');
29
30 /** Zend_Application */
31 require_once 'Core/Init.php';
32
33 // Create application, bootstrap, and run
34 $application = Core_Init::getApplication();
35
36 if (PHP_SAPI !== 'cli') {
37 $application->bootstrap()->run();
38 } else {
39 $application->bootstrap();
40 }
41