I have a collection set up in DocumentDB. Let's call it persons_collection
. Each document in this collection has an array of objects. Let's say it is a list of books each person read. So a sample document would look line this:
{
'name': 'John',
'lastName': 'Doe',
'books': [
{
'title': 'Blood of Elves',
'author': 'Andrzej Sapkowski'
},
{
'title': 'The Color of Magic',
'author': 'Terry Pratchett'
},
{
'title': 'The Warded Man',
'author': 'Peter V. Brett'
}
]
}
I have set up two entities in Doctrine (source code of these classes below) and a repository class that extends ServiceDocumentRepository
.
The problem I am having is that when I call findOneBy
on the repository class, it works on a pure MongoDB database, and the embedded array of Books is retrieved properly.
Howewver, when used with DocumentDB, it returns an array of empty objects instead. The number of objects is correct, but they are empty. Is there anything that should be configured specifically for DocumentDB?
Classes:
Book.php
<?php
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* Class Book
* @MongoDB\EmbeddedDocument
*/
class Book
{
/**
* @MongoDB\Field(type="string", name="title")
*/
protected string $title;
/**
* @MongoDB\Field(type="string", name="author")
*/
protected string $author;
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): Book
{
$this->title = $title;
return $this;
}
public function getAuthor(): string
{
return $this->author;
}
public function setAuthor(string $author): Book
{
$this->author = $author;
return $this;
}
}
Person.php
<?php
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use App\Infrastructure\Persistence\Doctrine\Repository\PersonRepository;
/**
* Class Person
* @package App\Infrastructure\Persistence\Doctrine\Document
* @MongoDB\Document(
* repositoryClass=PersonRepository::class,
* collection="persons_collection"
* )
*/
class Person
{
/**
* @MongoDB\Field(type="string", name="name")
*/
protected string $name;
/**
* @MongoDB\Field(type="string", name="lastName")
*/
protected string $lastName;
/**
* @MongoDB\EmbedMany(targetDocument=Book::class)
* @var array|Collection
*/
protected $books = [];
public function getName(): string
{
return $this->name;
}
public function setName(string $name): Person
{
$this->name = $name;
return $this;
}
public function getLastName(): string
{
return $this->lastName;
}
public function setLastName(string $lastName): Person
{
$this->lastName = $lastName;
return $this;
}
/**
* @return array|Collection
*/
public function getBooks()
{
return $this->books;
}
/**
* @param array|Collection $books
* @return Person
*/
public function setBooks($books): Person
{
$this->books = $books;
return $this;
}
}
Repository
<?php
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository;
class DocumentRepository extends ServiceDocumentRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Person::class);
}
}