Created
January 12, 2022 01:49
-
-
Save eman1986/86cb6386fa13dd1d12fe0c603c21e3a2 to your computer and use it in GitHub Desktop.
Revisions
-
eman1986 created this gist
Jan 12, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ <?php require 'vendor/autoload.php'; use App\Model\Users; use Symfony\Component\Uid\Uuid; $mongo = new MongoDB\Client('mongodb://localhost:27017/'); $collection = $mongo->selectCollection('demo', 'users'); $user = new Users(); $user->userUuid = Uuid::v4()->toRfc4122(); $user->emailAddress = '[email protected]'; $user->firstName = 'Mouse'; $user->lastName = 'Provide'; $user->roles = ['USER']; $result = $collection->insertOne($user); echo 'Inserted with Object ID: '. $result->getInsertedId()."\n"; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ <?php require 'vendor/autoload.php'; $typeMap = [ 'array' => 'array', 'document' => 'MongoDB\Model\BSONDocument', 'root' => 'MongoDB\Model\BSONDocument' ]; $mongo = new MongoDB\Client('mongodb://localhost:27017/'); $collection = $mongo->selectCollection('demo', 'users'); $query = [ 'UserUuid' => '47f4b337-cd9d-446a-b030-c311e534e94a' ]; $document = $collection->findOne($query, ['typeMap' => $typeMap]); if (null === $document) { echo "Nothing was found.\n"; } else { var_dump($document); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ <?php namespace App\Model; use DateTimeImmutable; use MongoDB\BSON\ObjectId; use MongoDB\BSON\Persistable; use MongoDB\BSON\UTCDateTime; class Users { public ?ObjectId $id = null; public string $userUuid = ''; public string $emailAddress = ''; public string $firstName = ''; public string $lastName = ''; public string $language = 'en'; public array $roles = []; public bool $isUserLocked = false; public bool $isUserEnabled = true; public DateTimeImmutable $createdOn; public DateTimeImmutable $updatedOn; public function __construct() { $this->id = new ObjectId(); $this->createdOn = new DateTimeImmutable(); $this->updatedOn = new DateTimeImmutable(); } public function __serialize(): array { return [ '_id' => $this->id, 'CreatedOn' => new UTCDateTime($this->createdOn), 'UpdatedOn' => new UTCDateTime($this->updatedOn), 'UserUuid' => $this->userUuid, 'EmailAddress' => $this->emailAddress, 'FirstName' => $this->firstName, 'LastName' => $this->lastName, 'Roles' => $this->roles, 'IsUserLocked' => $this->isUserLocked, 'IsUserEnabled' => $this->isUserEnabled ]; } public function __unserialize(array $data): void { $this->id = $data['_id']; $this->createdOn = DateTimeImmutable::createFromMutable($data['CreatedOn']->toDateTime()); $this->updatedOn = DateTimeImmutable::createFromMutable($data['UpdatedOn']->toDateTime()); $this->userUuid = $data['UserUuid']; $this->emailAddress = $data['EmailAddress']; $this->firstName = $data['FirstName']; $this->lastName = $data['LastName']; $this->roles = $data['Roles']; $this->isUserLocked = $data['IsUserLocked']; $this->isUserEnabled = $data['IsUserLocked']; } }