DataSource (class)
Class DataSource
DataSource base class
DataSources are the link between models and the source of data that models represent.
- Object
-
DataSource
Direct Subclasses
Indirect Subclasses
Link: http://book.cakephp.org/2.0/en/models/datasources.html#basic-api-for-datasources
Copyright: Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
License: MIT License
Location: Cake/Model/Datasource/DataSource.php
Properties summary
-
$_baseConfig
protectedThe default configuration of a specific DataSourcearray
-
$_descriptions
protectedHolds references to descriptions loaded by the DataSourcearray
-
$_sources
protectedHolds a list of sources (tables) contained in the DataSourcearray
-
$_transactionStarted
protectedWhether or not this DataSource is in the middle of a transactionboolean
-
$cacheSources
publicboolean
Whether or not source data like available tables and schema descriptions should be cached
-
$config
publicThe DataSource configurationarray
-
$connected
publicAre we connected to the DataSource?boolean
Method Summary
- __construct() publicConstructor.
- __destruct() publicCloses the current datasource.
- _cacheDescription() protectedCache the DataSource description
- begin() publicBegin a transaction
- close() publicCloses a connection. Override in subclasses.
- column() publicConverts column types to basic types
- commit() publicCommit a transaction
- create() publicUsed to create new records. The "C" CRUD.
- delete() publicDelete a record(s) in the datasource.
- describe() publicReturns a Model description (metadata) or null if none found.
- enabled() public
Check whether the conditions for the Datasource being available are satisfied. Often used from connect() to check for support before establishing a connection.
- getSchemaName() publicReturns the schema name. Override this in subclasses.
- insertQueryData() publicReplaces
{$__cakeID__$}
and{$__cakeForeignKey__$}
placeholders in query data. - lastAffected() publicReturns the number of rows affected by last query.
- lastInsertId() publicReturns the ID generated from the previous INSERT operation.
- lastNumRows() publicReturns the number of rows returned by last operation.
- listSources() publicCaches/returns cached results for child instances
- read() publicUsed to read records from the Datasource. The "R" in CRUD
- resolveKey() publicTo-be-overridden in subclasses.
- rollback() publicRollback a transaction
- setConfig() public
Sets the configuration for the DataSource. Merges the $config information with the _baseConfig and the existing $config property.
- update() publicUpdate a record(s) in the datasource.
Method Detail
__construct()source public
__construct( array $config array() )
Constructor.
Parameters
- array
$config
optional array() - Array of configuration information for the datasource.
Overrides
Object::__construct()
_cacheDescription()source protected
_cacheDescription( string $object , mixed $data null )
Cache the DataSource description
Parameters
- string
$object
- The name of the object (model) to cache
- mixed
$data
optional null - The description of the model, usually a string or array
Returns
mixedbegin()source public
begin( )
Begin a transaction
Returns
booleanReturns true if a transaction is not in progress
column()source public
column( string $real )
Converts column types to basic types
Parameters
- string
$real
- Real column type (i.e. "varchar(255)")
Returns
stringAbstract column type (i.e. "string")
commit()source public
commit( )
Commit a transaction
Returns
booleanReturns true if a transaction is in progress
create()source public
create( Model $Model , array $fields null , array $values null )
Used to create new records. The "C" CRUD.
To-be-overridden in subclasses.
Parameters
-
Model
$Model
- The Model to be created.
- array
$fields
optional null - An Array of fields to be saved.
- array
$values
optional null - An Array of values to save.
Returns
booleansuccess
delete()source public
delete( Model $Model , mixed $conditions null )
Delete a record(s) in the datasource.
To-be-overridden in subclasses.
Parameters
-
Model
$Model
- The model class having record(s) deleted
- mixed
$conditions
optional null - The conditions to use for deleting.
Returns
booleanSuccess
describe()source public
describe( Model|string $model )
Returns a Model description (metadata) or null if none found.
Parameters
-
Model
|string$model
- The model to describe.
Returns
array|nullArray of Metadata for the $model
enabled()source public
enabled( )
Check whether the conditions for the Datasource being available are satisfied. Often used from connect() to check for support before establishing a connection.
Returns
booleanWhether or not the Datasources conditions for use are met.
getSchemaName()source public
getSchemaName( )
Returns the schema name. Override this in subclasses.
Returns
string|nullThe schema name
insertQueryData()source public
insertQueryData( string $query , array $data , string $association , Model $Model , array $stack )
Replaces {$__cakeID__$}
and {$__cakeForeignKey__$}
placeholders in query data.
Parameters
- string
$query
- Query string needing replacements done.
- array
$data
- Array of data with values that will be inserted in placeholders.
- string
$association
- Name of association model being replaced.
-
Model
$Model
- Model instance.
- array
$stack
- The context stack.
Returns
mixedString of query data with placeholders replaced, or false on failure.
lastAffected()source public
lastAffected( mixed $source null )
Returns the number of rows affected by last query.
Parameters
- mixed
$source
optional null - The source name.
Returns
integerNumber of rows affected by last query.
lastInsertId()source public
lastInsertId( mixed $source null )
Returns the ID generated from the previous INSERT operation.
Parameters
- mixed
$source
optional null - The source name.
Returns
mixedLast ID key generated in previous INSERT
lastNumRows()source public
lastNumRows( mixed $source null )
Returns the number of rows returned by last operation.
Parameters
- mixed
$source
optional null - The source name.
Returns
integerNumber of rows returned by last operation
listSources()source public
listSources( mixed $data null )
Caches/returns cached results for child instances
Parameters
- mixed
$data
optional null - Unused in this class.
Returns
array|nullArray of sources available in this datasource.
read()source public
read( Model $Model , array $queryData array() , integer $recursive null )
Used to read records from the Datasource. The "R" in CRUD
To-be-overridden in subclasses.
Parameters
-
Model
$Model
- The model being read.
- array
$queryData
optional array() - An array of query data used to find the data you want
- integer
$recursive
optional null - Number of levels of association
Returns
mixedresolveKey()source public
resolveKey( Model $Model , string $key )
To-be-overridden in subclasses.
Parameters
-
Model
$Model
- Model instance
- string
$key
- Key name to make
Returns
stringKey name for model.
rollback()source public
rollback( )
Rollback a transaction
Returns
booleanReturns true if a transaction is in progress
setConfig()source public
setConfig( array $config array() )
Sets the configuration for the DataSource. Merges the $config information with the _baseConfig and the existing $config property.
Parameters
- array
$config
optional array() - The configuration array
update()source public
update( Model $Model , array $fields null , array $values null , mixed $conditions null )
Update a record(s) in the datasource.
To-be-overridden in subclasses.
Parameters
-
Model
$Model
- Instance of the model class being updated
- array
$fields
optional null - Array of fields to be updated
- array
$values
optional null - Array of values to be update $fields to.
- mixed
$conditions
optional null - The array of conditions to use.
Returns
booleanSuccess
Methods inherited from Object
_mergeVars()source protected
_mergeVars( array $properties , string $class , boolean $normalize true )
Merges this objects $property with the property in $class' definition. This classes value for the property will be merged on top of $class'
This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine this method as an empty function.
Parameters
- array
$properties
- The name of the properties to merge.
- string
$class
- The class to merge the property with.
- boolean
$normalize
optional true - Set to true to run the properties through Hash::normalize() before merging.
_set()source protected
_set( array $properties array() )
Allows setting of multiple properties of the object in a single line of code. Will only set properties that are part of a class declaration.
Parameters
- array
$properties
optional array() - An associative array containing properties and corresponding values.
_stop()source protected
_stop( integer|string $status 0 )
Stop execution of the current script. Wraps exit() making testing easier.
Parameters
- integer|string
$status
optional 0 - see http://php.net/exit for values
dispatchMethod()source public
dispatchMethod( string $method , array $params array() )
Calls a method on this object with the given parameters. Provides an OO wrapper for call_user_func_array
Parameters
- string
$method
- Name of the method to call
- array
$params
optional array() - Parameter list to use when calling $method
Returns
mixedReturns the result of the method call
log()source public
log( string $msg , integer $type LOG_ERR , null|string|array $scope null )
Convenience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
Parameters
- string
$msg
- Log message
- integer
$type
optional LOG_ERR - Error type constant. Defined in app/Config/core.php.
- null|string|array
$scope
optional null The scope(s) a log message is being created in. See CakeLog::config() for more information on logging scopes.
Returns
booleanSuccess of log write
requestAction()source public
requestAction( string|array $url , array $extra array() )
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
Under the hood this method uses Router::reverse() to convert the $url parameter into a string URL. You should use URL formats that are compatible with Router::reverse()
Passing POST and GET data
POST and GET data can be simulated in requestAction. Use $extra['url']
for GET data. The $extra['data']
parameter allows POST data simulation.
Parameters
- string|array
$url
String or array-based URL. Unlike other URL arrays in CakePHP, this URL will not automatically handle passed and named arguments in the $url parameter.
- array
$extra
optional array() if array includes the key "return" it sets the AutoRender to true. Can also be used to submit GET/POST data, and named/passed arguments.
Returns
mixedBoolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
toString()source public
toString( )
Object-to-string conversion. Each class can override this method as necessary.
Returns
stringThe name of this class
Properties detail
$_descriptionssource
protected array
Holds references to descriptions loaded by the DataSource
array()
$_transactionStartedsource
protected boolean
Whether or not this DataSource is in the middle of a transaction
false
$cacheSourcessource
public boolean
Whether or not source data like available tables and schema descriptions should be cached
true
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/2.8/class-DataSource.html