soluti / mongo-bundle
A bundle that provides connectivity to MongoDB
This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.
Requires
- php: >=7.0.0
- doctrine/common: ^2.6
- fzaninotto/faker: ^1.6
- mongodb/mongodb: ^1.0
- symfony/symfony: ~2.8|~3.0
Suggests
- ocramius/proxy-manager: Allows to use the reference hydrator with lazy loading.
This package is not auto-updated.
Last update: 2021-10-15 21:52:01 UTC
README
Provides a way to work with MongoDB new php driver.
Installation
Require the soluti/mongo-bundle package in your composer.json and update your dependencies.
$ composer require soluti/mongo-bundle
Add the SolutiMongoBundle to your application's kernel:
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Soluti\MongoBundle\SolutiMongoBundle(),
            // ...
        );
        // ...
    }
Configuration
To enable storing of sessions inside MongoDB add to config.yml:
framework:
    session:
        storage_id: app.mongo.session_storage
        cookie_httponly: true
        cookie_secure: true
Usage
There are 2 types of models that you can use:
- Persisted inside their own collection
- Embedded as part of another model
In order to create a model that is persisted in their own collection the following is need:
- a model
- a repository
- a hydrator
Model
In order to create a model that can be persisted, your model should implement the Soluti\MongoBundle\Model\ReferenceInterface. A good starting point is extending Soluti\MongoBundle\Model\BaseModel.
Important!!! All base field types are mapped if they have getters/setters. More complex types should be mapped via injected Hydrators.
namespace AppBundle\Model;
use Soluti\MongoBundle\Model\BaseModel;
use Soluti\MongoBundle\Model\Core\Geo;
use Soluti\MongoBundle\Model\ReferenceInterface;
class User extends BaseModel implements ReferenceInterface
{
    ...
    /** @var Geo */
    protected $geo;
    
    /**
     * @return Geo
     */
    public function getGeo(): Geo
    {
        return $this->geo;
    }
    /**
     * @param Geo $geo
     */
    public function setGeo(Geo $geo)
    {
        $this->geo = $geo;
    }
    ...
}
Repository
Your repository should extend Soluti\MongoBundle\Repository\BaseRepository  and implement the missing methods. An example of such a repository would be:
namespace AppBundle\Repository;
use AppBundle\Model\User;
use Soluti\DataFilterBundle\Repository\MongoRepository;
class DeviceRepository extends MongoRepository
{
    /**
     * @inheritdoc
     */
    public static function getCollectionName()
    {
        return User::getCollectionName();
    }
    /**
     * @inheritdoc
     */
    public static function getClassName()
    {
        return User::class;
    }
}
Hydrator
The hydrator should implement Soluti\MongoBundle\Hydrator\HydratorInterface, the bundle provides a 
Soluti\MongoBundle\Hydrator\BaseHydrator for quick use. You can inject any other hydrators via DI, and just provide a 
simple config to link them as appropriate.
namespace AppBundle\Hydrator;
use AppBundle\Model\Device;
use Soluti\MongoBundle\Hydrator\BaseHydrator;
use Soluti\MongoBundle\Hydrator\HydratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserHydrator extends BaseHydrator implements HydratorInterface
{
    const OBJECT = User::class;
    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->config = [
            'geo' => ['hydrator' => $container->get('soluti_mongo.hydrator.geo')],
            'geoUpdatedAt' => ['hydrator' => $container->get('soluti_mongo.hydrator.datetime')],
            'createdAt' => ['hydrator' => $container->get('soluti_mongo.hydrator.datetime')],
        ];
    }
}