Format GridView Footer as Currency

You can use YII2’s built in formatter to format any arbitrary value you like. For example, you may want to format the footer of a grid view as currency. When specifying the cell, you can set the format of that cell. However, while you can control what will be in the footer for that column, GridView does not have a method of formatting the footer values.

Here is a simple method to format any value.

  1. $formatter = new \yii\i18n\Formatter;
  2. $formatted_value = $formatter->asCurrency($value);

and Where greater than less than

When setting up an ActiveDataProvider in search function, you may need to pass a value and have it evaluated and less then (<) or greater than (>). I did not find this info straight forward. But, it is not hard.

Here is an example:

  1. $query->andWhere('table_name.date_created >= :date_val',[':date_val' => $_GET['date_val']);

This utilizes parameter binding, so it is safe to pass the $_GET parameter into it.

You will not need to use ‘table_name’ if you are not using a join clause.

More likely, you will use something like $this->date_val or $this->getAttribute(‘date_val’) rather than $_GET[‘date_val’].

YII2 Gridview limiting number rows per page

Using GridView::widget the default number of rows per page is 20. This can be adjusted. This is done when defining the dataProvider.

In the search model, modify dataProvider as follows:

  1. $dataProvider = new ActiveDataProvider([
  2.     'query' => $query,
  3.     'pagination' => [
  4.         'pageSize' => 50,
  5.     ],
  6. ]);

This will set the number of rows per page to 50.

Send e-mail from YII2 using PHP mail

To enable sending mail from your YII2 application using PHP mail function, make the following adjustment in the YII2 config file.

Find the following:

  1. 'mailer' => [
  2.             'class' => 'yii\swiftmailer\Mailer',
  3.             'viewPath' => '@common/mail',
  4.             // send all mails to a file by default. You have to set
  5.             // 'useFileTransport' to false and configure a transport
  6.             // for the mailer to send real emails.
  7.             'useFileTransport' => true,
  8.         ],

This will be under the components parameters in the config. If using advanced YII2 instance, the file will be in common/main-local.php by default.

Change the code to this:

  1. return [
  2.     'components' => [
  3.         ...
  4.         'mailer' => [
  5.             'class' => 'yii\swiftmailer\Mailer',
  6.             'viewPath' => '@common/mail',
  7.             'transport' => [
  8.                 'class' => 'Swift_MailTransport',
  9.             ],
  10.             'useFileTransport' => false,
  11.         ],
  12.  
  13.     ],
  14. ];

useFileTransport must be changed to false and transport is set to use Swift_MailTransport.

See the following site for info on swiftmailer transport types.

http://swiftmailer.org/docs/sending.html#using-the-mail-transport