Migrating a Module From Yii1.x to Yii2.x

Sep 21, 2015

Yii is an amazing framework. As it has been iterated on version 2 is now out and is quickly picking up adoption. With this comes the need to migrate components, modules, and a whole slew of other logic. This blog is a few pointers that we picked up along the way while doing a forum migration. This was the first feature migration we did in house, it was an interesting experience.

  1. RTFM, this may seem a no-brainer but read the manual, specifically about the Yii1->2 changes. http://www.yiiframework.com/doc-2.0/guide-intro-upgrade-from-v1.html
  2. Use a source control system. GiT, SVN, Mercurial, etc.
  3. Run the tests, make change, run tests, refactor, run tests, commit, run tests
  4. Pick a specific part of logic / cord to replace. GridViews, DetailList, etc. Do one chunk at a time.
  5. Give back to the community, no one like that guy that takes and takes and never gives back.

One of the most obvious changes has been the inclusion of autoloading classes. Goodbye require_once(‘composer/autoloader.php’), hello namespacing. With this change the class names have become much more sane. No more ‘CBaseModel’, now BaseModel.

The model level logic has seen a substantial change as well. “…In 1.1, query building was scattered among several classes, including CDbCommand, CDbCriteria, and CDbCommandBuilder. Yii 2.0 represents a DB query in terms of a Query object that can be turned into a SQL statement with the help of QueryBuilder behind the scene…” (Yii2 documentation). Now queries are similar this example:

$modelData = packageNamecommonmodelsModelName::find()->andWhere([‘id’ => $userId])->one();

Ideally this would return the user’s data from the specified model.

Moving on the view level widgets have had a measurable change as well:

Old:

$this->widget('zii.widgets.grid.CGridView', array(

   'dataProvider'=>$dataProvider,

   'columns'=> array(

       'title',          // display the 'title' attribute

       'category.name',  // display the 'name' attribute of the 'category' relation

       'content:html',   // display the 'content' attribute as purified HTML

       array(            // display 'create_time' using an expression

           'name'=>'create_time',

           'value'=>'date("M j, Y", $data->create_time)',

       ),

       array(            // display 'author.username' using an expression

           'name'=>'authorName',

           'value'=>'$data->author->username',

       ),

       array(            // display a column with "view", "update" and "delete" buttons

           'class'=>'CButtonColumn',

       ),

   ),

));

New:

GridView::widget([

   'dataProvider' => $dataProvider,

   'columns'        => [

       ['class' => 'yiigridSerialColumn'],


       'title',        // display the 'title' attribute

       'category.name',// display the 'name' attribute of the 'category' relation

       'content:raw', // display the 'content' attribute as purified HTML

       [             // display 'create_time' using an expression

           'name' => 'create_time',

           'value' => 'date("M j, Y", $data->create_time)',

       ],

       ]             // display 'author.username' using an expression

           'name' => 'authorName',

           'value' => '$data->author->username',

       ],


       ['class' => 'yiigridActionColumn'], //CRUD UI elements

   ]

]);

The astute eye will notice that the base code has simply been updated to the current recommendations. the main change being how the widget is instantiated. The astute will also notice the new column’s `SerialColumn` and `ActionColumn`. These two handy classes provide serialized numbering and CRUD UI elements for the column data. DetailView, ItemView, etc have all been iterated on in this way.

That is all the time we have for today but we hope this has provided insightful. It is highly recommended you take a look at the documentation provided by the Yii team for even more details. http://www.yiiframework.com/doc-2.0/guide-intro-upgrade-from-v1.html

RECENT POSTS

Meet Sourcetoad, a 2023 Best Places to Work honoree

Meet Sourcetoad, a 2023 Best Places to Work honoree

Tampa Bay Business Journal — April 26, 2023 Sourcetoad has been selected as a 2023 Best Places to Work! We are thrilled to be among Tampa's top workplaces for the fourth year in a row, and we are grateful to each of our Toads for making Sourcetoad such a fantastic...

This Year in FinTech: Trends to Watch in 2023

This Year in FinTech: Trends to Watch in 2023

The financial services industry experienced rapid digitization throughout the COVID-19 pandemic, and growth of the financial technology market is expected to continue in 2023. The global fintech market grew to $158.9 billion in 2022, and projections estimate it will...

2023 Top HealthTech Trends

2023 Top HealthTech Trends

Technological innovation has played a vital role in the transformation and growth of the healthcare industry. Not only is the medical space an early adopter of emerging technologies, HealthTech is currently one of the fastest-growing industries worldwide. The global...