Skip to content

Commit 8713727

Browse files
author
Ethan
committed
with
1 parent d459d15 commit 8713727

File tree

5 files changed

+82
-16
lines changed

5 files changed

+82
-16
lines changed

src/ModelTrait.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PFinal\Database\Relations\HasMany;
77
use PFinal\Database\Relations\HasOne;
88
use Leaf\DB;
9+
use PFinal\Database\Relations\RelationBase;
910

1011
/**
1112
* 模型
@@ -145,7 +146,9 @@ public function hasMany($related, $foreignKey = null, $localKey = 'id')
145146
/**
146147
* 渴求式加载
147148
*
148-
* eg. Blog::with('category')->where()->findAll()
149+
* eg:
150+
* Blog::with('category')->findAll()
151+
* Favorite::with('project.city', 'project.user')->findAll()
149152
*
150153
* @param string|array $relations 关联名称
151154
*/
@@ -154,15 +157,11 @@ public static function with($relations)
154157
$relations = is_string($relations) ? func_get_args() : $relations;
155158

156159
return DB::table(static::tableName())->asEntity(static::className())->afterFind(function ($models) use ($relations) {
157-
if (count($models) > 0) {
158-
foreach ($relations as $relation) {
159-
$relationObj = call_user_func([$models[0], $relation]);
160-
$relationObj->appendData($models, $relation);
161-
}
162-
}
160+
RelationBase::appendRelationData($models, $relations);
163161
});
164162
}
165163

164+
166165
public function __get($getter)
167166
{
168167
if (!method_exists($this, $getter)) {

src/Relations/BelongsTo.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace PFinal\Database\Relations;
44

55
use Leaf\Util;
6-
use PFinal\Database\Builder;
76

8-
class BelongsTo extends Builder
7+
class BelongsTo extends RelationBase
98
{
109
public $foreignKey = null;
1110
public $ownerKey;
@@ -18,13 +17,27 @@ public function __invoke()
1817
return $this->findOne();
1918
}
2019

21-
public function appendData($models, $name)
20+
public function appendData(array $models, $name, $relations = [])
2221
{
22+
if (count($models) == 0) {
23+
return;
24+
}
25+
26+
$relations = (array)$relations;
27+
28+
if (isset($models[0]->$name) && count($relations) > 0) {
29+
self::appendRelationData(array_column($models, $name), $relations);
30+
return;
31+
}
32+
2333
$ids = Util::arrayColumn($models, $this->foreignKey);
2434
$ids = array_unique($ids);
2535

2636
$this->whereIn($this->ownerKey, $ids);
2737
$relationData = $this->findAll();
38+
if (count($relations) > 0) {
39+
$this->appendRelationData($relationData, $relations);
40+
}
2841

2942
$relationData = Util::arrayColumn($relationData, null, $this->ownerKey);
3043

src/Relations/HasMany.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace PFinal\Database\Relations;
44

55
use Leaf\Util;
6-
use PFinal\Database\Builder;
76

8-
class HasMany extends Builder
7+
class HasMany extends RelationBase
98
{
109
public $foreignKey = null;
1110
public $localKey;
@@ -18,14 +17,28 @@ public function __invoke()
1817
return $this->findAll();
1918
}
2019

21-
public function appendData($models, $name)
20+
public function appendData(array $models, $name, $relations = [])
2221
{
22+
if (count($models) == 0) {
23+
return;
24+
}
25+
26+
$relations = (array)$relations;
27+
28+
if (isset($models[0]->$name) && count($relations) > 0) {
29+
self::appendRelationData(array_column($models, $name), $relations);
30+
return;
31+
}
32+
2333
$ids = Util::arrayColumn($models, $this->localKey);
2434
$ids = array_unique($ids);
2535

2636
$this->whereIn($this->foreignKey, $ids);
2737

2838
$relationData = $this->findAll();
39+
if (count($relations) > 0) {
40+
$this->appendRelationData($relationData, $relations);
41+
}
2942

3043
foreach ($models as $k => $model) {
3144
$models[$k][$name] = [];

src/Relations/HasOne.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace PFinal\Database\Relations;
44

55
use Leaf\Util;
6-
use PFinal\Database\Builder;
76

8-
class HasOne extends Builder
7+
class HasOne extends RelationBase
98
{
109
public $foreignKey = null;
1110
public $localKey;
@@ -18,14 +17,28 @@ public function __invoke()
1817
return $this->findOne();
1918
}
2019

21-
public function appendData($models, $name)
20+
public function appendData(array $models, $name, $relations = [])
2221
{
22+
if (count($models) == 0) {
23+
return;
24+
}
25+
26+
$relations = (array)$relations;
27+
28+
if (isset($models[0]->$name) && count($relations) > 0) {
29+
self::appendRelationData(array_column($models, $name), $relations);
30+
return;
31+
}
32+
2333
$ids = Util::arrayColumn($models, $this->localKey);
2434
$ids = array_unique($ids);
2535

2636
$this->whereIn($this->foreignKey, $ids);
2737

2838
$relationData = $this->findAll();
39+
if (count($relations) > 0) {
40+
$this->appendRelationData($relationData, $relations);
41+
}
2942

3043
$relationData = Util::arrayColumn($relationData, null, $this->foreignKey);
3144

src/Relations/RelationBase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PFinal\Database\Relations;
4+
5+
use PFinal\Database\Builder;
6+
7+
class RelationBase extends Builder
8+
{
9+
public static function appendRelationData($models, array $relations)
10+
{
11+
if (count($models) > 0) {
12+
foreach ($relations as $relation) {
13+
14+
$ind = strpos($relation, '.');
15+
if ($ind !== false) {
16+
$methodName = substr($relation, 0, $ind);
17+
$nextMethodName = substr($relation, $ind + 1);
18+
} else {
19+
$methodName = $relation;
20+
$nextMethodName = [];
21+
}
22+
23+
$relationObj = call_user_func([$models[0], $methodName]);
24+
$relationObj->appendData($models, $methodName, $nextMethodName);
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)