mhndev / pongo
使用PHP与mongodb协同工作,无ODM,无数据映射器,直接使用mongo json查询
1.0.0
2019-01-17 11:24 UTC
Requires
- php: >=7.1.0
- ext-json: *
- ext-mongodb: *
- mongodb/mongodb: ^1.4
Requires (Dev)
- phpunit/phpunit: >7.3
This package is auto-updated.
Last update: 2024-09-18 03:42:56 UTC
README
PHP MongoDB
此包包含如何使用PHP处理mongodb的示例源代码,不依赖任何库,只需使用mongodb/mongodb官方包的仓库模式。
你应该像以下源代码那样创建你的实体和仓库。
Post 实体类
<?php namespace mhndev\Pongo\Entity; use mhndev\Pongo\Contract\iMongoEntity; /** * Class EntityPost * @package mhndev\Pongo\Entity */ class EntityPost implements iMongoEntity { /** * @var string */ protected $id; /** * @var string */ protected $title; /** * @var string */ protected $body; /** * @var array */ protected $lastThreeComment; /** * @var \DateTime */ protected $created_at; /** * @param string $id * @return EntityPost */ public function setId(string $id): EntityPost { $this->id = $id; return $this; } /** * @return string */ public function getId(): string { return $this->id; } /** * @param string $title * @return EntityPost */ public function setTitle(string $title): EntityPost { $this->title = $title; return $this; } /** * @return string */ public function getTitle(): string { return $this->title; } /** * @param string $body * @return EntityPost */ public function setBody(string $body): EntityPost { $this->body = $body; return $this; } /** * @return string */ public function getBody(): string { return $this->body; } /** * @param array $lastThreeComment * @return EntityPost */ public function setLastThreeComment(array $lastThreeComment): EntityPost { $this->lastThreeComment = $lastThreeComment; return $this; } /** * @return array */ public function getLastThreeComment(): array { return $this->lastThreeComment; } /** * @param \DateTime $created_at * @return EntityPost */ public function setCreatedAt(\DateTime $created_at): EntityPost { $this->created_at = $created_at; return $this; } /** * @return \DateTime */ public function getCreatedAt(): \DateTime { return $this->created_at; } /** * @return array */ function toMongo() { return [ 'title' => $this->title, 'body' => $this->body, 'created_at' => $this->created_at ]; } }
考虑所有将要持久化到mongo的实体都应该实现iMongoEntity接口,该方法不包含任何其他内容,只是将对象填充到数组中,该数组可以被持久化到mongodb。
下面是.
Post 仓库
<?php namespace mhndev\Pongo\Repository; use mhndev\Pongo\Entity\EntityPost; use mhndev\Pongo\Exception\exEntityNotFound; use mhndev\Pongo\MongoHelper; use MongoDB\BSON\ObjectId; use MongoDB\Collection; use MongoDB\Database; /** * Class RepositoryPost * @package mhndev\Pongo\Repository */ class RepositoryPost { /** * @var Collection */ protected $gateway; /** * RepositoryPost constructor. * @param Database $db * @param string $collectionName */ function __construct(Database $db, string $collectionName) { $this->gateway = $db->selectCollection($collectionName); } /** * @param EntityPost $post * @return EntityPost */ function persist(EntityPost $post) { $result = $this->gateway->insertOne(MongoHelper::postEntityToMongoPersistable($post)); return $post->setId($result->getInsertedId()); } /** * @param string $id * @return EntityPost * @throws exEntityNotFound */ function findById(string $id) { $record = $this->gateway->findOne(['_id' => new ObjectId($id)]); if(is_null($record)) { throw new exEntityNotFound; } return MongoHelper::postMongoToEntityPost(iterator_to_array($record)); } /** * @return array */ function list10NewPosts() { $result = $this->gateway->find([], ['$sort' => ['created_at' => -1 ] ])->toArray(); return MongoHelper::arrayPostMongoToEntity($result); } }
这里你可以查看示例用法
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); include_once "vendor/autoload.php"; $settings = [ 'driver' => [ // Master Connection Client 'master' => [ 'host' => 'mongodb://:27017', 'options_uri' => [ ], 'options' => [ ], ], ], ]; $mongoDriver = new \mhndev\Pongo\MongoDriverManager(); $mongoDriver->addClient(new MongoDB\Client($settings['driver']['master']['host'] ), 'master' ); $mongoClient = $mongoDriver->byClient('master'); $db = $mongoClient->selectDatabase('db_name'); $postRepo = new \mhndev\Pongo\Repository\RepositoryPost($db, 'posts'); $post = (new \mhndev\Pongo\Entity\EntityPost()) ->setTitle('My New Post Title') ->setBody('My New Post Body') ->setCreatedAt(new DateTime()); $persistedPost = $postRepo->persist($post); var_dump($persistedPost); die(); $postEntity = $postRepo->findById('5bcae585cf2a2e1d122a2ab3'); var_dump($postEntity); die(); $last10Posts = $postRepo->list10NewPosts(); var_dump($last10Posts); die();