项目作者: mhndev

项目描述 :
This package helps Learn how to work with mongodb in php without any thirdparty package
高级语言: PHP
项目地址: git://github.com/mhndev/Pongo.git
创建时间: 2018-10-20T08:49:58Z
项目社区:https://github.com/mhndev/Pongo

开源协议:

下载


PHP MongoDB

this packages contains sample source code for how to work with mongodb in php,
without help of any library, just use mongodb/mongodb official package
using repository pattern.

you should create your entities and repositories like following source codes.

Post Entity class

  1. <?php
  2. namespace mhndev\Pongo\Entity;
  3. use mhndev\Pongo\Contract\iMongoEntity;
  4. /**
  5. * Class EntityPost
  6. * @package mhndev\Pongo\Entity
  7. */
  8. class EntityPost implements iMongoEntity
  9. {
  10. /**
  11. * @var string
  12. */
  13. protected $id;
  14. /**
  15. * @var string
  16. */
  17. protected $title;
  18. /**
  19. * @var string
  20. */
  21. protected $body;
  22. /**
  23. * @var array
  24. */
  25. protected $lastThreeComment;
  26. /**
  27. * @var \DateTime
  28. */
  29. protected $created_at;
  30. /**
  31. * @param string $id
  32. * @return EntityPost
  33. */
  34. public function setId(string $id): EntityPost
  35. {
  36. $this->id = $id;
  37. return $this;
  38. }
  39. /**
  40. * @return string
  41. */
  42. public function getId(): string
  43. {
  44. return $this->id;
  45. }
  46. /**
  47. * @param string $title
  48. * @return EntityPost
  49. */
  50. public function setTitle(string $title): EntityPost
  51. {
  52. $this->title = $title;
  53. return $this;
  54. }
  55. /**
  56. * @return string
  57. */
  58. public function getTitle(): string
  59. {
  60. return $this->title;
  61. }
  62. /**
  63. * @param string $body
  64. * @return EntityPost
  65. */
  66. public function setBody(string $body): EntityPost
  67. {
  68. $this->body = $body;
  69. return $this;
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function getBody(): string
  75. {
  76. return $this->body;
  77. }
  78. /**
  79. * @param array $lastThreeComment
  80. * @return EntityPost
  81. */
  82. public function setLastThreeComment(array $lastThreeComment): EntityPost
  83. {
  84. $this->lastThreeComment = $lastThreeComment;
  85. return $this;
  86. }
  87. /**
  88. * @return array
  89. */
  90. public function getLastThreeComment(): array
  91. {
  92. return $this->lastThreeComment;
  93. }
  94. /**
  95. * @param \DateTime $created_at
  96. * @return EntityPost
  97. */
  98. public function setCreatedAt(\DateTime $created_at): EntityPost
  99. {
  100. $this->created_at = $created_at;
  101. return $this;
  102. }
  103. /**
  104. * @return \DateTime
  105. */
  106. public function getCreatedAt(): \DateTime
  107. {
  108. return $this->created_at;
  109. }
  110. /**
  111. * @return array
  112. */
  113. function toMongo()
  114. {
  115. return [
  116. 'title' => $this->title,
  117. 'body' => $this->body,
  118. 'created_at' => $this->created_at
  119. ];
  120. }
  121. }

consider all entities that are going to be persisted in mongo should implement iMongoEntity interface
and this method contains nothing more than hydrate object to an array which can be persisted to mongodb.

and here is .

Post Repository

  1. <?php
  2. namespace mhndev\Pongo\Repository;
  3. use mhndev\Pongo\Entity\EntityPost;
  4. use mhndev\Pongo\Exception\exEntityNotFound;
  5. use mhndev\Pongo\MongoHelper;
  6. use MongoDB\BSON\ObjectId;
  7. use MongoDB\Collection;
  8. use MongoDB\Database;
  9. /**
  10. * Class RepositoryPost
  11. * @package mhndev\Pongo\Repository
  12. */
  13. class RepositoryPost
  14. {
  15. /**
  16. * @var Collection
  17. */
  18. protected $gateway;
  19. /**
  20. * RepositoryPost constructor.
  21. * @param Database $db
  22. * @param string $collectionName
  23. */
  24. function __construct(Database $db, string $collectionName)
  25. {
  26. $this->gateway = $db->selectCollection($collectionName);
  27. }
  28. /**
  29. * @param EntityPost $post
  30. * @return EntityPost
  31. */
  32. function persist(EntityPost $post)
  33. {
  34. $result = $this->gateway->insertOne(MongoHelper::postEntityToMongoPersistable($post));
  35. return $post->setId($result->getInsertedId());
  36. }
  37. /**
  38. * @param string $id
  39. * @return EntityPost
  40. * @throws exEntityNotFound
  41. */
  42. function findById(string $id)
  43. {
  44. $record = $this->gateway->findOne(['_id' => new ObjectId($id)]);
  45. if(is_null($record)) {
  46. throw new exEntityNotFound;
  47. }
  48. return MongoHelper::postMongoToEntityPost(iterator_to_array($record));
  49. }
  50. /**
  51. * @return array
  52. */
  53. function list10NewPosts()
  54. {
  55. $result = $this->gateway->find([], ['$sort' => ['created_at' => -1 ] ])->toArray();
  56. return MongoHelper::arrayPostMongoToEntity($result);
  57. }
  58. }

and here you can checkout the sample usage :

  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. include_once "vendor/autoload.php";
  6. $settings = [
  7. 'driver' => [
  8. // Master Connection Client
  9. 'master' => [
  10. 'host' => 'mongodb://localhost:27017',
  11. 'options_uri' => [
  12. ],
  13. 'options' => [
  14. ],
  15. ],
  16. ],
  17. ];
  18. $mongoDriver = new \mhndev\Pongo\MongoDriverManager();
  19. $mongoDriver->addClient(new MongoDB\Client($settings['driver']['master']['host'] ), 'master' );
  20. $mongoClient = $mongoDriver->byClient('master');
  21. $db = $mongoClient->selectDatabase('db_name');
  22. $postRepo = new \mhndev\Pongo\Repository\RepositoryPost($db, 'posts');
  23. $post = (new \mhndev\Pongo\Entity\EntityPost())
  24. ->setTitle('My New Post Title')
  25. ->setBody('My New Post Body')
  26. ->setCreatedAt(new DateTime());
  27. $persistedPost = $postRepo->persist($post);
  28. var_dump($persistedPost);
  29. die();
  30. $postEntity = $postRepo->findById('5bcae585cf2a2e1d122a2ab3');
  31. var_dump($postEntity);
  32. die();
  33. $last10Posts = $postRepo->list10NewPosts();
  34. var_dump($last10Posts);
  35. die();