Skip to content

Commit 07c0784

Browse files
committed
Merge branch 'master' of github.com:swoft-cloud/swoft
2 parents 30a22aa + 88b8b4c commit 07c0784

File tree

10 files changed

+193
-47
lines changed

10 files changed

+193
-47
lines changed

app/Http/Controller/DbModelController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace App\Http\Controller;
55

6+
use App\Model\Entity\Count;
67
use App\Model\Entity\User;
78
use Exception;
89
use Swoft\Http\Message\Response;
@@ -51,6 +52,10 @@ public function save(): array
5152

5253
$user->save();
5354

55+
$count = Count::new();
56+
$count->setUserId($user->getId());
57+
$count->save();
58+
5459
return $user->toArray();
5560
}
5661

app/Listener/ModelSavedListener.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace App\Listener;
5+
6+
use App\Model\Entity\User;
7+
use Swoft\Db\DbEvent;
8+
use Swoft\Db\Eloquent\Model;
9+
use Swoft\Event\Annotation\Mapping\Listener;
10+
use Swoft\Event\EventHandlerInterface;
11+
use Swoft\Event\EventInterface;
12+
13+
/**
14+
* Class RanListener
15+
*
16+
* @since 2.0
17+
*
18+
* @Listener(DbEvent::MODEL_SAVED)
19+
*/
20+
class ModelSavedListener implements EventHandlerInterface
21+
{
22+
/**
23+
* @param EventInterface $event
24+
*/
25+
public function handle(EventInterface $event): void
26+
{
27+
/* @var Model $modelStatic */
28+
$modelStatic = $event->getTarget();
29+
30+
if ($modelStatic instanceof User) {
31+
// to do something....
32+
}
33+
34+
// ....
35+
}
36+
}

app/Listener/RanListener.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace App\Listener;
5+
6+
use Swoft\Db\Connection\Connection;
7+
use Swoft\Db\DbEvent;
8+
use Swoft\Event\Annotation\Mapping\Listener;
9+
use Swoft\Event\EventHandlerInterface;
10+
use Swoft\Event\EventInterface;
11+
12+
/**
13+
* Class RanListener
14+
*
15+
* @since 2.0
16+
*
17+
* @Listener(DbEvent::SQL_RAN)
18+
*/
19+
class RanListener implements EventHandlerInterface
20+
{
21+
/**
22+
* @param EventInterface $event
23+
*/
24+
public function handle(EventInterface $event): void
25+
{
26+
27+
/* @var Connection $connection */
28+
$connection = $event->getTarget();
29+
30+
$querySql = $event->getParam(0);
31+
$bindings = $event->getParam(1);
32+
}
33+
}

app/Listener/UserSavingListener.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace App\Listener;
5+
6+
use App\Model\Entity\User;
7+
use Swoft\Event\Annotation\Mapping\Listener;
8+
use Swoft\Event\EventHandlerInterface;
9+
use Swoft\Event\EventInterface;
10+
11+
/**
12+
* Class UserSavingListener
13+
*
14+
* @since 2.0
15+
*
16+
* @Listener("swoft.model.user.saving")
17+
*/
18+
class UserSavingListener implements EventHandlerInterface
19+
{
20+
/**
21+
* @param EventInterface $event
22+
*/
23+
public function handle(EventInterface $event): void
24+
{
25+
/* @var User $user */
26+
$user = $event->getTarget();
27+
28+
if ($user->getAge() > 100) {
29+
// stopping saving
30+
$event->stopPropagation(true);
31+
32+
$user->setAdd(100);
33+
}
34+
}
35+
}

app/Migration/AddMsg20190630164222.php renamed to app/Migration/AddMsg.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
*
1313
* @since 2.0
1414
*
15-
* @Migration()
15+
* @Migration(20190630164222)
1616
*/
17-
class AddMsg20190630164222 extends BaseMigration
17+
class AddMsg extends BaseMigration
1818
{
1919
/**
2020
* @return void
2121
*/
22-
public function up()
22+
public function up(): void
2323
{
2424
$sql = <<<sql
2525
CREATE TABLE `users` (
@@ -37,12 +37,8 @@ public function up()
3737
/**
3838
* @return void
3939
*/
40-
public function down()
40+
public function down(): void
4141
{
42-
$truncate = <<<sql
43-
truncate `users`;
44-
sql;
45-
$this->execute($truncate);
4642

4743
$dropSql = <<<sql
4844
drop table if exists `users`;

app/Migration/AddUser20190627225524.php renamed to app/Migration/AddUser.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
use Swoft\Db\Schema;
1111
use Swoft\Db\Schema\Blueprint;
1212
use Swoft\Devtool\Annotation\Mapping\Migration;
13-
use Swoft\Devtool\Migration\MigrationInterface;
13+
use Swoft\Devtool\Migration\Contract\MigrationInterface;
1414

1515
/**
1616
* Class AddUser20190627225524
1717
*
1818
* @since 2.0
1919
*
20-
* @Migration()
20+
* @Migration(20190627225524)
2121
*/
22-
class AddUser20190627225524 implements MigrationInterface
22+
class AddUser implements MigrationInterface
2323
{
2424
/**
2525
* @return void
@@ -28,7 +28,7 @@ class AddUser20190627225524 implements MigrationInterface
2828
* @throws ContainerException
2929
* @throws DbException
3030
*/
31-
public function up()
31+
public function up(): void
3232
{
3333
Schema::createIfNotExists('users', function (Blueprint $blueprint) {
3434
$blueprint->increments('id');
@@ -49,10 +49,10 @@ public function up()
4949
* @throws ContainerException
5050
* @throws DbException
5151
*/
52-
public function down()
52+
public function down(): void
5353
{
5454
Schema::dropIfExists('users');
5555

56-
Schema::getSchemaBuilder('db.pool')->dropIfExists('user');
56+
Schema::getSchemaBuilder('db.pool')->dropIfExists('users');
5757
}
5858
}

app/Migration/Message20190627225525.php renamed to app/Migration/Message.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*
1717
* @since 2.0
1818
*
19-
* @Migration("db2.pool")
19+
* @Migration(time=20190627225525, pool="db3.pool")
2020
*/
21-
class Message20190627225525 extends BaseMigration
21+
class Message extends BaseMigration
2222
{
2323
/**
2424
* @return void
@@ -27,7 +27,7 @@ class Message20190627225525 extends BaseMigration
2727
* @throws ContainerException
2828
* @throws DbException
2929
*/
30-
public function up()
30+
public function up(): void
3131
{
3232
$this->schema->createIfNotExists('messages', function (Blueprint $blueprint) {
3333
$blueprint->increments('id');
@@ -43,7 +43,7 @@ public function up()
4343
* @throws ContainerException
4444
* @throws DbException
4545
*/
46-
public function down()
46+
public function down(): void
4747
{
4848
$this->schema->dropIfExists('messages');
4949
}

app/Model/Entity/Count.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
/**
13-
*
13+
*
1414
* Class Count
1515
*
1616
* @since 2.0
@@ -19,41 +19,53 @@
1919
*/
2020
class Count extends Model
2121
{
22+
protected const UPDATED_AT = 'update_time';
23+
protected const CREATED_AT = 'create_time';
24+
2225
/**
23-
*
26+
*
2427
* @Id()
2528
* @Column()
2629
* @var int|null
2730
*/
2831
private $id;
2932

3033
/**
31-
*
34+
*
3235
*
3336
* @Column(name="user_id", prop="userId")
3437
* @var int|null
3538
*/
3639
private $userId;
3740

3841
/**
39-
*
42+
*
4043
*
4144
* @Column(name="create_time", prop="createTime")
4245
* @var int|null
4346
*/
4447
private $createTime;
4548

4649
/**
47-
*
50+
*
4851
*
4952
* @Column()
5053
* @var string|null
5154
*/
5255
private $attributes;
5356

57+
/**
58+
*
59+
*
60+
* @Column(name="update_time", prop="updateTime")
61+
* @var string|null
62+
*/
63+
private $updateTime;
64+
5465

5566
/**
5667
* @param int|null $id
68+
*
5769
* @return void
5870
*/
5971
public function setId(?int $id): void
@@ -63,6 +75,7 @@ public function setId(?int $id): void
6375

6476
/**
6577
* @param int|null $userId
78+
*
6679
* @return void
6780
*/
6881
public function setUserId(?int $userId): void
@@ -72,6 +85,7 @@ public function setUserId(?int $userId): void
7285

7386
/**
7487
* @param int|null $createTime
88+
*
7589
* @return void
7690
*/
7791
public function setCreateTime(?int $createTime): void
@@ -81,13 +95,24 @@ public function setCreateTime(?int $createTime): void
8195

8296
/**
8397
* @param string|null $attributes
98+
*
8499
* @return void
85100
*/
86101
public function setAttributes(?string $attributes): void
87102
{
88103
$this->attributes = $attributes;
89104
}
90105

106+
/**
107+
* @param string|null $updateTime
108+
*
109+
* @return void
110+
*/
111+
public function setUpdateTime(?string $updateTime): void
112+
{
113+
$this->updateTime = $updateTime;
114+
}
115+
91116
/**
92117
* @return int|null
93118
*/
@@ -120,4 +145,12 @@ public function getAttributes(): ?string
120145
return $this->attributes;
121146
}
122147

148+
/**
149+
* @return string|null
150+
*/
151+
public function getUpdateTime(): ?string
152+
{
153+
return $this->updateTime;
154+
}
155+
123156
}

0 commit comments

Comments
 (0)