PHP code example of eman-development-design / mongodb-helpers

1. Go to this page and download the library: Download eman-development-design/mongodb-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eman-development-design / mongodb-helpers example snippets


class UserSorting extends DataSorting
{
    public const Email = 'Email';

    public const FirstName = 'FirstName';

    public const LastName = 'LastName';
}

UserSorting::AddToSortList(UserSorting::FirstName, UserSorting::ASC);

$sort = UserSorting::GetSortList();

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);
    }
}