CodeIgniter

Recently I started to write my CodeIgniter helper’s to use classes with static methods instead of writing global functions. This stops from having to check if the function name you are about to use already exists, and also allows the use of PHP namespaces if you desire.

Here is a example of how the helper functions are normally setup:

    if( !function_exists('longUniqueString')) {
            /**
          * Provides an extra long random unique String
          * 
          * @param $iterations (default 3)
          * @return String
          *          Random String
          */
         function longUniqueString($iterations = 3) {
             get_instance() -> load -> helper('string');
             $s = '';
             for($i = 0; $i < $iterations; $i++) {
                 $s .= random_string('unique');
             }

             return $s;
         }
    }

Here is the static method way of doing the same thing:

    class CMUtils {


        /**
         * Provides an extra long random unique String
         * 
         * @param $iterations (default 3)
         * @return String
         *          Random String
         */
        public static function longUniqueString($iterations = 3) {
            get_instance() -> load -> helper('string');
            $s = '';
            for($i = 0; $i < $iterations; $i++) {
                $s .= random_string('unique');
            }

            return $s;
        }
    }

The name of the php helper file can be anything you want. Let’s say this particular helper was called utils_helper.php . The code to call the helper would go something like this

    $this -> load -> helper('utils');
    $myStr = CMUtils :: longUniqueString();

0 1895

Occasionally it is necessary to obtain information about the table in a db schema to perform dynamic operations on the data. For example: if trying to save to a numeric db field, automatically validate to make sure the data being received is numeric. I ran across 2 items that I wanted to be able to get the information on for a column, but they were not available in the CI Meta Field Data.

The 2 pieces of information that I really needed to know were:

  1. Is this an auto incremented field?

  2. Does this field allow NULL?

I extended the CI_Model with MY_Model and added this method:

    protected function getTableMetaData($table) {


            //Replaced the default CI field_data so that we could determine if the field is a auto_increment

             /*
                Example object in the MetaData

                   stdClass::__set_state(array(
                 'Field' => 'coupon_no',
                 'Type' => 'bigint(20)',
                 'Null' => 'NO',
                 'Key' => 'PRI',
                 'Default' => NULL,
                 'Extra' => 'auto_increment',
              ))*/

    $fields = $this -> db -> query('DESCRIBE ' . $table) -> result();
            $md = array();
            foreach($fields as $field) {
                preg_match('/([a-zA-Z]+)((d+))?/', $field->Type, $matches);

                $type = (array_key_exists(1, $matches)) ? $matches[1] : NULL;
                $length = (array_key_exists(2, $matches)) ? preg_replace('/[^d]/', '', $matches[2]) : NULL;

                $F              = new stdClass();
                $F->name        = $field->Field;
                $F->type        = $type;
                $F->default     = $field->Default;
                $F->max_length  = $length;
                $F->primary_key = ( $field->Key == 'PRI' ? 1 : 0 );
                $F->auto_increment = strripos($field -> Extra, 'auto_increment') !== FALSE ? 1 : 0; 
                $F->allow_null = $field->Null === 'YES' ? TRUE : FALSE;

                $md[] = $F;
            }




        return $md;

    }

The 2 new properties on the field data are auto_increment which is a boolean and allow_null which is also a boolean. This is MySQL specific, so it would have to be tweaked for other DB’s.

0 3755

I recently made some updates to Ion Auth 2 to allow the user to login with their username or email address.  This was accomplished by creating another config setting called identity_alt.  This is optional and can be used to specify a secondary column which the user can use for their login. I am utilizing it for the email field, but really it can be linked to any other column on the user table.  This enhancement will force this secondary column to be unique for new users if it is specified.  

I have done a pull request to incorporate this into the core ion Auth code, and there is some debate on whether or not it should be there.  Please go there and weigh in, we would appreciate your feedback.

Feel free to grab the update from my fork if you would like to get going on it now.

0 3817

I recently needed to determine if a library was loaded in CI form my Model. I have the CI Rest library using the same model for a query and wanted to change the query if the system had a user logged in. I simply wanted to check and see if the Tank Auth library was loaded and grab the user id from it if it was. I ended up using the PHP function class_exists.

    if(class_exists('Ion_auth')) {
        //Include user specific query stuff here
    }

23 17679

I have been working on a project in PHP / CodeIgniter that I CANNOT wait to tell you guys about…

I have been developing some sweet CI libraries for this project and have decided to give some of them back to the community.   This is my first PHP open source contribution and my first solo Github project.

The CodeIgniter DataTable library handles all the DB operations for searching, sorting, paging, etc a jQuery DataTable.  The project can be found on GitHub CodeIgniter-DataTables.

This weekend I will be working on a CI example application that uses this library, and will update this post when it is available.  I have some other useful libraries that will be rolling out soon so stay tuned…
***Update: A full working example can be found here