James Galley

Web application developer

PHP, Yii, MySQL, Node.js - JavaScript, HTML, CSS & SASS - Linux, AWS & serverless

Converting Yii 1 models to Yii 2

I'm a Yii Developer based in Manchester who offers Yii 1 update services, and I've recently been upgrading a number of Yii 1 applications to Yii 2.

A key aspect of converting Yii 1 to Yii 2 is updating the models, an area where the format of the framework is quite different.

Version 1 of the Yii Framework was released in 2008, and is now approaching end-of-life in December 2020.

Lots of businesses are still using bespoke applications built in Yii 1, and a big focus for myself and those businesses during 2019 and through 2020 will be upgrading Yii 1 applications to Yii 2 or to another modern PHP framework like Laravel.

What's involved in converting Yii 1 models to Yii 2?

Models are a key aspect of a Model-View-Controller (MVC) framework like Yii. A model represents an object, usually an item (a row) from a database table, or a form object, both of which have properties and methods which define its structure and purpose.

When converting a Yii 1 application to Yii 2, converting the models is a key part of the process, and for a large established application is not an insignificant task.

Namespaces & Class names

Yii 2 introduced PHP Namespaces so when converting a Yii 1 model to Yii 2 you'll need to specify the namespace of your models. You'll also need to include other classes used within each model by using the use statement, and of course extend the right Yii framework base Model class.

<?php

namespace app\models;

use Yii;
use app\models\AnotherModel; // Another model used within this class

class MyModel extends \yii\db\ActiveRecord
{
    ...

Validation rules

While similar conceptually, the syntax and naming of model validation rules has changed between Yii 1 and Yii 2. You'll need to update your rules() method, and the use of any CValidator or inherited classes to use the new Yii 2 equivalents.

Relations

Active Record relationships are an area of models which has changed dramatically when updating from Yii 1 to Yii 2.

In Yii 1, the relations() method defines the Active Record relationships, but this has been deprecated in Yii 2 and replaced with individual methods which define a relation.

Furthemore, the definition of relations in Yii 1 as MANY_MANY HAS_MANY HAS_ONE and BELONGS_TO has been simplifed in Yii2 as Active Record methods of hasMany() and hasOne().

Search method

In Yii 1 the model's search() method defined the query and filters used for DataProviders fed into data widgets like GridView and ListView. The same model class was used both as a true representation of a model instance, and as a search instance. This was improved and clarified in Yii2 by separating search models into their own class inheriting the parent model, allowing for individual definition of searchable attributes and valdiation rules.

Therefore to convert from Yii 1 to Yii 2 you'll want to create a new search model class and move the search method into the new model, along with new validation rules as appropriate.

<?php

namespace app\models;

use app\models\Item;

class ItemSearch extends Item
{
    public function rules()
    {
        ... rules from the original model, rewritten in Yii 2 syntax of course
    }

    public function search($params)
    {
        ... Migrate and convert existing Yii 1 search method into Yii 2 query format
    }
}

Other Yii 1 to Yii 2 changes

There are numerous other minor changes, such as the tableName() method which is now static, and the requirements for beforeSave() and other hook methods.