clickhouse-mapper / clickhouse-mapper
Requires
- php: >=7.4
- ext-json: *
- doctrine/annotations: 1.13.2
- friendsofphp/php-cs-fixer: v3.0.0
- php-di/php-di: 6.3.5
- smi2/phpclickhouse: 1.4.1
- symfony/cache: 4.4.*
- symfony/console: v4.*
- symfony/yaml: v4.4.*
- webmozart/assert: 1.10.0
Requires (Dev)
- phpunit/phpunit: 9.5.10
This package is not auto-updated.
Last update: 2024-04-23 20:02:07 UTC
README
/**
* @CommonInfo(tableName="order_history")
*/
class Order
{
/**
* @PropertyColumn
* @CustomEvaluationRule(rule="dateTimeToString", class="Entity\Rules\DateTimeEvalRule")
*/
private DateTime $createdAt;
/**
* @PropertyColumn
*/
private int $orderId;
/**
* @PropertyColumn
*/
private int $personalId;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $isCallEvent = null;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $isStatusChanged = null;
/**
* @PropertyColumn
*/
private int $countryId;
/**
* @PropertyColumn
* @Nullable
*/
private ?float $bill = null;
/**
* @PropertyColumn
* @Nullable
*/
private ?string $currency = null;
/**
* @PropertyColumn
* @Nullable
*/
private ?float $exchangedUsdBill = null;
/**
* @PropertyColumn
*/
private int $offerId;
/**
* @PropertyColumn
* @CustomEvaluationRule(rule="dateTimeToString", class="Entity\Rules\DateTimeEvalRule")
*/
private DateTime $occurredAt;
/**
* @PropertyColumn
* @Nullable
*/
private ?string $webmasterLanding = null;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $webmasterId = null;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $webmasterSource = null;
/**
* @PropertyColumn
* @Nullable
*/
private ?string $city = null;
/**
* @PropertyColumn
* @Nullable
*/
private ?float $price = null;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $quantity = null;
/**
* @EmbeddedProperty(embeddedClass="Entity\Status")
*/
private Status $status;
public function __construct(
DateTime $createdAt,
DateTime $occurredAt,
int $orderId,
int $personalId,
int $countryId,
int $offerId,
Status $status
) {
$this->createdAt = $createdAt;
$this->occurredAt = $occurredAt;
$this->orderId = $orderId;
$this->personalId = $personalId;
$this->countryId = $countryId;
$this->offerId = $offerId;
$this->status = $status;
}
}
В примере выше указана некоторая сущность Order и аннотации маппера к ней, данная сущность имеет вложеннный объект типа Status (помечен аннотацией Embedded):
/**
* @EmbeddedClass
*/
class Status
{
/**
* @PropertyColumn
* @Nullable
*/
private ?int $statusProcessing;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $statusAccepted;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $statusShipped;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $statusDelivered;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $statusBuyout;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $statusReturn;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $statusSpam;
/**
* @PropertyColumn
* @Nullable
*/
private ?int $statusCanceled;
public function __construct(
int $statusProcessing,
int $statusAccepted,
int $statusShipped,
int $statusDelivered,
int $statusBuyout,
int $statusReturn,
int $statusSpam,
int $statusCanceled
) {
$this->statusProcessing = $statusProcessing;
$this->statusAccepted = $statusAccepted;
$this->statusShipped = $statusShipped;
$this->statusDelivered = $statusDelivered;
$this->statusBuyout = $statusBuyout;
$this->statusReturn = $statusReturn;
$this->statusSpam = $statusSpam;
$this->statusCanceled = $statusCanceled;
}
}
Давайте соберем наш объект типа Order:
$order = new Order(
new DateTime('-5 days'),
new DateTime(),
10,
20,
30,
40,
new Status(
0,
1,
0,
0,
0,
0,
0,
0
)
)
Теперь, чтобы посчитать и добавить данный объект на запись в ClickHouse, с учетом вложенной сущности Status, достаточно вызвать менеджер сущностей:
// Вызываем менеджер сущностей
$entityManager = Mapper::getEntityManager();
// Добавляем нашу сущность $order созданную выше
$entityManager->attach($order);
// Для записи в ClickHouse
$entityManager->release();
Внутри метода attach будет произведена обработка сущности с учетом всех аннотаций.
В маппере присутствует 6 типов аннотаций:
@CommonInfo(tableName="some_table",viewName="some_view") (Общая информация о сущности, название таблицы и view)
@CustomEvaluationRule(rule="convertDateTime",class="Utils\DateTimeConverter") (Правило переcчета поля сущности, например перед записью в ClickHouse перевести DateTime в строку)
@EmbeddedClass (Маркер вложенной сущности)
@EmbeddedProperty(embeddedClass="Entity\SomeEmbedded") (Поле ссылается на вложенную сущность)
@Nullable (Поле класса и таблица в ClickHouse имеет тип Nullable)
@PropertyColumn(tableColumn="occurred_at",viewColumn="occurred") (Соответствие между полем сущности и таблицей в ClickHouse, если не указаны параметры в конструкторе аннотации то по-умолчанию берется название поля сущности через "snake case")