How to Include PHP ActiveRecord in WordPress as Global Feature ?>

How to Include PHP ActiveRecord in WordPress as Global Feature

This post for developers who used to ORM/ActiveRecord and not familiar with WordPress Query (like me) and want the ORM/ActiveRecord to be available in WordPress.

As per post’s title, this post regarding how you can include PHP ActiveRecord for global use – let say you have multiple plugins that use PHP ActiveRecord, you can’t load same PHP ActiveRecord classes / redeclared classed in an application or system – to be precise in PHP.

So the solution is to add in PHP ActiveRecord in {wordpress_installation}/wp-includes

Configurations

  1. Download PHP ActiveRecord and extract to {wordpress_installation}/wp-includes
  2. Create a new file in {wordpress_installation}/wp-includes/php-activerecord/ called AR.php and use the codes below
    <?php
    
    class AR {
    	static $conn;
    	static $model_directories;
    
    	public static function add_model_directories($list) {
    		if(!is_array($list)) {
    			return false;
    		}
    
    		if(!is_array(self::$model_directories)) {
    			self::$model_directories = array();
    		}
    
    		foreach ($list as $key => $value) {
    			if(!in_array($value, self::$model_directories)) {
    				self::$model_directories[] = $value;
    			}
    		}
    
    		return true;
    	}
    }
    
    function ar_init() {
    	$connections = array(
    		'development' => 'mysql://'.DB_USER.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME,
    	);
    
    	AR::$conn = ActiveRecord\Config::instance();
    	AR::$conn->set_model_directory(AR::$model_directories);
    	AR::$conn->set_connections($connections);
    }
    
  3. Update ActiveRecord\Config class in php-activerecord/lib/Config.php for method set_model_directory and get_model_directory. This to ensure we can include multiple model directories from multiple plugins.:
    	public function set_model_directory($dir)
    	{
    		if( is_array($dir) ) {
    			foreach( $dir as $k => $path ) {
    				if( !file_exists($path ) ) {
    					throw new ConfigException('Invalid or non-existent model directory: '.$path);
    				}
    			}
    		} else if( $dir  && !file_exists( $dir) ) {
    			throw new ConfigException('Invalid or non-existent model directory: '. $dir);
    		}
    
    		$this->model_directory = $dir;
    	}
    
    	public function get_model_directory()
    	{
    		return $this->model_directory;
    	}
    
  4. Include ActiveRecord.php and AR.php in {wordpress_installation}/wp-settings.php after Line 155:
    require( ABSPATH . WPINC . '/php-activerecord/ActiveRecord.php' );
    require( ABSPATH . WPINC . '/php-activerecord/AR.php' );
    
  5. Add the following codes after Line 241 to initialize PHP ActiveRecord:
    /**
     * Initialize PHP ActiveRecord
     */
    ar_init();
    

How to Use in Your Plugin

  1. Create your WordPress Plugin (you can use wppb.me)
  2. Open up your WordPress Plugin main file and add in the following code in order to include your model directory in AR class:
    AR::add_model_directories(array(
    	PLUGIN_PATH . 'includes' . DIRECTORY_SEPARATOR . 'models'
    ));
    
  3. Now create a sample model file which extend ActiveRecord\Model in your plugin’s model directory. Following are the sample codes:
    <?php
    
    class GTState extends ActiveRecord\Model {
    	static $table_name = 'gt_states';
    }
    
  4. Now you’re ready to use the GTState class in your view / controller.

I hope this trick will speed up the development progress.

Sorry WordPress Developers, I need a little hack to develop fast with PHP ActiveRecord, because I’m a beginner with WordPress, more things to learn.

References

  1. Load Multiple Model Directories
  2. PHP ActiveRecord Wiki

Leave a Reply

Your email address will not be published. Required fields are marked *

four × three =