Class DataSource

DataSource base class

DataSources are the link between models and the source of data that models represent.

Object
Extended by DataSource

Direct Subclasses

Indirect Subclasses

Package: Cake\Model\Datasource
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 protected
    array
    The default configuration of a specific DataSource
  • $_descriptions protected
    array
    Holds references to descriptions loaded by the DataSource
  • $_sources protected
    array
    Holds a list of sources (tables) contained in the DataSource
  • boolean
    Whether or not this DataSource is in the middle of a transaction
  • $cacheSources public
    boolean

    Whether or not source data like available tables and schema descriptions should be cached

  • $config public
    array
    The DataSource configuration
  • $connected public
    boolean
    Are we connected to the DataSource?

Method Summary

  • __construct() public
    Constructor.
  • __destruct() public
    Closes the current datasource.
  • Cache the DataSource description
  • begin() public
    Begin a transaction
  • close() public
    Closes a connection. Override in subclasses.
  • column() public
    Converts column types to basic types
  • commit() public
    Commit a transaction
  • create() public
    Used to create new records. The "C" CRUD.
  • delete() public
    Delete a record(s) in the datasource.
  • describe() public
    Returns 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.

  • Returns the schema name. Override this in subclasses.
  • Replaces {$__cakeID__$} and {$__cakeForeignKey__$} placeholders in query data.
  • Returns the number of rows affected by last query.
  • Returns the ID generated from the previous INSERT operation.
  • lastNumRows() public
    Returns the number of rows returned by last operation.
  • listSources() public
    Caches/returns cached results for child instances
  • read() public
    Used to read records from the Datasource. The "R" in CRUD
  • resolveKey() public
    To-be-overridden in subclasses.
  • rollback() public
    Rollback a transaction
  • setConfig() public

    Sets the configuration for the DataSource. Merges the $config information with the _baseConfig and the existing $config property.

  • update() public
    Update 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()

__destruct()source public

__destruct( )

Closes the current datasource.

_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

mixed

begin()source public

begin( )

Begin a transaction

Returns

boolean
Returns true if a transaction is not in progress

close()source public

close( )

Closes a connection. Override in subclasses.

Returns

boolean

column()source public

column( string $real )

Converts column types to basic types

Parameters

string $real
Real column type (i.e. "varchar(255)")

Returns

string
Abstract column type (i.e. "string")

commit()source public

commit( )

Commit a transaction

Returns

boolean
Returns 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

boolean
success

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

boolean
Success

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|null
Array 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

boolean
Whether or not the Datasources conditions for use are met.

getSchemaName()source public

getSchemaName( )

Returns the schema name. Override this in subclasses.

Returns

string|null
The 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

mixed
String 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

integer
Number 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

mixed
Last 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

integer
Number 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|null
Array 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

mixed

resolveKey()source public

resolveKey( Model $Model , string $key )

To-be-overridden in subclasses.

Parameters

Model $Model
Model instance
string $key
Key name to make

Returns

string
Key name for model.

rollback()source public

rollback( )

Rollback a transaction

Returns

boolean
Returns 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

boolean
Success

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

mixed
Returns 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

boolean
Success 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

mixed

Boolean 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

string
The name of this class

Properties detail

$_baseConfigsource

protected array

The default configuration of a specific DataSource

array()

$_descriptionssource

protected array

Holds references to descriptions loaded by the DataSource

array()

$_sourcessource

protected array

Holds a list of sources (tables) contained in the DataSource

null

$_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

$configsource

public array

The DataSource configuration

array()

$connectedsource

public boolean

Are we connected to the DataSource?

false

© 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