Skip to content

Commit

Permalink
修正关联写入
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Feb 27, 2025
1 parent 955e025 commit 1bd0832
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/model/concern/RelationShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ private function relationSave(array $relations = [], bool $isUpdate = true)
if (is_numeric($key) && isset($relations[$name])) {
// 支持关联写入或更新
$relation = $relations[$name];
$isUpdate ? $relation->save() : $this->$name()->save($relation);
if ($isUpdate) {
$relation->save();
} else {
$this->$name()->save($relation, true);
$relation->setKey($relation->getLastInsID());
}
} elseif (is_array($name)) {
// 关联写入
$data = [];
Expand Down
6 changes: 3 additions & 3 deletions tests/orm/ModelHasOneThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public static function setUpBeforeClass(): void
`account` varchar(255) NOT NULL DEFAULT "",
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_profile_id` (`profile_id`)
KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;',
'DROP TABLE IF EXISTS `test_profile`;',
'CREATE TABLE `test_profile` (
Expand All @@ -36,7 +35,8 @@ public static function setUpBeforeClass(): void
`email` varchar(255) NOT NULL DEFAULT "",
`nickname` varchar(255) NOT NULL DEFAULT "",
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
KEY `idx_account_id` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;'
];
foreach ($sqlList as $sql) {
Expand Down
11 changes: 6 additions & 5 deletions tests/orm/ModelOneToOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function setUpBeforeClass(): void
'DROP TABLE IF EXISTS `test_profile`;',
'CREATE TABLE `test_profile` (
`id` int NOT NULL AUTO_INCREMENT,
`uid` int NOT NULL,
`user_id` int NOT NULL,
`email` varchar(255) NOT NULL DEFAULT "",
`nickname` varchar(255) NOT NULL DEFAULT "",
`update_time` datetime NOT NULL,
Expand Down Expand Up @@ -65,11 +65,11 @@ public function testBindAttr()
$user = UserModel::find($userID)
->bindAttr(
'profile',
['email', 'nick_name' => 'nickname', 'true_name' => fn ($model) =>$model?->get('nickname')]
['email', 'nick_name' => 'nickname']
);
$this->assertEquals(
[$userID, $email, $nickname, $nickname],
[$user->id, $user->email, $user->nick_name, $user->true_name]
[$userID, $email, $nickname],
[$user->id, $user->email, $user->nick_name]
);
}
/**
Expand All @@ -84,7 +84,7 @@ public function testBasicRelation()
$profile = new ProfileModel();
$profile->email = 'test@thinkphp.cn';
$profile->nickname = 'test';
$profile->uid = $user->id;
$profile->user_id = $user->id;
$profile->save();

// 测试hasOne关联
Expand Down Expand Up @@ -152,6 +152,7 @@ public function testRelationSave()
$this->assertEquals('new@thinkphp.cn', $user->profile->email);

// 测试关联更新
$user = UserModel::with(['profile'])->where('account', 'newuser')->find();
$user->profile->email = 'updated@thinkphp.cn';
$user->together(['profile'])->save();

Expand Down
2 changes: 1 addition & 1 deletion tests/orm/ModelViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class UserOrderAggregateView extends View
public function query($query)
{
$query->view('test_user_view', 'id as user_id,name,create_time')
->view('test_order_view', 'COUNT(id) as order_count,SUM(amount) as total_amount,MIN(create_time) as first_order_time', 'test_user_view.id=test_order_view.user_id')
->view('test_order_view', 'COUNT(*) as order_count,SUM(amount) as total_amount,MIN(create_time) as first_order_time', 'test_user_view.id=test_order_view.user_id')
->group('test_user_view.id');
}
}
Expand Down

0 comments on commit 1bd0832

Please # to comment.