Monthly Archives: December 2014

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:

Or

After applying the above code, you are all set.

Happy Coding!