PHP code example of marekpetras / yii2-merge-table

1. Go to this page and download the library: Download marekpetras/yii2-merge-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marekpetras / yii2-merge-table example snippets




use yii\db\ActiveRecord;
use marekpetras\mergetable\MergeTableTrait;
use marekpetras\mergetable\MergeTableInterface;

class Report extends ActiveRecord implements MergeTableInterface
{
    use MergeTableTrait;

    /**
     * Get the default table name, substitutes the tableName function if not a merge table model
     * @return string default table name
     */
    public static function defaultTableName()
    {
        return 'report';
    }
}




$id = 15432;
$tableName = Report::ensureExists($id);

$sql = sprintf("
    LOAD DATA INFILE '%s'
    REPLACE
    INTO TABLE `%s`
    FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
    LINES TERMINATED BY '\n'
    ", $file, $tableName);

$rows = Yii::$app->db->createCommand($sql)->execute();



$id = 15432;
Report::setTableName(Report::tableNameMerge($id));

$query = Report::find();
/*
    SELECT * FROM report_15432;
*/




$ids = [15432,12344];
Report::setTableName(Report::tableNameMerge($id));

$query = Report::find();
/*
    CREATE TEMPORARY TABLE unique_temp_name LIKE report_model;
    ALTER TABLE unqiue_temp_name ENGINE=MERGE, UNION=(report_15432,report_12344) INSERT_METHOD=NO;

    SELECT * FROM unqiue_temp_name;
*/




$query = Report::find();
/*
    SELECT * FROM report;
*/