Implementing Crystal in Codeigniter

Crystal as a library is inspired by the database class in Codeigniter. So from the very beginning Crystal plays well with Codeigniter. This tutorial will teach you how to replace the default Codeigniter database class with Crystal.

Paste Crystal in application/libraries/.

In the same directory create a new file called MY_Model.php with this structure:

class MY_Model extends Model 
{
 
	function MY_Model() 
	{
		parent::Model();
		define('DS', DIRECTORY_SEPARATOR);
		require_once APPPATH . DS .  'libraries' . DS . 'Crystal' . DS . 'Crystal.php';
		$this->db = Crystal::db();
	}

}


In Crystal 0.3 you can add your configuration options in application/config/database.php instead of the default path_to_crystal/config/database.php.

class MY_Model extends Model 
{
 
	function MY_Model() 
	{
		parent::Model();
		define('DS', DIRECTORY_SEPARATOR);
		require_once APPPATH . DS .  'libraries' . DS . 'Crystal' . DS . 'Crystal.php';
		
		// You can use Codeigniter's database config file with Crystal like this
		$this->db = Crystal::db(APPPATH . DS . 'config' . DS . 'database.php');
		//  The only thing that needs to be changed in the configuration file is this line:
		// From $db['default']['dbdriver'] = ""; to $db['default']['driver'] = "";
	}

}

And that's all.

class Blogmodel extends MY_Model 
{

    function Blogmodel()
    {
        parent::MY_Model();
    }
    
    function get_posts()
    {
       return $this->db->get('entries')->fetch_all();
       
    }

  
}