How to store array values in Yii model property

In some scenarios, we require to store multiple values in your class attribute of your Yii model. We usually use this for showing check boxes or multiple select in your Yii form.

Add new Rule for your field “arrayField” in your model:

Overwrite your model’s “savemethod and change the array value as serialized string to store this in your database table.

How can I avoid direct access to codeigniter controllers, rather than routes?

When we are defining all your URLs through the routes.php file, the direct access to your code-igniter class will make accessible duplicated pages for the website. Duplicated pages affect SEO in large extents, so better to avoid direct access to the controller functions.

Step 1: Create a controller function for 404 response page

Create a separate controller and a normal public function in the controller folder. The following code inside the function makes the 404 error response page.

Step 2: Modify your routes.php file

At this point, you already have a good idea about the config/routes.php file, otherwise please have a look at: Documentation for Codeigniter Routes. The routes.php will be similar to the below code after modification:

There are mainly two points in your routes files:

  1. Overwrite your default 404 error page with defined controller page:  $route['404_override'] = 'errors/show_404';
  2. After defining all your routes, avoid further URL access by pointing all other URLs to the 404 controller page written before:  $route['(:any)'] = 'errors/show_404';

So, pointing all requests to the 404 page after parsing all valid routes will help you to avoid direct access to the controller with controller name and its method names.

How to add new action link column for Yii CGridView

It is very simple to create custom columns in CGridView. While defining the columns, you have to define an array with specific values for the custom column. To create a column which points to another controller with ID parameter, use following code:

To add header for the column, modify the same array with an additional parameter for the header.

The entire widget for CGridView in view file will look like this:


CodeIgniter Form Validation – How to get all error fields as an array

CodeIgniter by default is not providing you the error fields for a form as an array. To add this feature to your codeigniter form_valiation library, you have to extend the system library and overload as form_validation library.

Create the file: application/libraries/My_Form_validation.php file and add the following code:

In your controller use the following code to get your error fields as an array:


How to make Yii form fields mandatory manually

When you generate the model and CRUD files, usually all rules in the model is generated automatically and you are not bothered about the rules. But if you want to manually make fields in a form mandatory, you have to write a simple rule in the model rules function – array. Suppose I am having username, email and password in my form and I want all fields to be mandatory. The rules function will be similar to the below code:

Note the last array in the rule, where the first element has all fields (separated by comma) in the form which are required. The second element in the array is the validation class, which is “required” in our case.

Change your Yii view directory name for your controller

This article will assist you to use custom folder names rather than generating folder names for your controller. Suppose you have a model named: “MySample“, your generated controller name will be: “MySampleController”; your view folder name will be “views/mySample“. If you want to change your view name from views/mySample to views/sample, add the below function to your controller and change the folder name.

As simple as that 🙂

Sending Push Notification in Drupal with your plugin

It is difficult to implement even simple features in Drupal. Luckily Drupal is one of the CMS which have the largest number of plugins (modules). For implementing push notification feature, there are some good plugins available. We are selecting one plugin from the module gallery and will try to implement the push notification feature for our custom developed module.

Drupal’s “Push Notifications” Module

The module “Push Notifications” will help us to do the push notifications and device registrations for us. You can find and download the module at Drupal website. The module supports:

  1. Apple Push Notification Service (APNS)
  2. Google Cloud Messaging (GCM) and
  3. Cloud 2 Device Messaging (C2DM).

 How to Implement Push Notification Module?

Login to your admin panel and install your module. Follow the steps given below:

  1. Login to your admin panel.
  2. Go to your admin page:
  3. In the field “Install from a URL”, use the following URL: (you can find the latest version from the module page).
  4. Click on “Install” button.
  5. If everything works fine, you will get a message like “Installation was completed successfully.”
  6. Click on the “Enable newly added modules” link to go to the modules listing page.
  7. Go down to the page to see the “Push Notifications” plugins under “Others” category.
  8. Check the enabled column and save the configurations. Your push notification module is enabled now.
  9. Add the certificates and key in the page: Configuration > Web Serivces > Push Notifications > Configuration. For APNS certificate, click on “Generate new certificate string” button, rename your certificates to the displayed names and upload to the sites/all/modules/push_notifications/certificates folder.
  10. After saving your settings, you can test messages to the registered devices from “Send Push” tab.

Configure your module to add the dependency

Since your module is going to use push_notifications module, you have to add dependency for the module. The push_notifications module itself is dependent to the  services, so your module is dependent to both services and push_notifications. After adding dependency, your module info file will look like:

Send Push Notifications from your custom module

Now you can use the push_notifications module to send messages from your custom module. The below example will send a message to the node user when someone comments on the node.

There are many other useful functions in the push_notifications module. You can find more details about the module in our article: Drupal Push Notifications Module’s Functions List (API Manuel).

Drupal Push Notifications Module’s Functions List (API Manuel)

The goal of this article is to give developers’ awareness about the functions included in the push notification module that can be used for your Drupal module development. There are around 20 functions available in the push notification module from which I am selecting 6 functions that can be used for your module to send notifications to APNS and GCM/C2DN services:


This function is the most valuable, which can be used to send push notification messages to selected users. The function sends messages to both Android and iPhone devices. There are two parameters for this message: the first parameter accepts the user IDs as an array; the second parameter accepts the message.

Usage in your module:


This function will get all registered tokens either for Android or iPhone devices. The function accepts three parameters: the device type id, a particular language based registered tokens, the return format – as raw token value array or as token object.

Note: The default type ids’ are: 0 – Apple; 1 – Android.

You have to use only the first parameter if you are not using any specific languages for your applications. Usage:


This function will help you to get all devices registered under a particular user. The function accepts User ID and returns the device tokens for both Apple and Android devices.

Usage in your module:


The function will send messages to the tokens given through the Apple Push Notification Service. The function accepts two parameters: Array of IOS tokens; The payload array to send message.

Make sure that you pass your payload in the correct array format. A valid format of the payload with custom data is given below:


This function will send a message to the Android devices through the Google Cloud Messaging service by accepting Android device tokens. The parameters are same as the previous method. The payload format is slightly different from the APNS function.

A valid payload format is:


The application of this function is same as the previous function. The only difference is that this function will use the Android Cloud to Device Messaging service instead of GCM. The two parameters and its formats are the same like the previous function.

Note: For implementing push notification in your custom module, please read the article: Sending Push Notification from your Drupal Module.

Sending email from your Codeigniter application with Gmail SMTP

With the Codeigniter’s default email library, we can send email through our Gmail account. This will help us to avoid spam mails, generating from the Codeigniter application.

1. Create GMAIL Configuration for the email configuration

You only have to take care of your email configuration to implement email sending properly for your application. Open your  application/config/config.php file and add the following lines of code at the end of your config file:

2. Create a function to send email from your Codeigniter application

You can create this function as a helper or library according to the usage. I am here, creating the function as a private method in the controller, assuming only one controller will share this email function. The method code is as follows:

3. Call the function and use HTML template for the email

We created the configuration to send HTML emails. So you have to create a view file that holds the complete HTML for the email template. You can create PHP variables in the file and pass dynamic values from the controller. Create a sample email template as below:

Save the file at:  application/views/email.php. In your controller page function, use the following code to initialize the email body and send the email:

Make sure that your computer is open to the port 465. Hopefully you will receive an email from your gmail account. There is chance that the email generating from your application can be considered as insecure, and the gmail will block your access. Login to your gmail account and turn on the less secure app configuration to allow insecure applications to access gmail SMTP.

Basic Codeigniter Active Record Knowledge

Following information will give you an idea about how you can use the codeigniter active record class in your models.

You have to initialize your database in your model constructor with

Select Data

You can make a simple select query like:  SELECT * FROM table_name  with the following:

A select query with where condition:

For selecting a single row from a table with the codeigniter active record class:


Looping Data

The result()  method gives you the result as an array of objects. You can parse through the array as normal. Following are the usual two scenarios for the looping:

Insert, Update and Delete with Codeigniter Active Records

Use insert()  method for SQL insertion operation.

Update operation is same like insertion. The update()  method has a third parameter as where condition with which the updating rows will be selected.

Delete method delete()  is also same as the previous two methods. Here, the second parameter is the where clause which used to select the deleting rows.

For more details, please read through the official codeigniter user guide.