2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DataSource base class
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Datasource
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.5.1790
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* DataSource base class
|
|
|
|
*
|
2013-06-26 20:07:52 +00:00
|
|
|
* DataSources are the link between models and the source of data that models represent.
|
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/models/datasources.html#basic-api-for-datasources
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Datasource
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2016-04-08 12:33:26 +00:00
|
|
|
class DataSource extends CakeObject {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Are we connected to the DataSource?
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $connected = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The default configuration of a specific DataSource
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_baseConfig = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds references to descriptions loaded by the DataSource
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_descriptions = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds a list of sources (tables) contained in the DataSource
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_sources = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The DataSource configuration
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $config = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Whether or not this DataSource is in the middle of a transaction
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_transactionStarted = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Whether or not source data like available tables and schema descriptions
|
|
|
|
* should be cached
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cacheSources = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
2009-12-19 18:23:46 +00:00
|
|
|
*
|
|
|
|
* @param array $config Array of configuration information for the datasource.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __construct($config = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
parent::__construct();
|
2008-08-01 06:08:23 +00:00
|
|
|
$this->setConfig($config);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Caches/returns cached results for child instances
|
|
|
|
*
|
2014-06-28 02:58:34 +00:00
|
|
|
* @param mixed $data Unused in this class.
|
2014-11-05 12:03:27 +00:00
|
|
|
* @return array|null Array of sources available in this datasource.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function listSources($data = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->cacheSources === false) {
|
|
|
|
return null;
|
|
|
|
}
|
2008-08-07 15:36:26 +00:00
|
|
|
|
|
|
|
if ($this->_sources !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->_sources;
|
|
|
|
}
|
|
|
|
|
2008-08-12 21:08:51 +00:00
|
|
|
$key = ConnectionManager::getSourceName($this) . '_' . $this->config['database'] . '_list';
|
2008-08-14 16:52:29 +00:00
|
|
|
$key = preg_replace('/[^A-Za-z0-9_\-.+]/', '_', $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
$sources = Cache::read($key, '_cake_model_');
|
|
|
|
|
2008-07-26 20:00:20 +00:00
|
|
|
if (empty($sources)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$sources = $data;
|
2008-07-26 20:00:20 +00:00
|
|
|
Cache::write($key, $data, '_cake_model_');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 00:41:27 +00:00
|
|
|
return $this->_sources = $sources;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a Model description (metadata) or null if none found.
|
|
|
|
*
|
2014-06-28 02:58:34 +00:00
|
|
|
* @param Model|string $model The model to describe.
|
2014-11-05 12:03:27 +00:00
|
|
|
* @return array|null Array of Metadata for the $model
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-09-03 15:57:58 +00:00
|
|
|
public function describe($model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->cacheSources === false) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-09-03 15:57:58 +00:00
|
|
|
if (is_string($model)) {
|
|
|
|
$table = $model;
|
|
|
|
} else {
|
|
|
|
$table = $model->tablePrefix . $model->table;
|
|
|
|
}
|
2010-01-14 04:14:06 +00:00
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (isset($this->_descriptions[$table])) {
|
|
|
|
return $this->_descriptions[$table];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-08-20 05:39:30 +00:00
|
|
|
$cache = $this->_cacheDescription($table);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($cache !== null) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_descriptions[$table] =& $cache;
|
2008-05-30 11:40:08 +00:00
|
|
|
return $cache;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Begin a transaction
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Returns true if a transaction is not in progress
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function begin() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return !$this->_transactionStarted;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Commit a transaction
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Returns true if a transaction is in progress
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function commit() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->_transactionStarted;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Rollback a transaction
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Returns true if a transaction is in progress
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function rollback() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->_transactionStarted;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Converts column types to basic types
|
|
|
|
*
|
2013-07-05 12:36:40 +00:00
|
|
|
* @param string $real Real column type (i.e. "varchar(255)")
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Abstract column type (i.e. "string")
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function column($real) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-10-20 01:09:47 +00:00
|
|
|
* Used to create new records. The "C" CRUD.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* To-be-overridden in subclasses.
|
|
|
|
*
|
2013-11-12 06:31:08 +00:00
|
|
|
* @param Model $Model The Model to be created.
|
2009-10-20 01:09:47 +00:00
|
|
|
* @param array $fields An Array of fields to be saved.
|
|
|
|
* @param array $values An Array of values to save.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool success
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-11-12 06:31:08 +00:00
|
|
|
public function create(Model $Model, $fields = null, $values = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-10-20 01:09:47 +00:00
|
|
|
* Used to read records from the Datasource. The "R" in CRUD
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* To-be-overridden in subclasses.
|
|
|
|
*
|
2013-11-12 06:31:08 +00:00
|
|
|
* @param Model $Model The model being read.
|
2009-10-20 01:09:47 +00:00
|
|
|
* @param array $queryData An array of query data used to find the data you want
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $recursive Number of levels of association
|
2009-10-20 01:09:47 +00:00
|
|
|
* @return mixed
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-11-12 06:31:08 +00:00
|
|
|
public function read(Model $Model, $queryData = array(), $recursive = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-10-20 01:09:47 +00:00
|
|
|
* Update a record(s) in the datasource.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* To-be-overridden in subclasses.
|
|
|
|
*
|
2013-11-12 06:31:08 +00:00
|
|
|
* @param Model $Model Instance of the model class being updated
|
2009-10-20 01:09:47 +00:00
|
|
|
* @param array $fields Array of fields to be updated
|
|
|
|
* @param array $values Array of values to be update $fields to.
|
2014-06-28 02:58:34 +00:00
|
|
|
* @param mixed $conditions The array of conditions to use.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-11-12 06:31:08 +00:00
|
|
|
public function update(Model $Model, $fields = null, $values = null, $conditions = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-10-20 01:09:47 +00:00
|
|
|
* Delete a record(s) in the datasource.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* To-be-overridden in subclasses.
|
|
|
|
*
|
2013-11-12 06:31:08 +00:00
|
|
|
* @param Model $Model The model class having record(s) deleted
|
2011-10-06 02:10:14 +00:00
|
|
|
* @param mixed $conditions The conditions to use for deleting.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-11-12 06:31:08 +00:00
|
|
|
public function delete(Model $Model, $conditions = null) {
|
2011-10-06 02:10:14 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the ID generated from the previous INSERT operation.
|
|
|
|
*
|
2014-06-28 02:58:34 +00:00
|
|
|
* @param mixed $source The source name.
|
2009-12-19 18:23:46 +00:00
|
|
|
* @return mixed Last ID key generated in previous INSERT
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function lastInsertId($source = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-23 02:21:55 +00:00
|
|
|
* Returns the number of rows returned by last operation.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-06-28 02:58:34 +00:00
|
|
|
* @param mixed $source The source name.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int Number of rows returned by last operation
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function lastNumRows($source = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-23 02:21:55 +00:00
|
|
|
* Returns the number of rows affected by last query.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-06-28 02:58:34 +00:00
|
|
|
* @param mixed $source The source name.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int Number of rows affected by last query.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function lastAffected($source = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-10-20 20:11:31 +00:00
|
|
|
/**
|
|
|
|
* Check whether the conditions for the Datasource being available
|
2012-12-22 22:48:15 +00:00
|
|
|
* are satisfied. Often used from connect() to check for support
|
2009-10-20 20:11:31 +00:00
|
|
|
* before establishing a connection.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Whether or not the Datasources conditions for use are met.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function enabled() {
|
2009-10-20 20:11:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-11-23 02:21:55 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-12-19 18:23:46 +00:00
|
|
|
* Sets the configuration for the DataSource.
|
|
|
|
* Merges the $config information with the _baseConfig and the existing $config property.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $config The configuration array
|
2008-08-01 06:08:23 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function setConfig($config = array()) {
|
2008-08-01 06:08:23 +00:00
|
|
|
$this->config = array_merge($this->_baseConfig, $this->config, $config);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Cache the DataSource description
|
|
|
|
*
|
|
|
|
* @param string $object The name of the object (model) to cache
|
|
|
|
* @param mixed $data The description of the model, usually a string or array
|
2009-12-19 18:23:46 +00:00
|
|
|
* @return mixed
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _cacheDescription($object, $data = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->cacheSources === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($data !== null) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_descriptions[$object] =& $data;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$key = ConnectionManager::getSourceName($this) . '_' . $object;
|
|
|
|
$cache = Cache::read($key, '_cake_model_');
|
|
|
|
|
|
|
|
if (empty($cache)) {
|
|
|
|
$cache = $data;
|
2008-07-26 20:00:20 +00:00
|
|
|
Cache::write($key, $cache, '_cake_model_');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $cache;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-12-19 18:23:46 +00:00
|
|
|
* Replaces `{$__cakeID__$}` and `{$__cakeForeignKey__$}` placeholders in query data.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-12-19 18:23:46 +00:00
|
|
|
* @param string $query Query string needing replacements done.
|
|
|
|
* @param array $data Array of data with values that will be inserted in placeholders.
|
2013-12-21 12:21:27 +00:00
|
|
|
* @param string $association Name of association model being replaced.
|
|
|
|
* @param Model $Model Model instance.
|
2014-06-28 02:58:34 +00:00
|
|
|
* @param array $stack The context stack.
|
2013-12-21 12:21:27 +00:00
|
|
|
* @return mixed String of query data with placeholders replaced, or false on failure.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-11-12 06:31:08 +00:00
|
|
|
public function insertQueryData($query, $data, $association, Model $Model, $stack) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$keys = array('{$__cakeID__$}', '{$__cakeForeignKey__$}');
|
|
|
|
|
2013-11-12 06:31:08 +00:00
|
|
|
$modelAlias = $Model->alias;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($keys as $key) {
|
2013-11-12 06:31:08 +00:00
|
|
|
if (strpos($query, $key) === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$insertKey = $InsertModel = null;
|
|
|
|
switch ($key) {
|
|
|
|
case '{$__cakeID__$}':
|
|
|
|
$InsertModel = $Model;
|
|
|
|
$insertKey = $Model->primaryKey;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case '{$__cakeForeignKey__$}':
|
|
|
|
foreach ($Model->associations() as $type) {
|
|
|
|
foreach ($Model->{$type} as $assoc => $assocData) {
|
|
|
|
if ($assoc !== $association) {
|
|
|
|
continue;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-11-12 06:31:08 +00:00
|
|
|
|
|
|
|
if (isset($assocData['foreignKey'])) {
|
|
|
|
$InsertModel = $Model->{$assoc};
|
|
|
|
$insertKey = $assocData['foreignKey'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-11-12 06:31:08 +00:00
|
|
|
|
|
|
|
break 3;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-11-12 06:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$val = $dataType = null;
|
|
|
|
if (!empty($insertKey) && !empty($InsertModel)) {
|
|
|
|
if (isset($data[$modelAlias][$insertKey])) {
|
|
|
|
$val = $data[$modelAlias][$insertKey];
|
|
|
|
} elseif (isset($data[$association][$insertKey])) {
|
|
|
|
$val = $data[$association][$insertKey];
|
|
|
|
} else {
|
|
|
|
$found = false;
|
|
|
|
foreach (array_reverse($stack) as $assocData) {
|
2016-02-19 02:03:57 +00:00
|
|
|
if (is_string($assocData) && isset($data[$assocData]) && isset($data[$assocData][$insertKey])) {
|
2013-11-12 06:31:08 +00:00
|
|
|
$val = $data[$assocData][$insertKey];
|
|
|
|
$found = true;
|
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-11-12 06:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$found) {
|
|
|
|
$val = '';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-11-12 06:31:08 +00:00
|
|
|
|
|
|
|
$dataType = $InsertModel->getColumnType($InsertModel->primaryKey);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-11-12 06:31:08 +00:00
|
|
|
|
|
|
|
if (empty($val) && $val !== '0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = str_replace($key, $this->value($val, $dataType), $query);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-11-12 06:31:08 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
return $query;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* To-be-overridden in subclasses.
|
|
|
|
*
|
2013-11-12 06:31:08 +00:00
|
|
|
* @param Model $Model Model instance
|
2009-12-19 18:23:46 +00:00
|
|
|
* @param string $key Key name to make
|
|
|
|
* @return string Key name for model.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-11-12 06:31:08 +00:00
|
|
|
public function resolveKey(Model $Model, $key) {
|
|
|
|
return $Model->alias . $key;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-11-05 10:57:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the schema name. Override this in subclasses.
|
|
|
|
*
|
2014-11-05 12:14:45 +00:00
|
|
|
* @return string|null The schema name
|
2011-11-05 10:57:08 +00:00
|
|
|
*/
|
|
|
|
public function getSchemaName() {
|
2011-12-29 12:18:37 +00:00
|
|
|
return null;
|
2011-11-05 10:57:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-16 15:51:18 +00:00
|
|
|
/**
|
2014-11-05 12:14:45 +00:00
|
|
|
* Closes a connection. Override in subclasses.
|
2012-10-16 00:19:22 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2012-03-16 15:51:18 +00:00
|
|
|
*/
|
|
|
|
public function close() {
|
|
|
|
return $this->connected = false;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Closes the current datasource.
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function __destruct() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->_transactionStarted) {
|
2011-02-25 00:41:27 +00:00
|
|
|
$this->rollback();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($this->connected) {
|
|
|
|
$this->close();
|
|
|
|
}
|
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|