当前位置: 首页 > 文档资料 > Group-Co 开发文档 >

同步服务(Service开发) - Dao

优质
小牛编辑
132浏览
2023-12-01

Dao

框架内部会做断线重连,失败3次后将进行重连操作

文档参考:Doctrine DBAL’s documentation

$this->getDefault()
获取默认服务器连接,返回\Doctrine\DBAL\Connection对象
$this->getRead($name = null)
获取读服务器连接,若name不填,随机读取。
$this->getWrite($name = null)
获取写服务器连接,若name不填,随机读取。
$this->getAllRead()

获取所有读服务器的连接

$this->getAllWrite()

获取所有写服务器的连接

$this->querySql($sql, $type, $name = null)

* @param sql

  1. \* @param type\[write\|all\_write\|read\|all\_read\|default\]
  2. \* @param name
具体业务中使用:
  1. namespace src\Service\User\Dao\Impl;
  2. use Dao;
  3. use src\Service\User\Dao\UserDao;
  4. class UserDaoImpl extends Dao implements UserDao
  5. {
  6. protected $table = "user";
  7. public function getUser($id)
  8. {
  9. $queryBuilder = $this->getDefault()->createQueryBuilder();
  10. $queryBuilder
  11. ->select("*")
  12. ->from($this->table)
  13. ->where('id = ?')
  14. ->setParameter(0, $id);
  15. return $queryBuilder->execute()->fetch();
  16. }
  17. public function addUser($user)
  18. {
  19. $conn = $this->getDefault();
  20. $affected = $conn->insert($this->table, $user);
  21. if ($affected <= 0) {
  22. return fasle;
  23. }
  24. return $conn->lastInsertId();
  25. }
  26. public function getUserByMobile($mobile)
  27. {
  28. $queryBuilder = $this->getDefault()->createQueryBuilder();
  29. $queryBuilder
  30. ->select("*")
  31. ->from($this->table)
  32. ->where('mobile = ?')
  33. ->setParameter(0, $mobile);
  34. return $queryBuilder->execute()->fetch();
  35. }
  36. public function updateUserPassword($userId, $password)
  37. {
  38. return $this->getDefault()->update($this->table, ['password' => $password], ['id' => $userId]);
  39. }
  40. }