Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Database Configuration

Documentation

Getting Started With Database Configuration

Introduction

Cygnite helps connecting with multiple databases seamlessly and running queries against database is very simple either using raw queries, Cyrus ActiveRecord ORM, and using Finders. Currently Cygnite's Cyrus ActiveRecord orm provides full supports of mysql database. Cygnite uses PDO database abstraction layer to connect with different database, meaning that you are also allowed to query against other popular databases.

Configuration

Cygnite makes configuring database connections extremely simple. The database configuration file for your application located at "src/Apps/Configs/database.php". You may define all your database connection parameters as well as which should be the default connection. Example connection configuration provided in this file. All database connections are lazy loaded by the model object.

  
 namespace Cygnite\Database;

 Configure::database(function ($config)
 {
    // set your default connection
    $config->default = 'db';

    $config->set([
        'db' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'port' => '',
            'database' => 'cygnite',
            'username' => 'root',
            'password' => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ]
        /*'db1' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'port' => '3607',
            'database' => 'search_engine',
            'username' => 'root',
            'password' => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ]*/
    ]);
 });


The above example demonstrate 'db' as default connection. You may add multiple connection configuration as db, db1, db2, db3 etc. Don't worry about the application performance, all database connections are lazy loaded by the model object. The connections are not active until you create a object of model class first time.

Retrieving Database Connection

You may access the specific connection using model object or directly extending \Cygnite\Database\ConnectionManagerTrait trait in your class.

Retrieving Connection By Model Object

Below basic model class.

  
 namespace Apps\Models;

 use Cygnite\Database\Schema;
 use Cygnite\Common\UrlManager\Url;
 use Cygnite\Database\Cyrus\ActiveRecord;

 class User extends ActiveRecord
 {
    //your database connection name
    protected $database = 'cygnite';

    //protected $tableName = 'user';

    protected $primaryKey = 'id';

    public function __construct()
    {
        parent::__construct();
    }
 }
 
 

Let us retrieve a specified database connection over model class as below.

 
 use Apps\Models\User;
 
 $user = new User();

 $connection = $user->queryBuilder()->resolveConnection(); // PDO object
 
 //or

 $connection = User::connection('cygnite'); // PDO object
 
 

You may also use "\Cygnite\Database\ConnectionManagerTrait" trait in your class to retrieve database connection as below.

 
 namespace Apps\Resources\Extensions;

 use Cygnite\Database\ConnectionManagerTrait;

 class PDOConnection 
 {
    use ConnectionManagerTrait;

   public function database($connection)
   {
      return $this->getConnection($connection);
   }
 }

 $connection = new \Apps\Resources\Extensions\PDOConnection();
 $connection->database('cygnite'); // PDO object
 
 
Follow Us On Facebook Twitter Google+ Linkedin
Released Under The MIT Public License. Copyrights @2012-2017. Powered by- Sanjoy Dey Productions.