Skip to content

Instantly share code, notes, and snippets.

Avatar

Nick Tsai yidas

View GitHub Profile
View PHP CLI Benchmark script
<?php
$startTime = time();
$runSeconds = 5;
$rounds = 5;
$count = 0;
for ($i=0; $i<$rounds; $i++) {
while ((time()-$startTime) <= $runSeconds) {
serialize(['bar'=>'foo']);
$count ++;
@yidas
yidas / yii2-migrate-by-web-controller.md
Last active Jul 6, 2018
Yii2 Migrate command called by Web controller
View yii2-migrate-by-web-controller.md
public function actionMigrate()
{
    // Keep current application
    $oldApp = \Yii::$app;
    // Load Console Application config
    $config = require \Yii::getAlias('@app'). '/config/console.php';
    new \yii\console\Application($config);
    $result = \Yii::$app->runAction('migrate', ['migrationPath' => '@app/migrations/', 'interactive' => false]);
    // Revert application
@yidas
yidas / mysql-change-datadir.md
Created Jul 11, 2018
MySQL change data-dir for Ubuntu 16.04
View mysql-change-datadir.md

MySQL change data-dir for Ubuntu 16.04

1. Change mysql.cnf

/etc/mysql/mysql.conf.d/mysql.cnf:

set datadir = /home/mysql
@yidas
yidas / mysql-change-sqlmode.md
Last active Jul 12, 2018
[MySQL] Permanently change SQL Mode in MySQL (disable_strict_mode)
View mysql-change-sqlmode.md

[MySQL] Permanently change SQL Mode in MySQL (disable_strict_mode)

Set into [mysqld] of MySQL config, for Ubuntu your could create a file /etc/mysql/conf.d/disable_strict_mode.cnf:

[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION

SQL mode example: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

@yidas
yidas / yii2-actionError.md
Last active Aug 1, 2018
[Yii2] Yii2 sample code of Error Action handling such 404 errors
View yii2-actionError.md

[Yii2] Yii2 sample code of Error Action handling such 404 errors

/**
 * Error action
 *
 * @return void
 */
public function actionError()
{
@yidas
yidas / yii2-mailer-set-layout.md
Last active Aug 1, 2018
[Yii2] Yii2 mailer set or disable layout template
View yii2-mailer-set-layout.md

[Yii2] Yii2 mailer set or disable layout template

Yii2 Mailer is extended by yii-mail-basemailer, which the compose() would render an mail view with default layout.

Set or Disable Mailer layout

$htmlLayout

[
@yidas
yidas / nginx-80-to-443.config.md
Last active Nov 22, 2018
Ngnix 80 to 443 Configuration
View nginx-80-to-443.config.md
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com; 
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
@yidas
yidas / PHP-DateTime-shiftMonths.md
Last active Nov 26, 2018
Shift a giving months from your input datetime with month based
View PHP-DateTime-shiftMonths.md

Shift a giving months from your input datetime with month based

If the target month is smaller than the original month, the extra days will be discarded.

/**
 * Shift a giving months from your input datetime with month based
 * 
 * If the target month is smaller than the original month, the extra days will be discarded.
 * 
@yidas
yidas / sql-joins-diagram.md
Last active Dec 18, 2018
SQL Joins Explained Diagram
View sql-joins-diagram.md
@yidas
yidas / yii2-app-basic-env-switch.md
Created Jan 3, 2019
Yii2 App Basic Environment Setting Switch for WEB & Console
View yii2-app-basic-env-switch.md

Yii2 App Basic Environment Setting Switch for WEB & Console

Server Variable Solution

Add server variable condition for ./web/index.php & ./yii:

if (isset($_SERVER['APP_ENV']) && $_SERVER['APP_ENV']=='production') {