Skip to content

[Encryption] PHPORM-360 Document limitations of encryption with collection inheritance #2790

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

Merged
merged 4 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 46 additions & 0 deletions docs/en/reference/attributes-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ Optional arguments:
users only. The default values for these options are suitable for the majority
of use cases, and should only be modified if your use case requires it.

.. note::

Queryable encryption is only supported in MongoDB version 8.0 and later.

Example:

.. code-block:: php
Expand All @@ -373,9 +377,50 @@ Example:
public string $name;
}

The ``#[Encrypt]`` attribute can be added to a class with `#[EmbeddedDocument]`_.
This will encrypt the entire embedded document, in the field that contains it.
Queryable encryption is not supported for embedded documents, so the ``queryType``
argument is not applicable. Encrypted embedded documents are stored as a binary
value in the parent document.

.. code-block:: php

<?php

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

#[Encrypt]
#[EmbeddedDocument]
class CreditCard
{
#[Field]
public string $number;

#[Field]
public string $expiryDate;
}

#[Document]
class User
{
#[EmbedOne(targetDocument: CreditCard::class)]
public CreditCard $creditCard;
}

For more details, refer to the MongoDB documentation on
`Queryable Encryption <https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/>`_.


.. note::

The encrypted collection must be created with the `Schema Manager`_ before
before inserting documents.

.. note::

Due to the way the encrypted fields map is generated, the queryable encryption
is not compatible with ``SINGLE_COLLECTION`` inheritance.

#[Field]
--------

Expand Down Expand Up @@ -1439,5 +1484,6 @@ root class specified in the view mapping.
.. _DBRef: https://docs.mongodb.com/manual/reference/database-references/#dbrefs
.. _geoNear command: https://docs.mongodb.com/manual/reference/command/geoNear/
.. _MongoDB\BSON\ObjectId: https://www.php.net/class.mongodb-bson-objectid
.. _Schema Manager: ../reference/migrating-schemas
.. |FQCN| raw:: html
<abbr title="Fully-Qualified Class Name">FQCN</abbr>
38 changes: 38 additions & 0 deletions docs/en/reference/migrating-schemas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,44 @@ problem!
for the Google App Engine datastore. Additional information may be found in
the `Objectify schema migration`_ documentation.

Creating a collection
--------------------

Collections are automatically created by the MongoDB server upon first insertion.
You must explicitly create the collections if you need specific options, such as
validation rules. In particular, encrypted collections must be created explicitly.

.. code-block:: php

<?php

// Assuming $dm is your DocumentManager instance
$schemaManager = $dm->getSchemaManager();

To create the collections for all the document classes, you can use the
`createCollections()` method on the ``DocumentManager``:

.. code-block:: php

<?php

$schemaManager->createCollections();

For a specific document class, you can use the `createDocumentCollection()`
method with the class name as an argument:

<?php

$schemaManager->createDocumentCollection(Person::class);

Once the collection is created, you can also set up indexes with ``ensureIndexes``,
and search indexes with ``createSearchIndexes``:

<?php

$schemaManager->ensureIndexes();
$schemaManager->createSearchIndexes();

Renaming a Field
----------------

Expand Down
Loading