Yii2 dev digest #2 Dec'13

Dec 4, 2013, 11:43:48 PM

It has gone some time after Yii2 Alpha announce, it increased activity in GitHub issues, a lot more people have tried to touch Yii2. And this is what I have noted.

$drivers = Driver::find()->with('cars')->asArray()->all();

will get populated nested arrays with releations

 $count = Yii::$app->db->createCommand('
      SELECT COUNT(*) FROM tbl_user WHERE status=:status
  ', [':status' => 1])->queryScalar();

  $dataProvider = new SqlDataProvider([
      'sql' => 'SELECT * FROM tbl_user WHERE status=:status',
      'params' => [':status' => 1],
      'totalCount' => $count,
      'sort' => [
          'attributes' => [
              'name' => [
                  'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
                  'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
                  'default' => SORT_DESC,
                  'label' => 'Name',
      'pagination' => [
          'pageSize' => 20,

Yii2 Hints

  • ActiveRecord::find($primaryKey)->asArray() code drops error

    Correct way MyActiveRecord::find()->where(['id' => $primaryKey])->asArray()->one();

  • Support for HTML 5 Input Types

    qiangxue > We will not add these input types to Html. You can easily use Html::input($type, $name, $value) to achieve these.

  • Reuse of Query object

    qiangxue > No, you cannot reuse the same query object for different queries. You need to create a new instance for each query.

  • Parsing an URL as a simple string

    klimov-paul > It is better to create new Request instance and fill it up via “setter” methods. In simplest case “setUrl” should be enough:

use yii\web\Request;
$request = new Request();
  • Dashes in table field names, If you have a table where the field name contains dashes, Gii will generate variables with dashes.

kartik-v > I think Yii proposes this to be handled by adhering to Naming Conventions for database fields

Read previous digests

Yii2 dev digest #1 Nov'13

comments powered by Disqus
Kuzminov "iJackUA"
Web Team Lead
at MobiDev (Kharkiv, Ukraine)
Code in PHP and Ruby, play with JS/Node.JS, evaluate Elixir, explore databases, use Ubuntu and MacOS, think about IT people and management