📄 dbo_source.php
字号:
} else { $log = $this->_queriesLog; } if ($this->_queriesCnt > 1) { $text = 'queries'; } else { $text = 'query'; } if (php_sapi_name() != 'cli') { print ("<table class=\"cake-sql-log\" id=\"cakeSqlLog_" . preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true)) . "\" summary=\"Cake SQL Log\" cellspacing=\"0\" border = \"0\">\n<caption>{$this->_queriesCnt} {$text} took {$this->_queriesTime} ms</caption>\n"); print ("<thead>\n<tr><th>Nr</th><th>Query</th><th>Error</th><th>Affected</th><th>Num. rows</th><th>Took (ms)</th></tr>\n</thead>\n<tbody>\n"); foreach ($log as $k => $i) { print ("<tr><td>" . ($k + 1) . "</td><td>" . h($i['query']) . "</td><td>{$i['error']}</td><td style = \"text-align: right\">{$i['affected']}</td><td style = \"text-align: right\">{$i['numRows']}</td><td style = \"text-align: right\">{$i['took']}</td></tr>\n"); } print ("</tbody></table>\n"); } else { foreach ($log as $k => $i) { print (($k + 1) . ". {$i['query']} {$i['error']}\n"); } } }/** * Log given SQL query. * * @param string $sql SQL statement * @todo: Add hook to log errors instead of returning false */ function logQuery($sql) { $this->_queriesCnt++; $this->_queriesTime += $this->took; $this->_queriesLog[] = array( 'query' => $sql, 'error' => $this->error, 'affected' => $this->affected, 'numRows' => $this->numRows, 'took' => $this->took ); if (count($this->_queriesLog) > $this->_queriesLogMax) { array_pop($this->_queriesLog); } if ($this->error) { return false; } }/** * Output information about an SQL query. The SQL statement, number of rows in resultset, * and execution time in microseconds. If the query fails, an error is output instead. * * @param string $sql Query to show information on. */ function showQuery($sql) { $error = $this->error; if (strlen($sql) > 200 && !$this->fullDebug && Configure::read() > 1) { $sql = substr($sql, 0, 200) . '[...]'; } if ($error && Configure::read() > 0) { e("<p style = \"text-align:left\"><b>Query:</b> {$sql} "); if ($error) { trigger_error("<span style = \"color:Red;text-align:left\"><b>SQL Error:</b> {$this->error}</span>", E_USER_WARNING); } else { e("<small>[Aff:{$this->affected} Num:{$this->numRows} Took:{$this->took}ms]</small>"); } e('</p>'); } }/** * Gets full table name including prefix * * @param mixed $model * @param boolean $quote * @return string Full quoted table name */ function fullTableName($model, $quote = true) { if (is_object($model)) { $table = $model->tablePrefix . $model->table; } elseif (isset($this->config['prefix'])) { $table = $this->config['prefix'] . strval($model); } else { $table = strval($model); } if ($quote) { return $this->name($table); } return $table; }/** * The "C" in CRUD * * @param Model $model * @param array $fields * @param array $values * @return boolean Success */ function create(&$model, $fields = null, $values = null) { $id = null; if ($fields == null) { unset($fields, $values); $fields = array_keys($model->data); $values = array_values($model->data); } $count = count($fields); for ($i = 0; $i < $count; $i++) { $valueInsert[] = $this->value($values[$i], $model->getColumnType($fields[$i])); } for ($i = 0; $i < $count; $i++) { $fieldInsert[] = $this->name($fields[$i]); if ($fields[$i] == $model->primaryKey) { $id = $values[$i]; } } if ($this->execute('INSERT INTO ' . $this->fullTableName($model) . ' (' . join(',', $fieldInsert). ') VALUES (' . join(',', $valueInsert) . ')')) { if (empty($id)) { $id = $this->lastInsertId($this->fullTableName($model, false), $model->primaryKey); } $model->setInsertID($id); $model->id = $id; return true; } else { $model->onError(); return false; } }/** * The "R" in CRUD * * @param Model $model * @param array $queryData * @param integer $recursive Number of levels of association * @return unknown */ function read(&$model, $queryData = array(), $recursive = null) { $queryData = $this->__scrubQueryData($queryData); $null = null; $array = array(); $linkedModels = array(); $this->__bypass = false; $this->__booleans = array(); if ($recursive === null && isset($queryData['recursive'])) { $recursive = $queryData['recursive']; } if (!is_null($recursive)) { $_recursive = $model->recursive; $model->recursive = $recursive; } if (!empty($queryData['fields'])) { $this->__bypass = true; $queryData['fields'] = $this->fields($model, null, $queryData['fields']); } else { $queryData['fields'] = $this->fields($model); } foreach ($model->__associations as $type) { foreach ($model->{$type} as $assoc => $assocData) { if ($model->recursive > -1) { $linkModel =& $model->{$assoc}; $external = isset($assocData['external']); if ($model->useDbConfig == $linkModel->useDbConfig) { if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) { $linkedModels[] = $type . '/' . $assoc; } } } } } $query = $this->generateAssociationQuery($model, $null, null, null, null, $queryData, false, $null); $resultSet = $this->fetchAll($query, $model->cacheQueries, $model->alias); if ($resultSet === false) { $model->onError(); return false; } $filtered = $this->__filterResults($resultSet, $model); if ($model->recursive > 0) { foreach ($model->__associations as $type) { foreach ($model->{$type} as $assoc => $assocData) { $linkModel =& $model->{$assoc}; if (!in_array($type . '/' . $assoc, $linkedModels)) { if ($model->useDbConfig == $linkModel->useDbConfig) { $db =& $this; } else { $db =& ConnectionManager::getDataSource($linkModel->useDbConfig); } } elseif ($model->recursive > 1 && ($type == 'belongsTo' || $type == 'hasOne')) { $db =& $this; } if (isset($db)) { $stack = array($assoc); $db->queryAssociation($model, $linkModel, $type, $assoc, $assocData, $array, true, $resultSet, $model->recursive - 1, $stack); unset($db); } } } $this->__filterResults($resultSet, $model, $filtered); } if (!is_null($recursive)) { $model->recursive = $_recursive; } return $resultSet; }/** * Private method. Passes association results thru afterFind filters of corresponding model * * @param array $results Reference of resultset to be filtered * @param object $model Instance of model to operate against * @param array $filtered List of classes already filtered, to be skipped * @return return */ function __filterResults(&$results, &$model, $filtered = array()) { $filtering = array(); $count = count($results); for ($i = 0; $i < $count; $i++) { if (is_array($results[$i])) { $classNames = array_keys($results[$i]); $count2 = count($classNames); for ($j = 0; $j < $count2; $j++) { $className = $classNames[$j]; if ($model->alias != $className && !in_array($className, $filtered)) { if (!in_array($className, $filtering)) { $filtering[] = $className; } if (isset($model->{$className}) && is_object($model->{$className})) { $data = $model->{$className}->afterFind(array(array($className => $results[$i][$className])), false); } if (isset($data[0][$className])) { $results[$i][$className] = $data[0][$className]; } } } } } return $filtering; }/** * Enter description here... * * @param Model $model * @param unknown_type $linkModel * @param string $type Association type * @param unknown_type $association * @param unknown_type $assocData * @param unknown_type $queryData * @param unknown_type $external * @param unknown_type $resultSet * @param integer $recursive Number of levels of association * @param array $stack */ function queryAssociation(&$model, &$linkModel, $type, $association, $assocData, &$queryData, $external = false, &$resultSet, $recursive, $stack) { if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) { if (!isset($resultSet) || !is_array($resultSet)) { if (Configure::read() > 0) { e('<div style = "font: Verdana bold 12px; color: #FF0000">' . sprintf(__('SQL Error in model %s:', true), $model->alias) . ' '); if (isset($this->error) && $this->error != null) { e($this->error); } e('</div>'); } return null; } $count = count($resultSet); if ($type === 'hasMany' && empty($assocData['limit']) && !empty($assocData['foreignKey'])) { $ins = $fetch = array(); for ($i = 0; $i < $count; $i++) { if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) { $ins[] = $in; } } if (!empty($ins)) { $fetch = $this->fetchAssociated($model, $query, $ins); } if (!empty($fetch) && is_array($fetch)) { if ($recursive > 0) { foreach ($linkModel->__associations as $type1) { foreach ($linkModel->{$type1} as $assoc1 => $assocData1) { $deepModel =& $linkModel->{$assoc1}; $tmpStack = $stack; $tmpStack[] = $assoc1; if ($linkModel->useDbConfig === $deepModel->useDbConfig) { $db =& $this; } else { $db =& ConnectionManager::getDataSource($deepModel->useDbConfig); } $db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack); } } } } return $this->__mergeHasMany($resultSet, $fetch, $association, $model, $linkModel, $recursive); } elseif ($type === 'hasAndBelongsToMany') { $ins = $fetch = array(); for ($i = 0; $i < $count; $i++) { if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) { $ins[] = $in; } } if (!empty($ins)) { $query = str_replace('{$__cakeID__$}', '(' .join(', ', $ins) .')', $query); $query = str_replace('= (', 'IN (', $query); $query = str_replace('= (', 'IN (', $query); $query = str_replace(' WHERE 1 = 1', '', $query); } $foreignKey = $model->hasAndBelongsToMany[$association]['foreignKey']; $joinKeys = array($foreignKey, $model->hasAndBelongsToMany[$association]['associationForeignKey']); list($with, $habtmFields) = $model->joinModel($model->hasAndBelongsToMany[$association]['with'], $joinKeys); $habtmFieldsCount = count($habtmFields); $q = $this->insertQueryData($query, null, $association, $assocData, $model, $linkModel, $stack); if ($q != false) { $fetch = $this->fetchAll($q, $model->cacheQueries, $model->alias); } else { $fetch = null; } } for ($i = 0; $i < $count; $i++) { $row =& $resultSet[$i]; if ($type !== 'hasAndBelongsToMany') { $q = $this->insertQueryData($query, $resultSet[$i], $association, $assocData, $model, $linkModel, $stack); if ($q != false) { $fetch = $this->fetchAll($q, $model->cacheQueries, $model->alias); } else { $fetch = null; } } $selfJoin = false; if ($linkModel->name === $model->name) { $selfJoin = true; } if (!empty($fetch) && is_array($fetch)) { if ($recursive > 0) { foreach ($linkModel->__associations as $type1) { foreach ($linkModel->{$type1} as $assoc1 => $assocData1) { $deepModel =& $linkModel->{$assoc1}; if (($type1 === 'belongsTo') || ($deepModel->alias === $model->alias && $type === 'belongsTo') || ($deepModel->alias != $model->alias)) { $tmpStack = $stack; $tmpStack[] = $assoc1; if ($linkModel->useDbConfig == $deepModel->useDbConfig) { $db =& $this; } else { $db =& ConnectionManager::getDataSource($deepModel->useDbConfig); } $db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack); } } } } if ($type == 'hasAndBelongsToMany') { $uniqueIds = $merge = array(); foreach($fetch as $j => $data) { if ( (isset($data[$with]) && $data[$with][$foreignKey] === $row[$model->alias][$model->primaryKey]) && (!in_array($data[$with][$joinKeys[1]], $uniqueIds)) ) { $uniqueIds[] = $data[$with][$joinKeys[1]]; if ($habtmFieldsCount <= 2) { unset($data[$with]); } $merge[] = $data; } } if (empty($merge) && !isset($row[$association])) { $row[$association] = $merge; } else { $this->__mergeAssociation($resultSet[$i], $merge, $association, $type); } } else { $this->__mergeAssociation($resultSet[$i], $fetch, $association, $type, $selfJoin); } $resultSet[$i][$association] = $linkModel->afterfind($resultSet[$i][$association]); } else { $tempArray[0][$association] = false; $this->__mergeAssociation($resultSet[$i], $tempArray, $association, $type, $selfJoin); } } } }/** * A more efficient way to fetch associations. Woohoo! * * @param model $model Primary model object * @param string $query Association query * @param array $ids Array of IDs of associated records * @return array Association results */ function fetchAssociated($model, $query, $ids) { $query = str_replace('{$__cakeID__$}', join(', ', $ids), $query); return $this->fetchAll($query, $model->cacheQueries, $model->alias); } function __mergeHasMany(&$resultSet, $merge, $association, &$model, &$linkModel) { foreach ($resultSet as $i => $value) { $count = 0; $merged[$association] = array(); foreach ($merge as $j => $data) { if (isset($value[$model->alias]) && $value[$model->alias][$model->primaryKey] === $data[$association][$model->hasMany[$association]['foreignKey']]) { if (count($data) > 1) { $data = array_merge($data[$association], $data); unset($data[$association]); foreach ($data as $key => $name) { if (is_numeric($key)) { $data[$association][] = $name; unset($data[$key]); } } $merged[$association][] = $data; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -