Skip to content

Commit 9da0ccf

Browse files
author
arutyunyan
committed
Fix #380. Добавил триггеры на insert
1 parent 96f93bd commit 9da0ccf

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change log
22

3+
## v7.3.14 - July 22, 2022
4+
5+
- Добавил триггеры на добавление записей
6+
7+
---
8+
39
## v7.3.13 - June 24, 2022
410

511
- Рефакторинг макросов

src/db/Container.php

+24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class Container
1313
* @var array контейнер подключений
1414
*/
1515
private static $items;
16+
/**
17+
* @var array контейнер триггеров
18+
*/
19+
private static $triggers;
1620
/**
1721
* Создает контейнер с подключениями
1822
* @param array $dbList
@@ -38,4 +42,24 @@ public static function get($connection)
3842
{
3943
return isset(self::$items[$connection]) ? self::$items[$connection] : null;
4044
}
45+
/**
46+
* Добавляет триггер
47+
* @param string $modelClass
48+
* @param string $type
49+
* @param \Closure $callback
50+
*/
51+
public static function addTrigger($modelClass, $type, $callback)
52+
{
53+
self::$triggers[$modelClass][$type] = $callback;
54+
}
55+
/**
56+
* Получает триггер
57+
* @param string $modelClass
58+
* @param string $type
59+
* @return \Closure|null
60+
*/
61+
public static function getTrigger($modelClass, $type)
62+
{
63+
return isset(self::$triggers[$modelClass][$type]) ? self::$triggers[$modelClass][$type] : null;
64+
}
4165
}

src/manager/TableEntityManager.php

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private function saveEntity($entity)
6565
$entity->init();
6666
$entity->addInsert($entity->query());
6767
$entity->getQuery($entity->query())->addInto($entity->table());
68+
$row = [];
6869
foreach ($entity->getPublicProperties() as $property) {
6970
$propertyName = $property->getName();
7071
if (is_subclass_of($entity->$propertyName, ActiveRecord::class)) {
@@ -74,6 +75,7 @@ private function saveEntity($entity)
7475
}
7576
else {
7677
$entity->getQuery($entity->query())->addValue($propertyName, $entity->$propertyName);
78+
$row[$propertyName] = $entity->$propertyName;
7779
}
7880
}
7981

@@ -91,6 +93,10 @@ private function saveEntity($entity)
9193

9294
$entity->getDb()->getPdo()->exec($entity->getRawSql());
9395

96+
if ($entity->hasTrigger('insert')) {
97+
$entity->runTrigger('insert', $row);
98+
}
99+
94100
$entity->setLastInsertId($entity->getDb()->getPdo()->lastInsertId());
95101
}
96102
/**

src/syntax/ActiveRecord.php

+33
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ReflectionProperty;
1313
use suql\core\Scheme;
1414
use suql\core\SmartDate;
15+
use suql\db\Container;
1516
use suql\manager\TableEntityManager;
1617
use suql\syntax\field\Field;
1718
use suql\syntax\field\Raw;
@@ -176,6 +177,38 @@ public function save()
176177
$entityManager->persist($this);
177178
$entityManager->run();
178179
}
180+
/**
181+
* Вешает триггер
182+
* @param string $type
183+
* @param \Closure $callback
184+
*/
185+
public static function trigger($type, $callback)
186+
{
187+
Container::addTrigger(static::class, $type, $callback);
188+
}
189+
/**
190+
* Проверяет есть ли какой-нибудь триггер
191+
* @param string $type
192+
* @return boolean
193+
*/
194+
public function hasTrigger($type)
195+
{
196+
if (!is_null(Container::getTrigger(static::class, $type))) {
197+
return true;
198+
}
199+
200+
return false;
201+
}
202+
/**
203+
* Запускает триггер
204+
* @param string $type
205+
* @param array $row
206+
*/
207+
public function runTrigger($type, $row)
208+
{
209+
$trigger = Container::getTrigger(static::class, $type);
210+
$trigger($row);
211+
}
179212
/**
180213
* Выборка с начально заданной фильтрацией
181214
* @return self

tests/suql/TriggerTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\TestCase;
6+
use suql\db\Container;
7+
use suql\syntax\Query;
8+
use test\suql\models\Query19;
9+
10+
final class TriggerTest extends TestCase
11+
{
12+
public function setUp(): void
13+
{
14+
// Create a database
15+
Container::create(require('config/db-null.php'));
16+
Query::create('create database db_test')->setConnection('connection')->exec();
17+
Container::add(require('config/db.php'));
18+
Query::create('create table table_10(f1 int, f2 int, primary key (f1))')->setConnection('db_test')->exec();
19+
Query::create('insert into table_10 (f1, f2) values (1, 1), (2, 2), (3, 3)')->setConnection('db_test')->exec();
20+
Query::create('insert into table_10 (f1, f2) values (?, ?), (?, ?), (?, ?)')->setConnection('db_test')->exec([4, 4, 5, 5, 6, 6]);
21+
}
22+
23+
public function tearDown(): void
24+
{
25+
// Drop the database
26+
Query::create('drop table table_10')->setConnection('db_test')->exec();
27+
Query::create('drop database db_test')->setConnection('db_test')->exec();
28+
}
29+
30+
public function testTrigger(): void
31+
{
32+
$expected = ['c1' => 7, 'c2' => 'sagittaracc'];
33+
34+
Query19::trigger('insert', function ($actual) use ($expected) {
35+
$this->assertEquals($expected, $actual);
36+
});
37+
38+
$record = new Query19();
39+
$record->c1 = 7;
40+
$record->c2 = 'sagittaracc';
41+
$record->save();
42+
}
43+
}

0 commit comments

Comments
 (0)