eman-development-design / mongodb-helpers
Helper Objects to make writing MongoDB apps easier.
Requires
- php: >=7.2.0
- ext-mongodb: ^1.8.1
- mongodb/mongodb: ^1.8
This package is auto-updated.
Last update: 2025-03-29 00:32:35 UTC
README
Helper Objects to make writing MongoDB apps easier.
Helpers
Sorting Helper
You can easily add sorting by extending DataSorting
class UserSorting extends DataSorting { public const Email = 'Email'; public const FirstName = 'FirstName'; public const LastName = 'LastName'; }
you can then use it like so:
UserSorting::AddToSortList(UserSorting::FirstName, UserSorting::ASC);
you then compile the sort list by doing this:
$sort = UserSorting::GetSortList();
this wil allow you you add as many sorting rules as you want to a query.
Model Helper
MongoModelInterface
is an interface helper that'll provide some helper methods for handling model data.
use MongoDB\BSON\Serializable; use MongoDB\Model\BSONDocument; use Edd\MongoDbHelpers\MongoModelInterface class User implements Serializable, MongoModelInterface { public $userGuid; public $email; public $firstName; public $lastName; public function bsonSerialize() : array { return [ 'UserUuid' => $this->userUuid, 'Email' => $this->email, 'FirstName' => $this->firstName, 'LastName' => $this->lastName ]; } public function map(BSONDocument $document) { $this->userUuid = $document['UserUuid']; $this->email = $document['Email']; $this->firstName = $document['FirstName']; $this->lastName = $document['LastName']; } public function toArray() : array { return get_object_vars($this); } }
map
helps you map a BSON Document result to your model properties.
toArray
is for presenting the data as an array.
Repository Helper
MongoRepository
makes it easy to setup a basic repository style architecture, the constructor will create an instance of MongoDB\Client
.
SetCollection
& GetCollection
setup and provide an instance of MongoDB\Collection
.