Tag Archives: yii2

Yii2 Lookup Module

You may be familiar with Lookup and might have used it in Yii framework. It provides good functionality to manage all of the optional data in one place, such as ‘Yes’, ‘No’ values or ‘Male’, ‘Female’ or any other type of optional values that you want to provide it end-user in any format (checkbox, radio button or dropdown).

It basically provides you with manage both integer part, textual part, order and status. So with these options you have the abililty to easily handle all of the data in one place using web interface.

following is one of the screen shot for the Lookup:

You can download this module by clicking here.

To configure this module, please follow the following steps:

Step # 1:

To add a lookup table to your database, following is the sql for lookup:

Step # 2:

To access the lookup functionality anywhere in you application (either frontend or backend) follow the following steps:

In your main.php under config folder add the following:

Step # 3:
Following are the few usage of lookup functionality:

/*** dropdown list from lookup ***/

/*** RadioButton List ***/

/*** CheckBoxes List ***/

/*** Dropdown List from Lookup ***/


Download Yii2 Lookup Module

I hope this may be of some help 🙂

Happy Coding 🙂


SQL Queries: Select Query in Yii2

We have to add the following namespaces before using Query class:


Following is the list of methods used in SQL Queries:

Query Class
Example 1Example 2


Example 1Example 2

Example 1

Example 1

Example 1

Example 1

Query With Prepared Statements
Example 1

Happy Coding!

Yii2 mcrypt php extension required

After installing Yii2, most of the time you can get this type of error message:

Yii2 uses PHP extension mcrypt which supports a wide variety of block algorithms such as DES, TripleDES, Blowfish (default), 3-WAY, SAFER-SK64, SAFER-SK128, TWOFISH, TEA, RC2 and GOST in CBC, OFB, CFB and ECB cipher modes. Additionally, it supports RC6 and IDEA which are considered “non-free”. CFB/OFB are 8bit by default.

To solve this problem type the following command in your terminal:

after running the above commands, you are all set.

Happy Coding!

How to upload a file in Yii2

If you have worked in Yii1.1 then you will know how to upload a file in Yii, to upload the file in Yii2 is almost the same.

In this article we will be using table named contact with few attributes (id, name, pic) and for this we will go step by step:

Step 1:

Setup global parameter for fileUploadUrl in params array of your configuration file, such as:

Step 2:

Create a Controller that will handle file upload:


Step 3:

To render to View for the file upload form:


Step 4:

Now, set your Model as follows:


with setting all these, now you will be able to upload file, save it in upload folder and save the file name in database table filed.

Happy Coding!

Yii2 How to Configure a Theme

A theme is directory of view and layout files. Sometimes one application may have more than one theme defending upon the requirements of the application.

Lets say, you want to create a theme named ‘test‘ and configure it with your application then:

  • In your application root, create a directory named ‘themes
  • Inside it create another directory named ‘test‘ (this will be your theme directory)
  • Copy ‘layouts‘ directory from ‘views‘ directory in your application and paste it in your newly created theme directory ‘themes/test
  • Open your configuration file and paste the following lines:

In the above, pathMap defines a map of original paths to themed paths while baseUrl defines the base URL for resources referenced by theme files.

In our case pathMap is [‘@app/views’ => ‘@app/themes/test’]. That means that every view in @app/views will be first searched under @app/themes/test and if a view exists in the theme directory it will be used instead of the original view.

Happy Coding!

Yii2 Data Manipulation in Database

Data Manipulation is quite easy to do in Yii2 and most of the methods are the same as in Yii were but with slight variation in syntax.

Following are the example of various Active Record methods:

To insert a new customer record:

To update an existing customer record:

To update an existing customer record:

To delete an existing customer record:

To delete several customers:

To increment the age of ALL customers by 1:

Happy Coding!

Yii2 Active Record Querying from Database

Lets say we have a table named ‘Customer’ and following are the few examples of retrieving data from database.

Retrieve all records from ‘Customer’ table:

To return customers whose ID is 1, 2 or 3:

To return a single customer whose ID is 1:

To retrieve all *active* customers and order them by their ID:

To return a single customer whose ID is 1:

To index the result by customer IDs:

To return customers in terms of arrays rather than Customer objects:

Happy Coding!

Yii2 Unable to find debug data tagged with

When you install Yii2 and enable the debug module, some time you will often get this kind of error:

When you see this kind of error: it means your project ‘runtime’ directory is not writable by the code. To solve this issue, all you need to do is make the ‘runtime’ directory writable.

In Ubuntu:


After applying the above code, you are all set.

Happy Coding!