1. Go to this page and download the library: Download kak/clickhouse 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/ */
$sql = 'SELECT
user_id, sum(income) AS sum_income
FROM stat
GROUP BY event_date
WITH TOTALS
LIMIT 10
';
/** @var \kak\clickhouse\Connection $clickhouse */
$clickhouse = \Yii::$app->clickhouse;
$command = $clickhouse->createCommand($sql);
$result = $command->queryAll();
var_dump($command->getMeta()); // columns meta info (columnName, dataType)
var_dump($command->getTotals()); // get totals rows to read
var_dump($command->getData()); // get rows data
var_dump($command->getRows()); // rows count current result
var_dump($command->getCountAll()); // rows count before limit at least
var_dump($command->getExtremes());
var_dump($command->getStatistics()); // stat query
//or
$command = $clickhouse->createCommand($sql);
$result = $command->queryAll($command::FETCH_MODE_ALL);
var_dump($result);
use kak\clickhouse\Query;
$command = (new Query());
// ...
$command->withTotals();
// or
$command->withCube();
// or
$command->withRollup();
/** @var \kak\clickhouse\Connection $client */
$client = \Yii::$app->clickhouse;
$sql = 'select * from stat where counter_id=:counter_id';
$client->createCommand($sql, [
':counter_id' => 122
])->setOptions([
'max_threads' => 2
])->queryAll();
// add options use method
// ->addOptions([])
use kak\clickhouse\Query;
// ...
$db = \Yii::$app->clickhouse;
$query = new Query();
// first argument scalar var or Query object
$query->withQuery($db->quoteValue('2021-10-05'), 'date1');
$query->select('*');
$query->from('stat');
$query->where('event_stat < date1');
$query->all();
/*
WITH '2020-07-26' AS date1 SELECT * FROM stat WHERE event_stat < date1
*/
use yii\base\Model;
class Stat extends Model
{
public $event_date; // Date;
public $counter_id = 0; // Int32,
public function save($validate = true)
{
/** @var \kak\clickhouse\Connection $client */
$client = \Yii::$app->clickhouse;
$this->event_date = date('Y-m-d');
if ($validate && !$this->validate()) {
return false;
}
$attributes = $this->getAttributes();
$client->createCommand(null)
->insert('stat', $attributes)
->execute();
return true;
}
}
use kak\clickhouse\ActiveRecord;
use app\models\User;
class Stat extends ActiveRecord
{
// pls overwrite method is config section !=clickhouse
// default clickhouse
public static function getDb()
{
return \Yii::$app->clickhouse;
}
public static function tableName()
{
return 'stat';
}
// use relation in mysql (Only with, do not use joinWith)
public function getUser()
{
return $this->hasOne(User::class, ['id' => 'user_id']);
}
}