Skip to content

Instantly share code, notes, and snippets.

@cetver
cetver / gist:4fd7b52fd9fe3820c4fe
Last active August 29, 2015 14:12
Postgresql: list of columns in select
select string_agg('"table name|alias".' || '"' || column_name || '"' || ' AS "alias' || column_name || '"', E',\n')
from information_schema.columns
where
table_schema = 'public' AND
table_name = 'table_name'
group by table_name
@cetver
cetver / MacHashSHA1
Created November 17, 2014 15:42
Yii2 security component with sha1 hmac algorythm
<?php
namespace common\components;
use yii\base\Security;
use yii\base\InvalidConfigException;
use yii\helpers\StringHelper;
/**
*
var template =
'<div class="row">' +
'<div class="col-xs-2 col-sm-1 align-right">' +
'<span class="{ icon_class }"></span>' +
'</div>' +
'<div class="col-xs-7 col-sm-9">' +
'<p>{message}</p>' +
'<time datetime=""><{when_created}</time>' +
'</div>' +
'<div class="col-xs-3 col-sm-2 align-right-fxd">' +
@cetver
cetver / gist:20f54a59e0a024678428
Last active August 29, 2015 14:04
Postgresql: split month by hours
with dates as (
select
(date_trunc('MONTH', NOW()))::timestamp without time zone --first date of month
+ concat(
generate_series(
0,
24
* to_char((date_trunc('MONTH', NOW()) + INTERVAL '1 MONTH - 1 day'), 'DD')::smallint --last day of month
-1
), --hours in month
<?php
function getClosest($needle, $haystack, $highestOnEqual = true) {
if ($highestOnEqual === true) {
rsort($haystack, SORT_NUMERIC);
} else {
asort($haystack, SORT_NUMERIC);
}
$value = current($haystack);
foreach ($haystack as $val) {
if (abs($needle - $value) > abs($val - $needle)) {
@cetver
cetver / gist:6714949
Created September 26, 2013 14:25
Yii https url rules
<?php
foreach ($httpsRules as $pattern => $route) {
if (is_string($route) === true || (is_array($route) && isset($route['class']) === false)) {
if (parse_url($pattern, PHP_URL_SCHEME) === 'http') {
$httpsRules = array_merge($httpsRules, array(str_replace('http', 'https', $pattern) => $route));
}
}
}
@cetver
cetver / gist:6591021
Created September 17, 2013 07:23
Yii PDO::FETCH_GROUP
<?php
$sth = Yii::app()->getDb()->getPdoInstance()->prepare('
SELECT
when_created,
type_id,
amount
FROM rates
WHERE employee_id = :employee_id
');
$sth->execute(array(