CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BakeTask
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeRoute
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbConfigTask
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • FileLog
  • FixtureTask
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • MootoolsEngineHelper
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginShortRoute
  • PluginTask
  • ProjectTask
  • PrototypeEngineHelper
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • Set
  • Shell
  • String
  • TemplateTask
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  1: <?php
  2: /**
  3:  * MySQL layer for DBO
  4:  *
  5:  * PHP versions 4 and 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       cake
 16:  * @subpackage    cake.cake.libs.model.datasources.dbo
 17:  * @since         CakePHP(tm) v 0.10.5.1790
 18:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 19:  */
 20: 
 21: /**
 22:  * Provides common base for MySQL & MySQLi connections
 23:  *
 24:  * @package       cake
 25:  * @subpackage    cake.cake.libs.model.datasources.dbo
 26:  */
 27: class DboMysqlBase extends DboSource {
 28: 
 29: /**
 30:  * Description property.
 31:  *
 32:  * @var string
 33:  */
 34:     var $description = "MySQL DBO Base Driver";
 35: 
 36: /**
 37:  * Start quote
 38:  *
 39:  * @var string
 40:  */
 41:     var $startQuote = "`";
 42: 
 43: /**
 44:  * End quote
 45:  *
 46:  * @var string
 47:  */
 48:     var $endQuote = "`";
 49: 
 50: /**
 51:  * use alias for update and delete. Set to true if version >= 4.1
 52:  *
 53:  * @var boolean
 54:  * @access protected
 55:  */
 56:     var $_useAlias = true;
 57: 
 58: /**
 59:  * Index of basic SQL commands
 60:  *
 61:  * @var array
 62:  * @access protected
 63:  */
 64:     var $_commands = array(
 65:         'begin'    => 'START TRANSACTION',
 66:         'commit'   => 'COMMIT',
 67:         'rollback' => 'ROLLBACK'
 68:     );
 69: 
 70: /**
 71:  * List of engine specific additional field parameters used on table creating
 72:  *
 73:  * @var array
 74:  * @access public
 75:  */
 76:     var $fieldParameters = array(
 77:         'charset' => array('value' => 'CHARACTER SET', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'beforeDefault'),
 78:         'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => ' ', 'column' => 'Collation', 'position' => 'beforeDefault'),
 79:         'comment' => array('value' => 'COMMENT', 'quote' => true, 'join' => ' ', 'column' => 'Comment', 'position' => 'afterDefault')
 80:     );
 81: 
 82: /**
 83:  * List of table engine specific parameters used on table creating
 84:  *
 85:  * @var array
 86:  * @access public
 87:  */
 88:     var $tableParameters = array(
 89:         'charset' => array('value' => 'DEFAULT CHARSET', 'quote' => false, 'join' => '=', 'column' => 'charset'),
 90:         'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => '=', 'column' => 'Collation'),
 91:         'engine' => array('value' => 'ENGINE', 'quote' => false, 'join' => '=', 'column' => 'Engine')
 92:     );
 93: 
 94: /**
 95:  * MySQL column definition
 96:  *
 97:  * @var array
 98:  */
 99:     var $columns = array(
100:         'primary_key' => array('name' => 'NOT NULL AUTO_INCREMENT'),
101:         'string' => array('name' => 'varchar', 'limit' => '255'),
102:         'text' => array('name' => 'text'),
103:         'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
104:         'float' => array('name' => 'float', 'formatter' => 'floatval'),
105:         'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
106:         'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
107:         'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'),
108:         'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
109:         'binary' => array('name' => 'blob'),
110:         'boolean' => array('name' => 'tinyint', 'limit' => '1')
111:     );
112: 
113: /**
114:  * Returns an array of the fields in given table name.
115:  *
116:  * @param string $tableName Name of database table to inspect
117:  * @return array Fields in table. Keys are name and type
118:  */
119:     function describe(&$model) {
120:         $cache = parent::describe($model);
121:         if ($cache != null) {
122:             return $cache;
123:         }
124:         $fields = false;
125:         $cols = $this->query('SHOW FULL COLUMNS FROM ' . $this->fullTableName($model));
126: 
127:         foreach ($cols as $column) {
128:             $colKey = array_keys($column);
129:             if (isset($column[$colKey[0]]) && !isset($column[0])) {
130:                 $column[0] = $column[$colKey[0]];
131:             }
132:             if (isset($column[0])) {
133:                 $fields[$column[0]['Field']] = array(
134:                     'type' => $this->column($column[0]['Type']),
135:                     'null' => ($column[0]['Null'] == 'YES' ? true : false),
136:                     'default' => $column[0]['Default'],
137:                     'length' => $this->length($column[0]['Type']),
138:                 );
139:                 if (!empty($column[0]['Key']) && isset($this->index[$column[0]['Key']])) {
140:                     $fields[$column[0]['Field']]['key'] = $this->index[$column[0]['Key']];
141:                 }
142:                 foreach ($this->fieldParameters as $name => $value) {
143:                     if (!empty($column[0][$value['column']])) {
144:                         $fields[$column[0]['Field']][$name] = $column[0][$value['column']];
145:                     }
146:                 }
147:                 if (isset($fields[$column[0]['Field']]['collate'])) {
148:                     $charset = $this->getCharsetName($fields[$column[0]['Field']]['collate']);
149:                     if ($charset) {
150:                         $fields[$column[0]['Field']]['charset'] = $charset;
151:                     }
152:                 }
153:             }
154:         }
155:         $this->__cacheDescription($this->fullTableName($model, false), $fields);
156:         return $fields;
157:     }
158: 
159: /**
160:  * Generates and executes an SQL UPDATE statement for given model, fields, and values.
161:  *
162:  * @param Model $model
163:  * @param array $fields
164:  * @param array $values
165:  * @param mixed $conditions
166:  * @return array
167:  */
168:     function update(&$model, $fields = array(), $values = null, $conditions = null) {
169:         if (!$this->_useAlias) {
170:             return parent::update($model, $fields, $values, $conditions);
171:         }
172: 
173:         if ($values == null) {
174:             $combined = $fields;
175:         } else {
176:             $combined = array_combine($fields, $values);
177:         }
178: 
179:         $alias = $joins = false;
180:         $fields = $this->_prepareUpdateFields($model, $combined, empty($conditions), !empty($conditions));
181:         $fields = implode(', ', $fields);
182:         $table = $this->fullTableName($model);
183: 
184:         if (!empty($conditions)) {
185:             $alias = $this->name($model->alias);
186:             if ($model->name == $model->alias) {
187:                 $joins = implode(' ', $this->_getJoins($model));
188:             }
189:         }
190:         $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
191: 
192:         if ($conditions === false) {
193:             return false;
194:         }
195: 
196:         if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) {
197:             $model->onError();
198:             return false;
199:         }
200:         return true;
201:     }
202: 
203: /**
204:  * Generates and executes an SQL DELETE statement for given id/conditions on given model.
205:  *
206:  * @param Model $model
207:  * @param mixed $conditions
208:  * @return boolean Success
209:  */
210:     function delete(&$model, $conditions = null) {
211:         if (!$this->_useAlias) {
212:             return parent::delete($model, $conditions);
213:         }
214:         $alias = $this->name($model->alias);
215:         $table = $this->fullTableName($model);
216:         $joins = implode(' ', $this->_getJoins($model));
217: 
218:         if (empty($conditions)) {
219:             $alias = $joins = false;
220:         }
221:         $complexConditions = false;
222:         foreach ((array)$conditions as $key => $value) {
223:             if (strpos($key, $model->alias) === false) {
224:                 $complexConditions = true;
225:                 break;
226:             }
227:         }
228:         if (!$complexConditions) {
229:             $joins = false;
230:         }
231: 
232:         $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model);
233:         if ($conditions === false) {
234:             return false;
235:         }
236:         if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
237:             $model->onError();
238:             return false;
239:         }
240:         return true;
241:     }
242: 
243: /**
244:  * Sets the database encoding
245:  *
246:  * @param string $enc Database encoding
247:  */
248:     function setEncoding($enc) {
249:         return $this->_execute('SET NAMES ' . $enc) != false;
250:     }
251: 
252: /**
253:  * Returns an array of the indexes in given datasource name.
254:  *
255:  * @param string $model Name of model to inspect
256:  * @return array Fields in table. Keys are column and unique
257:  */
258:     function index($model) {
259:         $index = array();
260:         $table = $this->fullTableName($model);
261:         if ($table) {
262:             $indexes = $this->query('SHOW INDEX FROM ' . $table);
263:             if (isset($indexes[0]['STATISTICS'])) {
264:                 $keys = Set::extract($indexes, '{n}.STATISTICS');
265:             } else {
266:                 $keys = Set::extract($indexes, '{n}.0');
267:             }
268:             foreach ($keys as $i => $key) {
269:                 if (!isset($index[$key['Key_name']])) {
270:                     $col = array();
271:                     $index[$key['Key_name']]['column'] = $key['Column_name'];
272:                     $index[$key['Key_name']]['unique'] = intval($key['Non_unique'] == 0);
273:                 } else {
274:                     if (!is_array($index[$key['Key_name']]['column'])) {
275:                         $col[] = $index[$key['Key_name']]['column'];
276:                     }
277:                     $col[] = $key['Column_name'];
278:                     $index[$key['Key_name']]['column'] = $col;
279:                 }
280:             }
281:         }
282:         return $index;
283:     }
284: 
285: /**
286:  * Generate a MySQL Alter Table syntax for the given Schema comparison
287:  *
288:  * @param array $compare Result of a CakeSchema::compare()
289:  * @return array Array of alter statements to make.
290:  */
291:     function alterSchema($compare, $table = null) {
292:         if (!is_array($compare)) {
293:             return false;
294:         }
295:         $out = '';
296:         $colList = array();
297:         foreach ($compare as $curTable => $types) {
298:             $indexes = $tableParameters = $colList = array();
299:             if (!$table || $table == $curTable) {
300:                 $out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
301:                 foreach ($types as $type => $column) {
302:                     if (isset($column['indexes'])) {
303:                         $indexes[$type] = $column['indexes'];
304:                         unset($column['indexes']);
305:                     }
306:                     if (isset($column['tableParameters'])) {
307:                         $tableParameters[$type] = $column['tableParameters'];
308:                         unset($column['tableParameters']);
309:                     }
310:                     switch ($type) {
311:                         case 'add':
312:                             foreach ($column as $field => $col) {
313:                                 $col['name'] = $field;
314:                                 $alter = 'ADD ' . $this->buildColumn($col);
315:                                 if (isset($col['after'])) {
316:                                     $alter .= ' AFTER ' . $this->name($col['after']);
317:                                 }
318:                                 $colList[] = $alter;
319:                             }
320:                         break;
321:                         case 'drop':
322:                             foreach ($column as $field => $col) {
323:                                 $col['name'] = $field;
324:                                 $colList[] = 'DROP ' . $this->name($field);
325:                             }
326:                         break;
327:                         case 'change':
328:                             foreach ($column as $field => $col) {
329:                                 if (!isset($col['name'])) {
330:                                     $col['name'] = $field;
331:                                 }
332:                                 $colList[] = 'CHANGE ' . $this->name($field) . ' ' . $this->buildColumn($col);
333:                             }
334:                         break;
335:                     }
336:                 }
337:                 $colList = array_merge($colList, $this->_alterIndexes($curTable, $indexes));
338:                 $colList = array_merge($colList, $this->_alterTableParameters($curTable, $tableParameters));
339:                 $out .= "\t" . join(",\n\t", $colList) . ";\n\n";
340:             }
341:         }
342:         return $out;
343:     }
344: 
345: /**
346:  * Generate a MySQL "drop table" statement for the given Schema object
347:  *
348:  * @param object $schema An instance of a subclass of CakeSchema
349:  * @param string $table Optional.  If specified only the table name given will be generated.
350:  *                      Otherwise, all tables defined in the schema are generated.
351:  * @return string
352:  */
353:     function dropSchema($schema, $table = null) {
354:         if (!is_a($schema, 'CakeSchema')) {
355:             trigger_error(__('Invalid schema object', true), E_USER_WARNING);
356:             return null;
357:         }
358:         $out = '';
359:         foreach ($schema->tables as $curTable => $columns) {
360:             if (!$table || $table == $curTable) {
361:                 $out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n";
362:             }
363:         }
364:         return $out;
365:     }
366: 
367: /**
368:  * Generate MySQL table parameter alteration statementes for a table.
369:  *
370:  * @param string $table Table to alter parameters for.
371:  * @param array $parameters Parameters to add & drop.
372:  * @return array Array of table property alteration statementes.
373:  * @todo Implement this method.
374:  */
375:     function _alterTableParameters($table, $parameters) {
376:         if (isset($parameters['change'])) {
377:             return $this->buildTableParameters($parameters['change']);
378:         }
379:         return array();
380:     }
381: 
382: /**
383:  * Generate MySQL index alteration statements for a table.
384:  *
385:  * @param string $table Table to alter indexes for
386:  * @param array $new Indexes to add and drop
387:  * @return array Index alteration statements
388:  */
389:     function _alterIndexes($table, $indexes) {
390:         $alter = array();
391:         if (isset($indexes['drop'])) {
392:             foreach($indexes['drop'] as $name => $value) {
393:                 $out = 'DROP ';
394:                 if ($name == 'PRIMARY') {
395:                     $out .= 'PRIMARY KEY';
396:                 } else {
397:                     $out .= 'KEY ' . $name;
398:                 }
399:                 $alter[] = $out;
400:             }
401:         }
402:         if (isset($indexes['add'])) {
403:             foreach ($indexes['add'] as $name => $value) {
404:                 $out = 'ADD ';
405:                 if ($name == 'PRIMARY') {
406:                     $out .= 'PRIMARY ';
407:                     $name = null;
408:                 } else {
409:                     if (!empty($value['unique'])) {
410:                         $out .= 'UNIQUE ';
411:                     }
412:                 }
413:                 if (is_array($value['column'])) {
414:                     $out .= 'KEY '. $name .' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
415:                 } else {
416:                     $out .= 'KEY '. $name .' (' . $this->name($value['column']) . ')';
417:                 }
418:                 $alter[] = $out;
419:             }
420:         }
421:         return $alter;
422:     }
423: 
424: /**
425:  * Inserts multiple values into a table
426:  *
427:  * @param string $table
428:  * @param string $fields
429:  * @param array $values
430:  */
431:     function insertMulti($table, $fields, $values) {
432:         $table = $this->fullTableName($table);
433:         if (is_array($fields)) {
434:             $fields = implode(', ', array_map(array(&$this, 'name'), $fields));
435:         }
436:         $values = implode(', ', $values);
437:         $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}");
438:     }
439: /**
440:  * Returns an detailed array of sources (tables) in the database.
441:  *
442:  * @param string $name Table name to get parameters 
443:  * @return array Array of tablenames in the database
444:  */
445:     function listDetailedSources($name = null) {
446:         $condition = '';
447:         if (is_string($name)) {
448:             $condition = ' LIKE ' . $this->value($name);
449:         }
450:         $result = $this->query('SHOW TABLE STATUS FROM ' . $this->name($this->config['database']) . $condition . ';');
451:         if (!$result) {
452:             return array();
453:         } else {
454:             $tables = array();
455:             foreach ($result as $row) {
456:                 $tables[$row['TABLES']['Name']] = $row['TABLES'];
457:                 if (!empty($row['TABLES']['Collation'])) {
458:                     $charset = $this->getCharsetName($row['TABLES']['Collation']);
459:                     if ($charset) {
460:                         $tables[$row['TABLES']['Name']]['charset'] = $charset;
461:                     }
462:                 }
463:             }
464:             if (is_string($name)) {
465:                 return $tables[$name];
466:             }
467:             return $tables;
468:         }
469:     }
470: 
471: /**
472:  * Converts database-layer column types to basic types
473:  *
474:  * @param string $real Real database-layer column type (i.e. "varchar(255)")
475:  * @return string Abstract column type (i.e. "string")
476:  */
477:     function column($real) {
478:         if (is_array($real)) {
479:             $col = $real['name'];
480:             if (isset($real['limit'])) {
481:                 $col .= '('.$real['limit'].')';
482:             }
483:             return $col;
484:         }
485: 
486:         $col = str_replace(')', '', $real);
487:         $limit = $this->length($real);
488:         if (strpos($col, '(') !== false) {
489:             list($col, $vals) = explode('(', $col);
490:         }
491: 
492:         if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
493:             return $col;
494:         }
495:         if (($col == 'tinyint' && $limit == 1) || $col == 'boolean') {
496:             return 'boolean';
497:         }
498:         if (strpos($col, 'int') !== false) {
499:             return 'integer';
500:         }
501:         if (strpos($col, 'char') !== false || $col == 'tinytext') {
502:             return 'string';
503:         }
504:         if (strpos($col, 'text') !== false) {
505:             return 'text';
506:         }
507:         if (strpos($col, 'blob') !== false || $col == 'binary') {
508:             return 'binary';
509:         }
510:         if (strpos($col, 'float') !== false || strpos($col, 'double') !== false || strpos($col, 'decimal') !== false) {
511:             return 'float';
512:         }
513:         if (strpos($col, 'enum') !== false) {
514:             return "enum($vals)";
515:         }
516:         return 'text';
517:     }
518: }
519: 
520: /**
521:  * MySQL DBO driver object
522:  *
523:  * Provides connection and SQL generation for MySQL RDMS
524:  *
525:  * @package       cake
526:  * @subpackage    cake.cake.libs.model.datasources.dbo
527:  */
528: class DboMysql extends DboMysqlBase {
529: 
530: /**
531:  * Datasource description
532:  *
533:  * @var string
534:  */
535:     var $description = "MySQL DBO Driver";
536: 
537: /**
538:  * Base configuration settings for MySQL driver
539:  *
540:  * @var array
541:  */
542:     var $_baseConfig = array(
543:         'persistent' => true,
544:         'host' => 'localhost',
545:         'login' => 'root',
546:         'password' => '',
547:         'database' => 'cake',
548:         'port' => '3306'
549:     );
550: 
551: /**
552:  * Connects to the database using options in the given configuration array.
553:  *
554:  * @return boolean True if the database could be connected, else false
555:  */
556:     function connect() {
557:         $config = $this->config;
558:         $this->connected = false;
559: 
560:         if (!$config['persistent']) {
561:             $this->connection = mysql_connect($config['host'] . ':' . $config['port'], $config['login'], $config['password'], true);
562:             $config['connect'] = 'mysql_connect';
563:         } else {
564:             $this->connection = mysql_pconnect($config['host'] . ':' . $config['port'], $config['login'], $config['password']);
565:         }
566: 
567:         if (!$this->connection) {
568:             return false;
569:         }
570: 
571:         if (mysql_select_db($config['database'], $this->connection)) {
572:             $this->connected = true;
573:         }
574: 
575:         if (!empty($config['encoding'])) {
576:             $this->setEncoding($config['encoding']);
577:         }
578: 
579:         $this->_useAlias = (bool)version_compare(mysql_get_server_info($this->connection), "4.1", ">=");
580: 
581:         return $this->connected;
582:     }
583: 
584: /**
585:  * Check whether the MySQL extension is installed/loaded
586:  *
587:  * @return boolean
588:  */
589:     function enabled() {
590:         return extension_loaded('mysql');
591:     }
592: /**
593:  * Disconnects from database.
594:  *
595:  * @return boolean True if the database could be disconnected, else false
596:  */
597:     function disconnect() {
598:         if (isset($this->results) && is_resource($this->results)) {
599:             mysql_free_result($this->results);
600:         }
601:         $this->connected = !@mysql_close($this->connection);
602:         return !$this->connected;
603:     }
604: 
605: /**
606:  * Executes given SQL statement.
607:  *
608:  * @param string $sql SQL statement
609:  * @return resource Result resource identifier
610:  * @access protected
611:  */
612:     function _execute($sql) {
613:         return mysql_query($sql, $this->connection);
614:     }
615: 
616: /**
617:  * Returns an array of sources (tables) in the database.
618:  *
619:  * @return array Array of tablenames in the database
620:  */
621:     function listSources() {
622:         $cache = parent::listSources();
623:         if ($cache != null) {
624:             return $cache;
625:         }
626:         $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';');
627: 
628:         if (!$result) {
629:             return array();
630:         } else {
631:             $tables = array();
632: 
633:             while ($line = mysql_fetch_row($result)) {
634:                 $tables[] = $line[0];
635:             }
636:             parent::listSources($tables);
637:             return $tables;
638:         }
639:     }
640: 
641: /**
642:  * Returns a quoted and escaped string of $data for use in an SQL statement.
643:  *
644:  * @param string $data String to be prepared for use in an SQL statement
645:  * @param string $column The column into which this data will be inserted
646:  * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
647:  * @return string Quoted and escaped data
648:  */
649:     function value($data, $column = null, $safe = false) {
650:         $parent = parent::value($data, $column, $safe);
651: 
652:         if ($parent != null) {
653:             return $parent;
654:         }
655:         if ($data === null || (is_array($data) && empty($data))) {
656:             return 'NULL';
657:         }
658:         if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') {
659:             return  "''";
660:         }
661:         if (empty($column)) {
662:             $column = $this->introspectType($data);
663:         }
664: 
665:         switch ($column) {
666:             case 'boolean':
667:                 return $this->boolean((bool)$data);
668:             break;
669:             case 'integer':
670:             case 'float':
671:                 if ($data === '') {
672:                     return 'NULL';
673:                 }
674:                 if (is_float($data)) {
675:                     return str_replace(',', '.', strval($data));
676:                 }
677:                 if ((is_int($data) || $data === '0') || (
678:                     is_numeric($data) && strpos($data, ',') === false &&
679:                     $data[0] != '0' && strpos($data, 'e') === false)
680:                 ) {
681:                     return $data;
682:                 }
683:             default:
684:                 return "'" . mysql_real_escape_string($data, $this->connection) . "'";
685:             break;
686:         }
687:     }
688: 
689: /**
690:  * Returns a formatted error message from previous database operation.
691:  *
692:  * @return string Error message with error number
693:  */
694:     function lastError() {
695:         if (mysql_errno($this->connection)) {
696:             return mysql_errno($this->connection).': '.mysql_error($this->connection);
697:         }
698:         return null;
699:     }
700: 
701: /**
702:  * Returns number of affected rows in previous database operation. If no previous operation exists,
703:  * this returns false.
704:  *
705:  * @return integer Number of affected rows
706:  */
707:     function lastAffected() {
708:         if ($this->_result) {
709:             return mysql_affected_rows($this->connection);
710:         }
711:         return null;
712:     }
713: 
714: /**
715:  * Returns number of rows in previous resultset. If no previous resultset exists,
716:  * this returns false.
717:  *
718:  * @return integer Number of rows in resultset
719:  */
720:     function lastNumRows() {
721:         if ($this->hasResult()) {
722:             return mysql_num_rows($this->_result);
723:         }
724:         return null;
725:     }
726: 
727: /**
728:  * Returns the ID generated from the previous INSERT operation.
729:  *
730:  * @param unknown_type $source
731:  * @return in
732:  */
733:     function lastInsertId($source = null) {
734:         $id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false);
735:         if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) {
736:             return $id[0]['insertID'];
737:         }
738: 
739:         return null;
740:     }
741: 
742: /**
743:  * Enter description here...
744:  *
745:  * @param unknown_type $results
746:  */
747:     function resultSet(&$results) {
748:         if (isset($this->results) && is_resource($this->results) && $this->results != $results) {
749:             mysql_free_result($this->results);
750:         }
751:         $this->results =& $results;
752:         $this->map = array();
753:         $numFields = mysql_num_fields($results);
754:         $index = 0;
755:         $j = 0;
756: 
757:         while ($j < $numFields) {
758:             $column = mysql_fetch_field($results, $j);
759:             if (!empty($column->table) && strpos($column->name, $this->virtualFieldSeparator) === false) {
760:                 $this->map[$index++] = array($column->table, $column->name);
761:             } else {
762:                 $this->map[$index++] = array(0, $column->name);
763:             }
764:             $j++;
765:         }
766:     }
767: 
768: /**
769:  * Fetches the next row from the current result set
770:  *
771:  * @return unknown
772:  */
773:     function fetchResult() {
774:         if ($row = mysql_fetch_row($this->results)) {
775:             $resultRow = array();
776:             $i = 0;
777:             foreach ($row as $index => $field) {
778:                 list($table, $column) = $this->map[$index];
779:                 $resultRow[$table][$column] = $row[$index];
780:                 $i++;
781:             }
782:             return $resultRow;
783:         } else {
784:             return false;
785:         }
786:     }
787: 
788: /**
789:  * Gets the database encoding
790:  *
791:  * @return string The database encoding
792:  */
793:     function getEncoding() {
794:         return mysql_client_encoding($this->connection);
795:     }
796: 
797: /**
798:  * Query charset by collation
799:  *
800:  * @param string $name Collation name
801:  * @return string Character set name
802:  */
803:     function getCharsetName($name) {
804:         if ((bool)version_compare(mysql_get_server_info($this->connection), "5", ">=")) {
805:             $cols = $this->query('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= ' . $this->value($name) . ';');
806:             if (isset($cols[0]['COLLATIONS']['CHARACTER_SET_NAME'])) {
807:                 return $cols[0]['COLLATIONS']['CHARACTER_SET_NAME'];
808:             }
809:         }
810:         return false;
811:     }
812: }
813: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs