Skip to content

Commit 85d56a3

Browse files
committed
改进一对一关联绑定
1 parent f86f6fa commit 85d56a3

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/Entity.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ protected function getFields(?string $field = null)
291291
*
292292
* @return array
293293
*/
294-
protected function parseData(array | object $data): array
294+
private function parseData(array | object $data): array
295295
{
296296
if ($data instanceof self) {
297297
$data = $data->getData();
@@ -314,7 +314,7 @@ protected function parseValidate(): string
314314
}
315315

316316
/**
317-
* 获取模型实例.
317+
* 获取模型或数据对象实例.
318318
* @return Model|Query
319319
*/
320320
public function model()
@@ -330,7 +330,7 @@ public function model()
330330
*
331331
* @return void
332332
*/
333-
protected function initializeData(array | object $data, bool $fromSave = false)
333+
private function initializeData(array | object $data, bool $fromSave = false)
334334
{
335335
// 分析数据
336336
$data = $this->parseData($data);
@@ -387,7 +387,7 @@ protected function initializeData(array | object $data, bool $fromSave = false)
387387
*
388388
* @return void
389389
*/
390-
protected function parseRelationData(array $relations)
390+
private function parseRelationData(array $relations)
391391
{
392392
foreach ($relations as $relation => $val) {
393393
$relation = $this->getRealFieldName($relation);
@@ -414,7 +414,7 @@ protected function parseRelationData(array $relations)
414414
*
415415
* @return void
416416
*/
417-
protected function setTempRelation(string $relation, array $data)
417+
private function setTempRelation(string $relation, array $data)
418418
{
419419
$this->setWeakData('relation', $relation, $data);
420420
}
@@ -748,13 +748,13 @@ public function dec(string $field, float $step = 1, int $lazyTime = 0)
748748
* @throws InvalidArgumentException
749749
* @return void
750750
*/
751-
protected function validate(array $data, array $allow)
751+
protected function validate(array $data, array $allow = [])
752752
{
753753
$validater = $this->getOption('validate');
754754
if (!empty($validater) && class_exists('think\validate')) {
755755
try {
756756
validate($validater)
757-
->only($allow)
757+
->only($allow ?: array_keys($data))
758758
->check($data);
759759
} catch (ValidateException $e) {
760760
// 验证失败 输出错误信息
@@ -966,9 +966,9 @@ protected function isNotRequireUpdate(string $name, $val, array $origin): bool
966966
* 写入模型关联数据(一对一).
967967
*
968968
* @param array $relations 数据
969-
* @return bool
969+
* @return void
970970
*/
971-
protected function relationSave(array $relations = [])
971+
private function relationSave(array $relations = [])
972972
{
973973
foreach ($relations as $name => $relation) {
974974
if ($relation && in_array($name, $this->getOption('together'))) {
@@ -987,7 +987,7 @@ protected function relationSave(array $relations = [])
987987
* @param array $relations 数据
988988
* @return void
989989
*/
990-
protected function relationDelete(array $relations = [])
990+
private function relationDelete(array $relations = [])
991991
{
992992
foreach ($relations as $name => $relation) {
993993
if ($relation && in_array($name, $this->getOption('together'))) {
@@ -1009,7 +1009,7 @@ protected function getRelationKey(string $relation)
10091009
}
10101010

10111011
/**
1012-
* 是否为虚拟模型(不能写入).
1012+
* 是否为虚拟模型(不能查询).
10131013
*
10141014
* @return bool
10151015
*/

src/model/relation/OneToOne.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ public function getBindAttr(): array
272272
*/
273273
protected function match(string $model, string $relation, Model $result): void
274274
{
275-
if ($result->getEntity()) {
276-
$data = $result->getEntity()->getRelation($relation);
275+
if ($result instanceof Entity) {
276+
$data = $result->getRelation($relation);
277277
if (!empty($data)) {
278-
$relationModel = (new $model())->newInstance($data);
279278
if ($this->bindAttr) {
280-
$result->getEntity()->bindRelationAttr($data, $this->bindAttr);
279+
$result->bindRelationAttr($data, $this->bindAttr);
281280
} else {
281+
$relationModel = (new $model())->newInstance($data);
282282
$result->setRelation($relation, $relationModel);
283283
}
284284
}

0 commit comments

Comments
 (0)