DataMapper OverZealous Edition


Writing an Extension

It is incredibly simple to write your own extension. This will walk you through the basic process, by recreating the included extension for JSON encoding and decoding.

If you'd like to share your own extensions, please let me know on the CodeIgniter forum. Eventually, I'd like to start building a repository of extensions.

Planning an Extension

An extension is simply a class with one or more exposed methods. These methods have a few minor rules.

You can include private methods simply by prepending the method with an underscore (_). These methods will not be exposed in the DataMapper model, but will be available for your use.

Creating the Extension Class

The class is a normal PHP class. The file should follow the same naming rules as a CodeIgniter Library, naming the file the lowercase form of the class name, ending with .php. Store the class definition in the application/datamapper.

application/datamapper/json.php

1 2 3 4 5 6 7
<?php class Json { } /* End of file json.php */ /* Location: ./application/datamapper/json.php */

Adding Public Methods

We want to be able to easily convert a DataMapper model into a JSON object. We'll use PHP5's json_encode for the heavy lifting, but we need to get the data out of the large and complex DataMapper model first.

The first argument to each public method is always the $object in use.

application/datamapper/json.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php class Json { function to_json($object) { $fields = $object->fields; $result = array(); foreach($fields as $f) { $result[$f] = $object->{$f}; } $json = json_encode($result); if($result === FALSE) { return FALSE; } return $json; } } /* End of file json.php */ /* Location: ./application/datamapper/json.php */

Now we can call $object->to_json(), and we'll get a JSON encoded view of $object.

Creating Custom Validation Rules

See Validation - Custom Validation for more information about custom validation rules.

application/datamapper/custom_rules.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php class Custom_Rules { function __construct() { $CI =& get_instance(); // load in the custom rules language file. $CI->lang->load('custom_rules'); } // Validation prepping function to encrypt passwords function rule_exact_length($field, $param) { // Check if field value is the required length if (strlen($this->{$field}) == $param) { return TRUE; } // Field value is not the required length return FALSE; } } /* End of file custom_rules.php */ /* Location: ./application/datamapper/custom_rules.php */

To use this rule, we have to add 'custom_rules' to our config or $extensions variable on a specific class. Then simply add 'exact_length' to a field's rules.

Accepting Load-Time Options

If you want to allow load-time options, simple accept a value in the constructor.

1 2 3 4 5 6 7 8 9 10 11 12 13
<?php class Custom_Rules { function __construct($options) { foreach($options as $k => $v) { $this->{$k} = $v; } $CI =& get_instance(); // load in the custom rules language file. $CI->lang->load('custom_rules'); } // ...

 

To learn more, take a look a the included extensions.