I'm using Doctrine 2.10 with Symfony 4. I can see in the documentation for Doctrine's ODM references that a referenceMany
relationship can store different types of documents in references by omitting the targetDocument
option:
/* @Document */
Class User
{
/* @ReferenceMany(targetDocument="Account") */
private accounts = [];
}
becomes
/* @Document */
Class User
{
/* @ReferenceMany */
private accounts = [];
}
It seems that the same can't be achieved with the referenceOne
relationship. When I use referenceOne
without the targetDocument
option, I get a mapping exception:
Class '' does not exist
Ideally I'd like to be able to use the mappedBy
and inversedBy
options so that the Account document owns the relationship, and is able to reference either a User or a Company, which share some properties. The following doesn't work, but is there a way to achieve what it represents?
/* @Document */
class User
{
/* @ReferenceMany(targetDocument="Account", mappedBy="owner") */
private $accounts = [];
}
/* @Document */
class Company
{
/* @ReferenceMany(targetDocument="Account", mappedBy="owner") */
private $accounts = [];
}
/* @Document */
class Account
{
/* @ReferenceOne(inversedBy="accounts") */
private $owner;
}