diff --git a/README.md b/README.md index 48e2261..9090b4b 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,31 @@ $resolver = new Illuminate\CodeIgniter\CodeIgniterConnectionResolver($ci); Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver); ``` +To enable events dispatcher support: + +```php +// use our mock PDO class if PDO is not enabled on this server +if (!class_exists('PDO')) { + class_alias('Illuminate\CodeIgniter\FakePDO', 'PDO'); +} + +// pass all Laravel database queries through to CodeIgniter +$ci = get_instance(); +$resolver = new Illuminate\CodeIgniter\CodeIgniterConnectionResolver($ci); + +// create a new event dispatcher instance +$event_dispatcher = new Illuminate\Events\Dispatcher(); +//create a new database manager instance +$database_manager = new Illuminate\Database\Capsule\Manager(); +// attach the evebt dispatcher instance to the manager instance +$database_manager->setEventDispatcher($event_dispatcher); +// attach the event dispatcher to the eloquent +$database_manager->bootEloquent(); + +Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver); +``` + + ## License [MIT License](https://github.com/expressodev/laravel-codeigniter-db/blob/master/LICENSE) diff --git a/src/CodeIgniterConnection.php b/src/CodeIgniterConnection.php index 53fc6c2..63eb86d 100644 --- a/src/CodeIgniterConnection.php +++ b/src/CodeIgniterConnection.php @@ -21,6 +21,13 @@ public function __construct($ci) $this->tablePrefix = $this->ci->db->dbprefix; $this->useDefaultQueryGrammar(); $this->useDefaultPostProcessor(); + try { + $this->pdo = new PDO("{$this->ci->db->dbdriver}:host={$this->ci->db->hostname};dbname={$this->ci->db->database};charset={$this->ci->db->char_set}", $this->ci->db->username, $this->ci->db->password); + } catch(Exception $e) { + if ($this->ci->db->dbdriver == "mysqli") { + $this->pdo = new PDO("mysql:host={$this->ci->db->hostname};dbname={$this->ci->db->database};charset={$this->ci->db->char_set}", $this->ci->db->username, $this->ci->db->password); + } + } } /** @@ -74,7 +81,7 @@ protected function getDefaultPostProcessor() */ public function getPdo() { - throw new \BadMethodCallException('PDO is not supported by CodeIgniter database driver'); + return $this->pdo; } /**