July 18 2017

Yii2 set wraptext on moonlandsoft phpexcel

On phpexcel you are allow to set “setWrapText(true)” property. but on Yii2 plugin moonlandsoft phpexcel it don’t have this property.

To set this property. you need to add extra code on the plugin.
Open Excel.php file. and then scroll to line 387 add the code below.
if (isset($column['wraptext']) &&  $column['wraptext'] == true) {
	$activeSheet->getStyle($col.$row)->getAlignment()->setWrapText(true);
}

August 29 2016

Yii2 number compare validation rules

Tagged Under :

Yii
In Yii2 compare two numbers attributes in operator function. you will found that it not working properly. This is because the wrongly “type” in compare validation rules.

You may need to set the type to number as below:
'type' => 'number'
Example:
['min_price', 'compare', 'compareAttribute' => 'max_price', 'operator' => '<', 'type' => 'number'],

July 27 2016

Yii2 ActiveRecord model use MySQL function

Tagged Under : ,

Yii
To use the MySQL function on Yii2 ActiveRecord Model you to add “\yii\db\Expression” on your script instead of you direct use the “DATE_ADD” function.

Example:
$model->date = new \yii\db\Expression('DATE_ADD(CURRENT_DATE, INTERVAL - 7 DAY)')

July 27 2016

Yii2 an absolute url image src

Tagged Under :

Yii
If you want to set full url or absolute url on Yii2 Html img, you may need to use “Url” helper this class on your page.

use yii\helpers\Url;
The way to use:
Html::img(Url::to('@web/images/logo.png', true));

March 25 2016

Yii2 always load bootstrap.js

Tagged Under :

Yii
Normally on Yii2 wouldn’t always load the bootstrap.js on our Web App, only when you use js dependent bootstrap widgets such as yii\bootstrap\Modal and etc.

You need add some code on the framework to always load the bootstrap.js on the Web App.
Open the file on the following directory:

December 02 2015

Yii2 cactiveform client validation not working with fancybox ajax

Tagged Under :

Yii
If using fancybox ajax display the Yii2 cactiveform you will found that the client validation are not working.

Its due to the form ID was invalid or duplicated in same page. what you need to do is assign a new form id inside cactiveform.

Example
$form = ActiveForm::begin([
  'enableClientValidation' => true,
  'id' => 'new_form_id'
]);

December 02 2015

Yii2 gridview column pass an array as param

Tagged Under :

Yii
In Yii2 now you are allow pass an array as param to gridview column for display the data. Below is the example how to pass array to gridview column.

Exmaple:
The $status is an array and $data is the model results.
'content' => function($data) use ($status) {
  return (isset($status[$data->status]) ? $status[$data->status] : $data->status);
},

Complete Example:
 $model->search(),
  'filterModel' => $model,
  'showOnEmpty'=>true,
  'columns' => [
    'id',
    'name',
    [
      'attribute' => 'status',
      'filter' => $status,
      'content' => function($data) use ($status) {
        return (isset($status[$data->status]) ? $status[$data->status] : $data->status);
      },
    ],
  ],
]); ?>

December 21 2014

Yii2 sql user table schema

Tagged Under :

Yii
In Yii2 they didn’t provide proper SQL user table schema for the developer.
Below was the Yii2 user table schema, and it can be run on MySQL Database.
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `auth_key` varchar(32) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `password_reset_token` varchar(255) NOT NULL,
  `email` varchar(100) NOT NULL,
  `status` smallint(10) NOT NULL,
  `role` int(11) NOT NULL,
  `created_at` int(11) NOT NULL,
  `updated_at` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);
And the data.
INSERT INTO `user` (`id`, `username`, `auth_key`, `password_hash`, `password_reset_token`, `email`, `status`, `role`, `created_at`, `updated_at`) VALUES
(1, 'admin', '', '$2y$13$uqe3LPW9ya3RZhynJpPN5um9fvdxUmoqjOqQBJDdIDXSKxRZB5bPu', '', '', 10, 0, 0, 0);
Now you can login with username admin and password 123qwe

April 14 2014

Yii2 send email through swiftmailer expansion

Tagged Under :

Yii
In here i wouldn’t introduced what is swiftmailer, who interested can find the information by yourself. In below will explain how to send email with SMTP through Yii2 swiftmailer.

Inside the config folder. Open the web.php configuration file. You will see the following code as below:
$config = [
  ......
  'components' => [
    ......
    'mailer' => [
      'class' => 'yii\swiftmailer\Mailer',
      'useFileTransport' => true, //for the testing purpose, you need to enable this
    ]
  ]
]
If useFileTransport is enable, all the email wouldn’t send out and the email contents will keep a copy at ‘runtime/mail‘ folder.