Encode embedded documents with updateMany->set($embed)

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Fixed
    • Priority: Unknown
    • doctrine-2.13.0
    • Affects Version/s: None
    • Component/s: Doctrine
    • None
    • 🔵 Done
    • PHP Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      https://github.com/doctrine/mongodb-odm/issues/2825

      When passing an embedded document, it must be encoded.

       

      use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
      #[ODM\EmbeddedDocument()]
      class Embedded {
       public function __construct(
              #[ODM\Field(type: 'string')]
              #[ODM\Index()]
              private string $name,
      }
      }
      #[ODM\Document()]
      class MainDocument {
          #[ODM\EmbedOne(targetDocument: Embedded::class)]
              private Embedded $embedded;
      }
      

       

      In MainDocumentRepository:

              $qb = $this->createQueryBuilder();
              $qb->updateMany()
                  ->field('embedded.name')->equals($embedded->getName())
                  ->field('embedded')->set($embedded)
                  ->getQuery()
                  ->execute();

      updateMany() accepts to set EmbeddedDocument, and stores the full document {{

      { "name": "value" }

      }} in the target field.

            Assignee:
            Andreas Braun
            Reporter:
            Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: