Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A sample Symfony project implementing doctrine mongodb. This repository contains a symfony project and the mongodb service. Services run on docker.

License

Notifications You must be signed in to change notification settings

symfony-examples/doctrine-mongodb

Repository files navigation

SYMFONY EXAMPLES : doctrine mongodb

CI Security

About this repository

This is a simple implementation of doctrine mongodb.

Installation

Requirements

  • git
  • docker
  • docker-compose
  • makefile

Steps

Clone the project

git clone https://github.com/symfony-examples/doctrine-mongodb.git

Installation

make install-local

Enjoy ! πŸ₯³

Documentation

Mongodb extension

Add mongodb extension to php.ini

; mongodb.ini
extension=mongodb.so

Mongodb driver

Install mongodb driver

## INSTALL MONGODB DRIVER
RUN set -xe \
 && apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS openssl curl-dev openssl-dev \
 && pecl install mongodb
COPY ./.docker/php/mongodb.ini /usr/local/etc/php/conf.d/

Installation using composer

Note : If you use flex, you need to run this command before installation of the doctrine/mongodb-odm-bundle package

composer config extra.symfony.allow-contrib true

Install the package

composer require doctrine/mongodb-odm-bundle

Show mongodb commands
./bin/console list doctrine:mongodb

ODM

Document

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
#[ODM\Document(collection: 'Company')]
class Company
{
 #[ODM\Id(type: 'string', strategy: 'auto')]
 private ?string $id = null;
}

EmbeddedDocument

NOTE : don't add primary key to an embedded document

EmbedOne

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
#[ODM\EmbeddedDocument]
class Registration
{
 // without id
}
#[ODM\Document(collection: 'Company')]
class Company
{
 #[ODM\Id(type: 'string', strategy: 'auto')]
 private ?string $id = null;
 #[ODM\EmbedOne(targetDocument: Registration::class)]
 private Registration $registration;
}

EmbedMany

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
#[ODM\EmbeddedDocument]
class Address
{
}
#[ODM\Document(collection: 'Company')]
class Company
{
 #[ODM\Id(type: 'string', strategy: 'auto')]
 private ?string $id = null;
 #[ODM\EmbedMany(targetDocument: Address::class)]
 private Collection $addresses;
 
 public function __construct()
 {
 $this->addresses = new ArrayCollection();
 }
}

ReferenceDocument

ReferenceOne

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
#[ODM\Document(collection: 'Product')]
class Product
{
 #[ODM\Id(type: 'string', strategy: 'auto')]
 private ?string $id = null;
 #[ODM\ReferenceOne(targetDocument: Store::class, cascade: 'persist')]
 private Store $store;
}
#[ODM\Document(collection: 'Store')]
class Store
{
 #[ODM\Id(type: 'string', strategy: 'auto')]
 private ?string $id = null;
}

ReferenceMany

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
#[ODM\Document(collection: 'Store')]
class Store
{
 #[ODM\Id(type: 'string', strategy: 'auto')]
 private ?string $id = null;
 
 #[ODM\ReferenceMany(targetDocument: Product::class, mappedBy: 'store')]
 private Collection $products;
 public function __construct()
 {
 $this->products = new ArrayCollection();
 }
}
#[ODM\Document(collection: 'Product')]
class Product
{
 #[ODM\Id(type: 'string', strategy: 'auto')]
 private ?string $id = null;
 // inversedBy is optional, don't add it if you don't need the bi-directional reference
 #[ODM\ReferenceOne(targetDocument: Store::class, cascade: 'persist', inversedBy: 'products')]
 private Store $store;
}

References

About

A sample Symfony project implementing doctrine mongodb. This repository contains a symfony project and the mongodb service. Services run on docker.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /