Skip to content

Commit f392b5c

Browse files
committed
兼容Model属性定义方式
1 parent db61ebd commit f392b5c

File tree

6 files changed

+28
-22
lines changed

6 files changed

+28
-22
lines changed

README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
# ThinkORM4.0
1+
# ThinkORM5.0
22

33
基于PHP8.0+ 和PDO实现的ORM。
44

5-
- 模型分层设计
6-
- 引入实体模型
7-
- 引入视图模型
8-
- 完全兼容3.0版本
9-
5+
完全重构的Model层,更轻量化,目前仅供开发测试用
106

117
## 安装
128
~~~
13-
composer require topthink/think-orm
9+
composer require topthink/think-orm:5.0.x-dev
1410
~~~
1511

16-
## 文档
17-
18-
详细参考 [ThinkORM开发指南](https://doc.thinkphp.cn/@think-orm)

src/Entity.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,11 @@ public function jsonSerialize(): array
125125
/**
126126
* 模型数据转数组.
127127
*
128-
* @param array $allow 允许输出字段
129128
* @return array
130129
*/
131-
public function toArray(array $allow = []): array
130+
public function toArray(): array
132131
{
133-
return $this->model()->toArray($allow);
132+
return $this->model()->toArray();
134133
}
135134

136135
/**

src/Model.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ protected function setOption(string $name, $value)
206206
*/
207207
protected function getOption(string $name, $default = null)
208208
{
209+
// 兼容读取3.0版本的属性参数定义
210+
if (property_exists($this, $name) && isset($this->$name)) {
211+
return $this->$name;
212+
}
209213
return self::$weakMap[$this][$name] ?? $default;
210214
}
211215

src/model/concern/Attribute.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,16 @@ public function isEmpty(): bool
417417
return empty($this->getData());
418418
}
419419

420+
/**
421+
* 判断JSON数据是否为数组格式.
422+
*
423+
* @return bool
424+
*/
425+
public function isJsonAssoc(): bool
426+
{
427+
return $this->getOption('jsonAssoc', false);
428+
}
429+
420430
/**
421431
* 设置数据对象的值 并进行类型自动转换
422432
*

src/model/concern/AutoWriteData.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ protected function autoWriteData(array &$data, bool $update)
6262
*/
6363
protected function autoDateTime(array &$data, bool $update)
6464
{
65-
$autoDateTime = $this->getOption('auto_timestamp', true);
65+
$autoDateTime = $this->getOption('autoWriteTimestamp', true);
6666
if ($autoDateTime) {
67-
$dateTimeFields = [$this->getOption('update_time')];
67+
$dateTimeFields = [$this->getOption('updateTime')];
6868
if (!$update) {
69-
array_unshift($dateTimeFields, $this->getOption('create_time'));
69+
array_unshift($dateTimeFields, $this->getOption('createTime'));
7070
}
7171

7272
foreach ($dateTimeFields as $field) {
@@ -121,12 +121,12 @@ public function getDateFormat()
121121

122122
public function setDateFormat(string $format)
123123
{
124-
$this->setOption('datetime_format', $format);
124+
$this->setOption('dateFormat', $format);
125125
}
126126

127127
public function setTimeField($createTime, $updateTime)
128128
{
129-
$this->setOption('create_time', $createTime);
130-
$this->setOption('update_time', $updateTime);
129+
$this->setOption('createTime', $createTime);
130+
$this->setOption('updateTime', $updateTime);
131131
}
132132
}

src/model/concern/ModelEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function setEvent($event)
4343
*/
4444
public function withEvent(bool $event)
4545
{
46-
$this->setOption('with_event', $event);
46+
$this->setOption('withEvent', $event);
4747

4848
return $this;
4949
}
@@ -57,13 +57,13 @@ public function withEvent(bool $event)
5757
*/
5858
protected function trigger(string $event): bool
5959
{
60-
if (!$this->getOption('with_event', true)) {
60+
if (!$this->getOption('withEvent', true)) {
6161
return true;
6262
}
6363

6464
$call = 'on' . Str::studly($event);
6565
$obj = $this->getOption('event');
66-
$obser = $this->getOption('event_observer');
66+
$obser = $this->getOption('eventObserver');
6767
try {
6868
if ($obser) {
6969
$reflect = new ReflectionClass($obser);

0 commit comments

Comments
 (0)