Yii2 dev digest #2 Dec'13 > stdout.in Ievgen Kuzminov IT blog

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' => [
              'age',
              '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();
$request->setUrl('/site/contact');
Yii::$app->urlManager->parseRequest($request);
Yii::$app->mail->compose('contact/html')
  ->setFrom('from@domain.com')
  ->setTo($form->email)
  ->setSubject($form->subject)
       ->send();
  • 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
Ievgen
Kuzminov "iJackUA"
Web Team Lead
at MobiDev (Kharkiv, Ukraine)
Code in Ruby and Elixir, but still love PHP. Explore ES6 and Vue.js. Explore databases, use Ubuntu and MacOS, think about IT people and management

Notes


Ansistrano- ansistrano.deploy and ansistrano.rollback are Ansible Galaxy roles to easily manage the deployment process for scripting applications such as PHP, Python and Ruby. It's an Ansible port for Capistrano.