Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add attribute driver support #692

Merged
merged 6 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions DependencyInjection/Compiler/DoctrineMongoDBMappingsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler;

use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver;
use Doctrine\Persistence\Mapping\Driver\PHPDriver;
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
Expand Down Expand Up @@ -112,6 +113,27 @@ public static function createAnnotationMappingDriver(array $namespaces, array $d
return new DoctrineMongoDBMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
}

/**
* @param array $namespaces List of namespaces that are handled with attribute mapping
* @param array $directories List of directories to look for attribute mapping files
* @param string[] $managerParameters List of parameters that could which object manager name
* your bundle uses. This compiler pass will automatically
* append the parameter name for the default entity manager
* to this list.
* @param string|false $enabledParameter Service container parameter that must be present to
* enable the mapping. Set to false to not do any check,
* optional.
* @param string[] $aliasMap Map of alias to namespace.
*
* @return DoctrineMongoDBMappingsPass
*/
public static function createAttributeMappingDriver(array $namespaces, array $directories, array $managerParameters, $enabledParameter = false, array $aliasMap = [])
{
$driver = new Definition(AttributeDriver::class, [$directories, new Reference('doctrine_mongodb.odm.metadata.attribute_reader')]);

return new DoctrineMongoDBMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
}

/**
* @param array $namespaces List of namespaces that are handled with static php mapping
* @param array $directories List of directories to look for static php mapping files
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<!-- metadata -->
<parameter key="doctrine_mongodb.odm.metadata.driver_chain.class">Doctrine\Persistence\Mapping\Driver\MappingDriverChain</parameter>
<parameter key="doctrine_mongodb.odm.metadata.annotation.class">Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver</parameter>
<parameter key="doctrine_mongodb.odm.metadata.attribute.class">Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver</parameter>
<parameter key="doctrine_mongodb.odm.metadata.attribute_reader.class">Doctrine\ODM\MongoDB\Mapping\Driver\AttributeReader</parameter>
<parameter key="doctrine_mongodb.odm.metadata.xml.class">Doctrine\Bundle\MongoDBBundle\Mapping\Driver\XmlDriver</parameter>

<!-- directories -->
Expand Down Expand Up @@ -115,6 +117,11 @@
<argument>%doctrine_mongodb.odm.document_dirs%</argument>
</service>
<service id="doctrine_mongodb.odm.metadata.annotation_reader" alias="annotation_reader" />
<service id="doctrine_mongodb.odm.metadata.attribute" class="%doctrine_mongodb.odm.metadata.attribute.class%">
<argument>%doctrine_mongodb.odm.document_dirs%</argument>
<argument type="service" id="doctrine_mongodb.odm.metadata.attribute_reader" />
</service>
<service id="doctrine_mongodb.odm.metadata.attribute_reader" class="%doctrine_mongodb.odm.metadata.attribute_reader.class%" />
<service id="doctrine_mongodb.odm.metadata.xml" class="%doctrine_mongodb.odm.metadata.xml.class%">
<argument>%doctrine_mongodb.odm.xml_mapping_dirs%</argument>
</service>
Expand Down
8 changes: 4 additions & 4 deletions Resources/doc/.doctor-rst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ rules:
no_app_bundle: ~

versionadded_directive_major_version:
major_version: 3
major_version: 4

versionadded_directive_min_version:
min_version: '3.0'
min_version: '4.0'

deprecated_directive_major_version:
major_version: 3
major_version: 4

deprecated_directive_min_version:
min_version: '3.0'
min_version: '4.0'
6 changes: 3 additions & 3 deletions Resources/doc/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Explicit definition of all the mapped documents is the only necessary
configuration for the ODM and there are several configuration options that you
can control. The following configuration options exist for a mapping:

- ``type`` One of ``annotation``, ``xml``, ``yml``, ``php`` or ``staticphp``.
- ``type`` One of ``annotation``, ``attribute``, ``xml``, ``php`` or ``staticphp``.
This specifies which type of metadata type your mapping uses.

- ``dir`` Path to the mapping or document files (depending on the driver). If
Expand Down Expand Up @@ -176,7 +176,7 @@ The following configuration shows a bunch of mapping examples:
mappings:
MyBundle1: ~
MyBundle2: xml
MyBundle3: { type: annotation, dir: Documents/ }
MyBundle3: { type: attribute, dir: Documents/ }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if we keep annotation example and add a new one for attribute

MyBundle4: { type: xml, dir: Resources/config/doctrine/mapping }
MyBundle5:
type: xml
Expand All @@ -202,7 +202,7 @@ The following configuration shows a bunch of mapping examples:
<doctrine_mongodb:document-manager id="default">
<doctrine_mongodb:mapping name="MyBundle1" />
<doctrine_mongodb:mapping name="MyBundle2" type="yml" />
<doctrine_mongodb:mapping name="MyBundle3" type="annotation" dir="Documents/" />
<doctrine_mongodb:mapping name="MyBundle3" type="attribute" dir="Documents/" />
<doctrine_mongodb:mapping name="MyNundle4" type="xml" dir="Resources/config/doctrine/mapping" />
<doctrine_mongodb:mapping name="MyBundle5" type="xml" dir="my-bundle-mappings-dir" alias="BundleAlias" />
<doctrine_mongodb:mapping name="doctrine_extensions"
Expand Down
147 changes: 102 additions & 45 deletions Resources/doc/cookbook/registration_form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,122 @@ The User Model

We begin this tutorial with the model for a ``User`` document:

.. code-block:: php
.. configuration-block::

// src/Document/User.php
namespace App\Document;
.. code-block:: php-annotations

use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;
// src/Document/User.php
namespace App\Document;

/**
* @MongoDB\Document(collection="users")
* @MongoDBUnique(fields="email")
*/
class User
{
/**
* @MongoDB\Id
*/
protected $id;

/**
* @MongoDB\Field(type="string")
* @Assert\NotBlank()
* @Assert\Email()
*/
protected $email;
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @MongoDB\Field(type="string")
* @Assert\NotBlank()
* @MongoDB\Document(collection="users")
* @MongoDB\Unique(fields="email")
*/
protected $password;

public function getId()
class User
{
return $this->id;
/**
* @MongoDB\Id
*/
protected $id;

/**
* @MongoDB\Field(type="string")
* @Assert\NotBlank()
* @Assert\Email()
*/
protected $email;

/**
* @MongoDB\Field(type="string")
* @Assert\NotBlank()
*/
protected $password;

public function getId()
{
return $this->id;
}

public function getEmail()
{
return $this->email;
}

public function setEmail($email)
{
$this->email = $email;
}

public function getPassword()
{
return $this->password;
}

// stupid simple encryption (please don't copy it!)
public function setPassword($password)
{
$this->password = sha1($password);
}
}

public function getEmail()
{
return $this->email;
}
.. code-block:: php-attributes

public function setEmail($email)
{
$this->email = $email;
}
// src/Document/User.php
namespace App\Document;

public function getPassword()
{
return $this->password;
}
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

// stupid simple encryption (please don't copy it!)
public function setPassword($password)
#[MongoDB\Document(collection: 'users')]
#[MongoDB\Unique(fields: 'email')]
class User
{
$this->password = sha1($password);
/**
* @MongoDB\Id
*/
#[MongoDB\Id]
protected string $id;

#[MongoDB\Field(type: 'string')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in our examples we could suggest to use constants like Doctrine\ODM\MongoDB\Types\Type::STRING

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be changed in annotation examples then too but I think this is a candidate for a separate PR.
I don't see the constants usages in ORM docs neither and I don't think it's clear to new developers.

#[Assert\NotBlank]
#[Assert\Email]
protected ?string $email = null;

#[MongoDB\Field(type: 'string')]
#[Assert\NotBlank]
protected ?string $password = null;

public function getId(): string
{
return $this->id;
}

public function getEmail(): ?string
{
return $this->email;
}

public function setEmail(?string $email): void
{
$this->email = $email;
}

public function getPassword(): ?string
{
return $this->password;
}

// stupid simple encryption (please don't copy it!)
public function setPassword(?string $password): void
{
$this->password = sha1($password);
}
}
}

This ``User`` document contains three fields and two of them (email and
password) should be displayed in the form. The email property must be unique
Expand Down
70 changes: 54 additions & 16 deletions Resources/doc/first_steps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,15 @@ For Doctrine to be able to do this, you have to create "metadata", or
configuration that tells Doctrine exactly how the ``Product`` class and its
properties should be *mapped* to MongoDB. This metadata can be specified
in a number of different formats including XML or directly inside the
``Product`` class via annotations:
``Product`` class via annotations or PHP 8 attributes:

.. configuration-block::

.. code-block:: xml
.. versionadded:: 4.4

<!-- src/Resources/config/doctrine/Product.mongodb.xml -->
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
https://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
The attribute mapping support was added in Doctrine MongoDB ODM Bundle 4.4 and requires PHP 8.0 or newer.

<document name="App\Document\Product">
<id />
<field field-name="name" type="string" />
<field field-name="price" type="float" />
</document>
</doctrine-mongo-mapping>
.. configuration-block::

.. code-block:: php
.. code-block:: php-annotations

// src/Document/Product.php
namespace App\Document;
Expand Down Expand Up @@ -93,6 +82,41 @@ in a number of different formats including XML or directly inside the
protected $price;
}

.. code-block:: php-attributes

// src/Document/Product.php
namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

#[MongoDB\Document]
class Product
{
#[MongoDB\Id]
protected string $id;

#[MongoDB\Field(type: 'string')]
protected string $name;

#[MongoDB\Field(type: 'float')]
protected float $price;
}

.. code-block:: xml

<!-- src/Resources/config/doctrine/Product.mongodb.xml -->
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
https://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="App\Document\Product">
<id />
<field field-name="name" type="string" />
<field field-name="price" type="float" />
</document>
</doctrine-mongo-mapping>

.. seealso::

You can also check out Doctrine's `Basic Mapping Documentation`_ for
Expand Down Expand Up @@ -335,6 +359,20 @@ To do this, add the name of the repository class to your mapping definition.
// ...
}

.. code-block:: php-attributes

// src/Document/Product.php
namespace App\Document;

use App\Repository\ProductRepository;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

#[MongoDB\Document(repositoryClass: ProductRepository::class)]
class Product
{
// ...
}

.. code-block:: xml

<!-- src/Resources/config/doctrine/Product.mongodb.xml -->
Expand Down
Loading