lithium\data\Connections::add PHP Method

add() public static method

For example: Connections::add('default', array( 'type' => 'database', 'adapter' => 'MySql', 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'my_blog' )); or Connections::add('couch', array( 'type' => 'http', 'adapter' => 'CouchDb', 'host' => '127.0.0.1', 'port' => 5984 )); or Connections::add('mongo', array('type' => 'MongoDb', 'database' => 'my_app'));
See also: lithium\data\Model::$_meta
public static add ( string $name, array $config = [] ) : array
$name string The name by which this connection is referenced. Use this name to retrieve the connection again using `Connections::get()`, or to bind a model to it using `Model::$_meta['connection']`.
$config array Contains all additional configuration information used by the connection, including the name of the adapter class where applicable (i.e. `MySql`), the server name and port or socket to connect to, and (typically) the name of the database or other entity to use. Each adapter has its own specific configuration settings for handling things like connection persistence, data encoding, etc. See the individual adapter or data source class for more information on what configuration settings it supports. Basic / required options supported by most adapters: - `'type'` _string_: The type of data source that defines this connection; typically a class or namespace name. Relational database data sources, use `'database'`, while CouchDB and other HTTP-related data sources use `'http'`, etc. For classes which directly extend `lithium\data\Source`, and do not use an adapter, simply use the name of the class, i.e. `'MongoDb'`. - `'adapter'` _string_: For `type`s such as `'database'` which are adapter-driven, provides the name of the adapter associated with this configuration. - `'host'` _string_: The host name that the database should connect to. Typically defaults to `'localhost'`. - `'login'` _string_: If the connection requires authentication, specifies the login name to use. - `'password'` _string_: If the connection requires authentication, specifies the password to use.
return array Returns the final post-processed connection information, as stored in the internal configuration array used by `Connections`.
    public static function add($name, array $config = array())
    {
        $defaults = array('type' => null, 'adapter' => null, 'login' => '', 'password' => '');
        return static::$_configurations[$name] = $config + $defaults;
    }

Usage Example

示例#1
0
 public function setUp()
 {
     $this->_handlers = array('id' => function ($v) {
         return is_string($v) && preg_match('/^[0-9a-f]{24}$/', $v) ? new MongoId($v) : $v;
     }, 'date' => function ($v) {
         $v = is_numeric($v) ? (int) $v : strtotime($v);
         return !$v ? new MongoDate() : new MongoDate($v);
     }, 'regex' => function ($v) {
         return new MongoRegex($v);
     }, 'integer' => function ($v) {
         return (int) $v;
     }, 'float' => function ($v) {
         return (double) $v;
     }, 'boolean' => function ($v) {
         return (bool) $v;
     }, 'code' => function ($v) {
         return new MongoCode($v);
     }, 'binary' => function ($v) {
         return new MongoBinData($v);
     });
     $model = $this->_model;
     Connections::add('mockconn', array('object' => new MongoDb(array('autoConnect' => false))));
     $model::config(array('meta' => array('connection' => 'mockconn')));
     $model::schema(false);
     $model::schema($this->_schema);
 }
All Usage Examples Of lithium\data\Connections::add