Skip to content

bakaphp/incubator-mongodb

This branch is 63 commits behind phalcon/incubator-mongodb:master.

Folders and files

NameName
Last commit message
Last commit date
Oct 19, 2021
Aug 22, 2020
Aug 31, 2020
Jul 26, 2020
Jul 26, 2020
Jul 26, 2020
Jul 26, 2020
Jun 25, 2020
Jul 26, 2020
Nov 2, 2021
Aug 31, 2020
Oct 3, 2021
Oct 3, 2021
Jul 26, 2020
Jul 28, 2020
Oct 3, 2021

Repository files navigation

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(
    'collectionManager',
    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",
                     ],
                 ]
             )
         );
    }
}

About

MongoDB Database adapter for Phalcon Framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.7%
  • Other 0.3%