Giter Site home page Giter Site logo

incubator-mongodb's Introduction

Phalcon\Incubator\MongoDB

Discord Packagist Version PHP from Packagist codecov Packagist

Issues tracker

https://github.com/phalcon/incubator/issues

What is it

Set of helpers - simplifying working with mongodb via AR paradigm.

Helper

Phalcon\Incubator\MongoDB\Helper

Method Description
Helper::isValidObjectId($id) Checks if id parameter is a valid ObjectID
Helper::convertDatetime($datetime) Converts a DateTime object to UTCDateTime from MongoDB

Collection Manager

Manager controls the initialization of collections, keeping record of relations between the different collections of the application.

use Phalcon\Incubator\MongoDB\Mvc\Collection\Manager;

$di->set(
    'collectionsManager',
    function () {
        return new Manager();
    }
);

Collection

ActiveRecord class for the management of MongoDB collections.

Defining collection

use Phalcon\Incubator\MongoDB\Mvc\Collection;

class RobotsCollection extends Collection
{
    public $code;

    public $theName;

    public $theType;

    public $theYear;
}

$robots = new RobotsCollection($data);

Search examples

use MongoDB\BSON\ObjectId;

// How many robots are there?
$robots = RobotsCollection::find();

echo "There are ", count($robots), "\n";

// How many mechanical robots are there?
$robots = RobotsCollection::find([
    [
        "type" => "mechanical",
    ],
]);

echo "There are ", count(robots), "\n";

// Get and print virtual robots ordered by name
$robots = RobotsCollection::findFirst([
    [
        "type" => "virtual",
    ],
    "order" => [
        "name" => 1,
    ],
]);

foreach ($robots as $robot) {
    echo $robot->name, "\n";
}

// Get first 100 virtual robots ordered by name
$robots = RobotsCollection::find([
    [
        "type" => "virtual",
    ],
    "order" => [
        "name" => 1,
    ],
    "limit" => 100,
]);

foreach (RobotsCollection as $robot) {
    echo $robot->name, "\n";
}

$robot = RobotsCollection::findFirst([
    [
        "_id" => new ObjectId("45cbc4a0e4123f6920000002"),
    ],
]);

// Find robot by using \MongoDB\BSON\ObjectId object
$robot = RobotsCollection::findById(
    new ObjectId("545eb081631d16153a293a66")
);

// Find robot by using id as sting
$robot = RobotsCollection::findById("45cbc4a0e4123f6920000002");

// Validate input
if ($robot = RobotsCollection::findById($_POST["id"])) {
    // ...
}

Adding behavior

use Phalcon\Incubator\MongoDB\Mvc\Collection;
use Phalcon\Incubator\MongoDB\Mvc\Collection\Behavior\Timestampable;

class RobotsCollection extends Collection
{
    public $code;

    public $theName;

    public $theType;

    public $theYear;
    
    protected function onConstruct()
    {
         $this->addBehavior(
             new Timestampable(
                 [
                     "beforeCreate" => [
                         "field"  => "created_at",
                         "format" => "Y-m-d",
                     ],
                 ]
             )
         );
    }
}

incubator-mongodb's People

Contributors

dependabot[bot] avatar jeckerson avatar ruudboon avatar tacxou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.