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`.
Beispiel #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);
 }
 public function testStreamConnection()
 {
     $config = array('socket' => 'Stream', 'host' => 'localhost', 'login' => 'root', 'password' => '', 'port' => '80');
     Connections::add('stream-test', 'Http', $config);
     $result = Connections::get('stream-test');
     $this->assertTrue($result instanceof \lithium\data\source\Http);
 }
Beispiel #3
0
 public function setUp()
 {
     Connections::add('mockconn', array('object' => new MockSource()));
     $schema = new Schema(array('fields' => array('id' => 'int', 'title' => 'string', 'body' => 'text')));
     MockPost::config(array('meta' => array('connection' => 'mockconn', 'key' => 'id', 'locked' => true), 'schema' => $schema));
     $this->_record = new Record(array('model' => 'lithium\\tests\\mocks\\data\\MockPost'));
 }
 public function setUp()
 {
     $this->_db = new MockDatabase();
     Connections::add('mockconn', array('object' => $this->_db));
     MockGallery::config(array('meta' => array('connection' => 'mockconn')));
     MockImage::config(array('meta' => array('connection' => 'mockconn')));
 }
Beispiel #5
0
 /**
  * Teardown method run after every test method.
  */
 public function tearDown()
 {
     Connections::reset();
     foreach ($this->_backup as $name => $config) {
         Connections::add($name, $config);
     }
 }
Beispiel #6
0
 public function setUp()
 {
     Connections::add('test-gh', array('type' => 'http', 'adapter' => 'GitHub', 'login' => '', 'password' => '', 'socket' => 'li3_github\\tests\\mocks\\MockGitHubSocket'));
     Issues::config(array('connection' => 'test-gh'));
     Repos::config(array('connection' => 'test-gh'));
     Users::config(array('connection' => 'test-gh'));
     Orgs::config(array('connection' => 'test-gh'));
 }
 public function testConnectWithWrongPassword()
 {
     $config = $this->_dbConfig;
     $config['login'] = '******';
     $config['password'] = '******';
     $connection = 'wrong_passord';
     Connections::add($connection, $config);
     $this->expectException('/Host connected, but could not access database/');
     Connections::get($connection)->connect();
 }
 public function setUp()
 {
     $this->_backup['cwd'] = getcwd();
     $this->_backup['_SERVER'] = $_SERVER;
     $_SERVER['argv'] = array();
     Libraries::add('create_test', array('path' => $this->_testPath . '/create_test'));
     $this->request = new Request(array('input' => fopen('php://temp', 'w+')));
     $this->request->params = array('library' => 'create_test', 'action' => null);
     Connections::add('default', array('type' => null, 'adapter' => 'lithium\\tests\\mocks\\data\\model\\MockDatabase'));
 }
Beispiel #9
0
 /**
  * 添加数据库链接
  * @param string $name 名称
  * @param array $config 配置
  * @param bool $overwrite 是否覆盖已有配置,默认否
  * @return array
  */
 public static function add($name, array $config = array(), $overwrite = false)
 {
     if ($overwrite) {
         parent::add($name, $config);
     } else {
         if (!isset(static::$_configurations[$name])) {
             parent::add($name, $config);
         }
     }
     return static::$_configurations[$name];
 }
Beispiel #10
0
 public function setUp()
 {
     $this->_db = new CouchDb(array('socket' => false));
     $model = $this->_model;
     Connections::add('mockconn', array('object' => $this->_db));
     $model::config(array('meta' => array('connection' => 'mockconn')));
     $model::resetSchema();
     $entity = new Document(compact('model'));
     $type = 'create';
     $this->_query = new Query(compact('model', 'entity', 'type'));
 }
Beispiel #11
0
 public function testConnectWithWrongPassword()
 {
     $this->skipIf(!$this->with('PostgreSql'));
     $config = $this->_dbConfig;
     $config['login'] = '******';
     $config['password'] = '******';
     $config['object'] = null;
     $connection = 'wrong_passord';
     Connections::add($connection, $config);
     $this->expectException();
     Connections::get($connection)->connect();
 }
Beispiel #12
0
 public function setUp()
 {
     $this->_db = new MockDatabase();
     Connections::add('mockconn', array('object' => $this->_db));
     $config = array('meta' => array('connection' => 'mockconn'));
     MockQueryPost::config($config);
     MockQueryComment::config($config);
     MockGallery::config($config);
     MockImage::config($config);
     MockImageTag::config($config);
     MockTag::config($config);
 }
Beispiel #13
0
 public function testErrorExceptions()
 {
     $config = array('adapter' => 'None', 'type' => 'Error');
     Connections::add('NoConnection', $config);
     $result = false;
     try {
         Connections::get('NoConnection');
     } catch (Exception $e) {
         $result = true;
     }
     $this->assertTrue($result, 'Exception is not thrown');
 }
Beispiel #14
0
 public function setUp()
 {
     Connections::add('mocksource', array('object' => new MockSource()));
     Connections::add('mockconn', array('object' => new MockDatabase()));
     MockPost::config(array('meta' => array('connection' => 'mocksource')));
     MockTag::config(array('meta' => array('connection' => 'mocksource')));
     MockComment::config(array('meta' => array('connection' => 'mocksource')));
     MockCreator::config(array('meta' => array('connection' => 'mocksource')));
     MockSubProduct::config(array('meta' => array('connection' => 'mockconn')));
     MockProduct::config(array('meta' => array('connection' => 'mockconn')));
     MockPostForValidates::config(array('meta' => array('connection' => 'mockconn')));
     $this->_altSchema = new Schema(array('fields' => array('id' => array('type' => 'integer'), 'author_id' => array('type' => 'integer'), 'title' => array('type' => 'string'), 'body' => array('type' => 'text'))));
 }
Beispiel #15
0
 public function setUp()
 {
     $connection = new MockDocumentSource();
     Connections::add('mockconn', array('object' => $connection));
     MockDocumentPost::config(array('meta' => array('connection' => 'mockconn')));
 }
Beispiel #16
0
<?php

use lithium\data\Connections;
use lithium\core\Libraries;
/**
 * Sets up Sqlite3 database for searching.
 */
Connections::add('li3_docs', array('type' => 'database', 'adapter' => 'Sqlite3', 'database' => Libraries::get('li3_docs', 'path') . '/resources/data/symbols.db'));
Beispiel #17
0
 * and encourage you to take advantage of multiple database technologies, choosing the most optimal
 * one for each task.
 *
 * As with other `Adaptable`-based configurations, each database configuration is defined by a name,
 * and an array of information detailing what database adapter to use, and how to connect to the
 * database server. Unlike when configuring other classes, `Connections` uses two keys to determine
 * which class to select. First is the `'type'` key, which specifies the type of backend to
 * connect to. For relational databases, the type is set to `'database'`. For HTTP-based backends,
 * like CouchDB, the type is `'http'`. Some backends have no type grouping, like MongoDB, which is
 * unique and connects via a custom PECL extension. In this case, the type is set to `'MongoDb'`,
 * and no `'adapter'` key is specified. In other cases, the `'adapter'` key identifies the unique
 * adapter of the given type, i.e. `'MySql'` for the `'database'` type, or `'CouchDb'` for the
 * `'http'` type. Note that while adapters are always specified in CamelCase form, types are
 * specified either in CamelCase form, or in underscored form, depending on whether an `'adapter'`
 * key is specified. See the examples below for more details.
 *
 * ### Multiple environments
 *
 * As with other `Adaptable` classes, `Connections` supports optionally specifying different
 * configurations per named connection, depending on the current environment. For information on
 * specifying environment-based configurations, see the `Environment` class.
 *
 * @see lithium\core\Adaptable
 * @see lithium\core\Environment
 */
use lithium\data\Connections;
/**
 * Uncomment this configuration to use MongoDB as your default database.
 */
Connections::add('default', array('development' => array('type' => 'MongoDb', 'host' => 'localhost', 'database' => 'apps_dev'), 'production' => array('type' => 'MongoDb', 'host' => 'localhost', 'database' => 'apps')));
 * specified either in CamelCase form, or in underscored form, depending on whether an `'adapter'`
 * key is specified. See the examples below for more details.
 *
 * ### Multiple environments
 *
 * As with other `Adaptable` classes, `Connections` supports optionally specifying different
 * configurations per named connection, depending on the current environment. For information on
 * specifying environment-based configurations, see the `Environment` class.
 *
 * @see lithium\core\Adaptable
 * @see lithium\core\Environment
 */
use lithium\data\Connections;
/**
 * Uncomment this configuration to use MongoDB as your default database.
 */
Connections::add('mongo', array('type' => 'MongoDb', 'host' => 'localhost', 'database' => 'bus'));
/**
 * Uncomment this configuration to use CouchDB as your default database.
 */
// Connections::add('default', array(
// 	'type' => 'http',
// 	'adapter' => 'CouchDb',
// 	'host' => 'localhost',
// 	'database' => 'my_app'
// ));
/**
 * Uncomment this configuration to use MySQL as your default database.
 */
Connections::add('default', array('type' => 'database', 'adapter' => 'MySql', 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'my_app', 'encoding' => 'UTF-8'));
 public function testResultSet()
 {
     Employees::config(array('meta' => array('connection' => 'test')));
     Companies::config(array('meta' => array('connection' => 'test')));
     Connections::get('test')->read('DROP TABLE IF EXISTS employees;');
     $sql = "CREATE TABLE employees (id int,title varchar(100))";
     Connections::get('test')->read($sql);
     for ($i = 1; $i < 9; $i++) {
         $sql = "INSERT INTO employees (id, title) VALUES ({$i}, 'Title {$i}')";
         Connections::get('test')->read($sql);
     }
     $employees = Employees::all();
     $cpt = 0;
     foreach ($employees as $employee) {
         $cpt++;
         $this->assertEqual($cpt, $employee->id);
     }
     $this->assertEqual(8, $cpt);
     $this->assertEqual(8, count($employees));
     Employees::reset();
     Companies::reset();
     $base = Libraries::get(true, 'resources') . '/tmp/tests';
     $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
     Connections::add('sqlite_file', array('type' => 'database', 'adapter' => 'Sqlite3', 'database' => "{$base}/sqlite_file.sq3", 'database' => ':memory:', 'encoding' => 'UTF-8'));
     Employees::config(array('meta' => array('connection' => 'sqlite_file')));
     Companies::config(array('meta' => array('connection' => 'sqlite_file')));
     Connections::get('sqlite_file')->read('DROP TABLE IF EXISTS employees;');
     $sql = "CREATE TABLE employees (id int,title varchar(100))";
     Connections::get('sqlite_file')->read($sql);
     for ($i = 1; $i < 9; $i++) {
         $sql = "INSERT INTO employees (id, title) VALUES ({$i}, 'Title {$i}')";
         Connections::get('sqlite_file')->read($sql);
     }
     $employees = Employees::all();
     $cpt = 0;
     foreach ($employees as $employee) {
         $cpt++;
         $this->assertEqual($cpt, $employee->id);
     }
     $this->assertEqual(8, $cpt);
     $this->assertEqual(8, count($employees));
     $this->_cleanUp();
     Connections::get('test')->read('DROP TABLE employees;');
     Connections::get('sqlite_file')->read('DROP TABLE employees;');
 }
Beispiel #20
0
<?php

define('LITHIUM_APP_PATH', dirname(__DIR__));
define('LITHIUM_LIBRARY_PATH', dirname(LITHIUM_APP_PATH) . '/libraries');
include __DIR__ . '/libraries.php';
use lithium\data\Connections;
/**
 * Setup test database
 */
Connections::add('test', array('test' => array('type' => 'database', 'adapter' => 'Sqlite3', 'database' => ':memory:', 'encoding' => 'UTF-8')));
Beispiel #21
0
 public function testRelationshipGeneration()
 {
     Connections::add('mock-source', $this->_testConfig);
     $from = 'lithium\\tests\\mocks\\data\\MockComment';
     $to = 'lithium\\tests\\mocks\\data\\MockPost';
     $from::config(array('connection' => 'mock-source'));
     $to::config(array('connection' => 'mock-source'));
     $result = $this->db->relationship($from, 'belongsTo', 'MockPost');
     $expected = compact('from', 'to') + array('name' => 'MockPost', 'type' => 'belongsTo', 'keys' => array('mockComment' => '_id'), 'link' => 'contained', 'conditions' => null, 'fields' => true, 'fieldName' => 'mockPost', 'init' => true);
     $this->assertEqual($expected, $result->data());
     Connections::config(array('mock-source' => false));
 }
Beispiel #22
0
 * key is specified. See the examples below for more details.
 *
 * ### Multiple environments
 *
 * As with other `Adaptable` classes, `Connections` supports optionally specifying different
 * configurations per named connection, depending on the current environment. For information on
 * specifying environment-based configurations, see the `Environment` class.
 *
 * @see lithium\core\Adaptable
 * @see lithium\core\Environment
 */
use lithium\data\Connections;
/**
 * Uncomment this configuration to use MongoDB as your default database.
 */
Connections::add('default', array('type' => 'MongoDb', 'host' => 'localhost', 'database' => 'dprblog'));
/**
 * Uncomment this configuration to use CouchDB as your default database.
 */
// Connections::add('default', array(
// 	'type' => 'http',
// 	'adapter' => 'CouchDb',
// 	'host' => 'localhost',
// 	'database' => 'my_app'
// ));
/**
 * Uncomment this configuration to use MySQL as your default database.
 */
// Connections::add('default', array(
// 	'type' => 'database',
// 	'adapter' => 'MySql',
Beispiel #23
0
<?php

use lithium\data\Connections;
Connections::add('default', array('type' => CONNECTION_TYPE, 'host' => array(CONNECTION), 'database' => CONNECTION_DB, 'login' => CONNECTION_USER, 'password' => CONNECTION_PASS));
Beispiel #24
0
 public function setUp()
 {
     $result = Memoize::$objectNames = array();
     Connections::add('default', array('type' => 'database', 'adapter' => 'Sqlite3', 'database' => LITHIUM_APP_PATH . '/tests/mocks/sqlite.db'));
 }
Beispiel #25
0
 public function tearDown()
 {
     foreach ($this->_preserved as $name => $config) {
         Connections::add($name, $config);
     }
 }
Beispiel #26
0
 public function testGridFsDeleteWithCustomPrefix()
 {
     $data = array('_id' => new MongoId());
     $db = new MongoDb($this->_testConfig + array('gridPrefix' => 'custom'));
     $db->server = new MockMongoConnection();
     $db->connection = new MockMongoConnection();
     Connections::add('temp', array('object' => $db));
     MockMongoPost::config(array('meta' => array('source' => 'fs.files', 'connection' => 'temp')));
     MockMongoPost::create($data, array('exists' => true))->delete();
     $this->assertIdentical(null, $db->connection->gridFsPrefix);
     MockMongoPost::config(array('meta' => array('source' => 'custom.files')));
     MockMongoPost::create($data, array('exists' => true))->delete();
     $this->assertIdentical('custom', $db->connection->gridFsPrefix);
     Connections::remove('temp');
 }
Beispiel #27
0
 * 
 * So add on libraries wishing to use Lithium Bootstrap, should consider
 * using these connection names in their models. They should also consider
 * prefixing their model $_meta['source'] values to use prefixed names for
 * tables/collections to avoid conflicts since using a conventional/default
 * connection will mean multiple libraries using the same database.
 * For example, if two libraries have a `User` model, they would conflict
 * if both used the default `users` table/collection. A better idea would
 * be to set $_meta['source'] = 'libName.users' or 'libName_users' etc.
 * Not only does this avoid conflict (since there can not be two library
 * directories by the same name), but it also immediately clues a developer
 * into which tables/collections are used by which library when looking
 * at the database.
 * 
*/
/**
 * But first, we'll add a default connection.
 * MongoDB makes the most sense here because it often does not
 * require a username and password. Lithium Bootstrap can be
 * used with any database, but MongoDB is the preferred database.
 */
Connections::add('li3b_mongodb', array('production' => array('type' => 'MongoDb', 'host' => 'localhost', 'database' => 'li3bootstrap'), 'development' => array('type' => 'MongoDb', 'host' => 'localhost', 'database' => 'li3bootstrap_dev'), 'test' => array('type' => 'database', 'adapter' => 'MongoDb', 'database' => 'li3bootstrap_test', 'host' => 'localhost')));
$appConfig = Libraries::get(true);
$connd = $appConfig['path'] . '/config/bootstrap/connections/*.php';
$conndFiles = glob($connd);
if (!empty($conndFiles)) {
    asort($conndFiles);
}
foreach ($conndFiles as $filename) {
    include $filename;
}
Beispiel #28
0
 * Locate and load Lithium core library files.  Throws a fatal error if the core can't be found.
 * If your Lithium core directory is named something other than `lithium`, change the string below.
 */
if (!(include LITHIUM_LIBRARY_PATH . '/lithium/core/Libraries.php')) {
    $message = "Lithium core could not be found.  Check the value of LITHIUM_LIBRARY_PATH in ";
    $message .= __FILE__ . ".  It should point to the directory containing your ";
    $message .= "/libraries directory.";
    throw new ErrorException($message);
}
use lithium\core\Libraries;
use lithium\data\Connections;
/**
 * Add the Lithium core library.  This sets default paths and initializes the autoloader.  You
 * generally should not need to override any settings.
 */
Libraries::add('lithium');
/**
 * Add the application.  You can pass a `'path'` key here if this bootstrap file is outside of
 * your main application, but generally you should not need to change any settings.
 */
Libraries::add('li3_tree', ['default' => true]);
/**
 * Load test dependencies
 */
Libraries::add('li3_behaviors');
Libraries::add('li3_fixtures');
/**
 * Setup test database
 */
Connections::add('test', ['type' => 'database', 'adapter' => 'MySql', 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'li3tree_test', 'encoding' => 'UTF-8']);
Beispiel #29
0
 public function testConnectionRemove()
 {
     $result = Connections::add('conn-to-remove', array('type' => 'Mock') + $this->config);
     $expected = $this->config + array('type' => 'Mock');
     $this->assertEqual($expected, $result);
     $result = Connections::get('conn-to-remove');
     $this->assertInstanceOf('lithium\\data\\source\\Mock', $result);
     Connections::remove('conn-to-remove');
     $result = Connections::get('conn-to-remove');
     $this->assertNull($result);
 }
Beispiel #30
0
 public function setUp()
 {
     Connections::add('mock', array('object' => $this->_db = new MockPostgreSql()));
     MockDatabasePost::config(array('meta' => array('connection' => 'mock')));
 }