Skip to content

Instantly share code, notes, and snippets.

View rela589n's full-sized avatar

Yevhen Sidelnyk rela589n

View GitHub Profile
@rela589n
rela589n / scratch.sql
Created August 1, 2024 14:10
Postgresql group by overlapping date periods
WITH period_ranges AS (
SELECT
id,
tstzrange(period_start, period_end) AS period
FROM
your_table
), recursive_group AS (
SELECT
id,
period,
@rela589n
rela589n / source.php
Last active June 15, 2024 11:55
Non-blocking http requests in PHP
<?php
final class PricesHttpClient implements PricesClient
{
public function __construct(
// AMPHP http client (https://github.com/amphp/http-client)
private HttpClient $httpClient,
) {
}
@rela589n
rela589n / source.php
Created May 22, 2024 17:29
Doctrine stop removing foreign key
#[AsDoctrineListener(event: ToolEvents::postGenerateSchema)]
final readonly class QuestionForeignKeyListener
{
/** @throws SchemaException */
public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs): void
{
$schema = $eventArgs->getSchema();
$table = $schema->getTable('answers');
@rela589n
rela589n / gist:a38ac8d34acf8778a4e994dd4985bb9a
Created May 22, 2024 13:24
Manually initializing relationship on doctrine entity
// warning: this approach is error-prone in case if there's a doctrine's lazy-entity proxy!
// when trying to call getQuestion() method on such non-initialized object, "property not initialized" error will come up
trait ManyToOneOtherEntityRelationshipMappingTrait
{
protected Question $question;
#[ORM\Column(type: 'uuid')]
private Uuid $questionId;
@rela589n
rela589n / upgrade.md
Last active June 19, 2024 08:48
Upgrade project deps

Useful commands:

  • composer outdated (check for outdated packages);
  • composer why-not (check for version constraints not allowing us to upgrade);
  • composer recipes:update - update symfony-packages automatically;

In order to upgrade symfony to a new version, consider following 3-step the scenario:

Step 1:

@rela589n
rela589n / source.php
Last active April 3, 2024 11:13
Symfony Doctrine Type Dependency Injection
// Doctrine Type
final class YourCustomType extends Type
{
public const NAME = 'your_custom_type';
private YourCustomService $yourCustomService;
public function setYourCustomService(YourCustomService $yourCustomService): void
{
@rela589n
rela589n / gist:4abc6b61ce790057fe25dcbfa9d036e3
Created March 25, 2024 15:09
Doctrine orm Joined table inheritance LOCK FOR UPDATE
class ForUpdateOfTableWalker extends SqlWalker
{
public const FOR_UPDATE_OF_TABLE = 'ForUpdateOfTable';
public function walkSelectStatement(SelectStatement $AST): string
{
$sql = parent::walkSelectStatement($AST);
/** @var array $hint */
$hint = $this->getQuery()->getHint(self::FOR_UPDATE_OF_TABLE);
@rela589n
rela589n / services.yaml
Created March 6, 2024 14:59
Symfony Tagged Locator
services:
routable.event.bus:
class: Symfony\Component\Messenger\RoutableMessageBus
arguments:
- !tagged_locator { tag: 'messenger.bus', index_by: 'id' }
- '@event.bus'
@rela589n
rela589n / gist:cc2929c18c2655f6d22b2cd7b0a0e658
Created December 7, 2023 19:32
Remove event listeners with javascript
document.body.removeEventListener('mousedown',getEventListeners(document.body).mousedown[0].listener)
document.removeEventListener('copy',getEventListeners(document).copy[0].listener)
document.removeEventListener('paste',getEventListeners(document).paste[0].listener)
@rela589n
rela589n / CategoryRepository.php
Created December 7, 2023 09:05
postgresql dfs preorder tree traversal
public function findCategories(): array
{
// dfs preorder tree traversal
$sql = <<<'SQL'
WITH RECURSIVE preorder_categories_traversal
AS (SELECT category.id,
category.parent_id,
ARRAY [ROW (category.sort_order,category.id)] AS path
FROM categories category