Skip to content

Commit

Permalink
QueryBuilder根据Entity注解参数主动切换数据库 (swoft-cloud/swoft-component#164)
Browse files Browse the repository at this point in the history
* [modify]Query::table(Model)支持根据Model主动切换数据库

```
/**
 * Model\Entity
 * @entity(instance="other")
 * @table(name="tableName")
 */
```

* modify testSelectinstance

modify testSelectinstance

* add test case when select Instance

add test case when select Instance

* Update QueryBuilderTest.php
  • Loading branch information
HZMarico authored and huangzhhui committed Aug 16, 2018
1 parent 00eee11 commit 805ab2c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,8 @@ private function getTableNameByClassName($tableName): string
if (!isset($entities[$tableName]['table']['name'])) {
throw new DbException('Class is not an entity,className=' . $tableName);
}
// 选择数据库
$this->selectInstance($entities[$tableName]['instance']);

return $entities[$tableName]['table']['name'];
}
Expand Down
20 changes: 16 additions & 4 deletions test/Cases/Mysql/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,24 @@ public function testSelectinstance()
'description' => 'this my desc instance',
'age' => mt_rand(1, 100),
];
$userid = Query::table(User::class)->selectInstance('other')->insert($data)->getResult();
$userId = Query::table(User::class)->selectInstance('other')->insert($data)->getResult();
$data['description']='this my desc default instance';
$userId2 = Query::table(User::class)->insert($data)->getResult();

$user = OtherUser::findById($userid)->getResult();
$user2 = Query::table(User::class)->selectInstance('other')->where('id', $userid)->one()->getResult();
$user2 = Query::table(User::class)->selectInstance('other')->where('id', $userId)->one()->getResult();
$this->assertEquals($user2['description'], 'this my desc instance');
$this->assertEquals($user2['id'], $userid);
$this->assertEquals($user2['id'], $userId);

$otherUser = Query::table(OtherUser::class)->where('id', $userId)->one()->getResult();
$this->assertEquals($otherUser['age'], $data['age']);
$this->assertEquals($otherUser['id'], $userId);

$otherUser2 = Query::table(OtherUser::class)->selectInstance('default')->where('id', $userId2)->one()->getResult();
$this->assertEquals('this my desc default instance', $otherUser2['description']);

$user = OtherUser::findById($userId)->getResult();
$this->assertEquals($user->getAge(), $data['age']);
$this->assertEquals($user->getId(), $userId);
}

public function testSelectinstanceByCo()
Expand Down

0 comments on commit 805ab2c

Please sign in to comment.