我有两个实体映射如下:
namespace AppEntityEmail;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use KnpDoctrineBehaviorsContractEntityBlameableInterface;
use KnpDoctrineBehaviorsContractEntitySoftDeletableInterface;
use KnpDoctrineBehaviorsContractEntityTimestampableInterface;
use KnpDoctrineBehaviorsModelBlameableBlameableTrait;
use KnpDoctrineBehaviorsModelSoftDeletableSoftDeletableTrait;
use KnpDoctrineBehaviorsModelTimestampableTimestampableTrait;
/**
* @ORMEntity(repositoryClass="AppRepositoryEmailEmailRepository")
*/
class Email implements SoftDeletableInterface, TimestampableInterface, BlameableInterface
{
use SoftDeletableTrait;
use TimestampableTrait;
use BlameableTrait;
/**
* @ORMColumn(type="integer")
* @ORMId()
* @ORMGeneratedValue()
*/
private ?int $id = null;
...
/**
* @var Collection|EmailAddress[]
* @ORMOneToMany(targetEntity="AppEntityEmailEmailAddress", mappedBy="sentEmail", cascade={"all"}, orphanRemoval=true)
*/
private ?Collection $senders = null;
/***
* @var Collection|EmailAddress[]
* @ORMOneToMany(targetEntity="AppEntityEmailEmailAddress", mappedBy="receivedEmail", cascade={"all"}, orphanRemoval=true)
*/
private ?Collection $recipients = null;
...
}
和
namespace AppEntityEmail;
use DoctrineORMMapping as ORM;
use KnpDoctrineBehaviorsContractEntityBlameableInterface;
use KnpDoctrineBehaviorsContractEntitySoftDeletableInterface;
use KnpDoctrineBehaviorsContractEntityTimestampableInterface;
use KnpDoctrineBehaviorsModelBlameableBlameableTrait;
use KnpDoctrineBehaviorsModelSoftDeletableSoftDeletableTrait;
use KnpDoctrineBehaviorsModelTimestampableTimestampableTrait;
/**
* @ORMEntity()
*/
class EmailAddress implements SoftDeletableInterface, TimestampableInterface, BlameableInterface
{
use SoftDeletableTrait;
use TimestampableTrait;
use BlameableTrait;
/**
* @ORMColumn(type="integer")
* @ORMId()
* @ORMGeneratedValue()
*/
private ?int $id = null;
...
/**
* @ORMManyToOne(targetEntity="AppEntityEmailEmail", inversedBy="senders")
* @ORMJoinColumn()
*/
private ?Email $sentEmail = null;
/**
* @ORMManyToOne(targetEntity="AppEntityEmailEmail", inversedBy="recipients")
* @ORMJoinColumn()
*/
private ?Email $receivedEmail = null;
...
}
出于未知的原因,我收到了来自条令的信息:
The association AppEntityEmailEmailAddress#receivedEmail refers to the inverse side field AppEntityEmailEmail#recipients which does not exist.
我的映射有什么问题?我真的没有看到任何错误。我已经让我的同事检查我的代码,他们也没有发现任何问题。奇怪的是,这种关系发送电子邮件->发件人是根据权利原则映射的,而且它是有效的。
我也尝试过像这样将OneToMany映射更改为ManyToMany,但我仍然遇到了同样的错误。
编辑1:
数据库中的日期:表电子邮件
id | created_by_id | updated_by_id | deleted_by_id | deleted_at | created_at | updated_at | subject | content
1 | NULL | NULL | NULL | NULL | 1616156920 | 1616156920 | Test | Test
表电子邮件地址
id | created_by_id | updated_by_id | deleted_by_id | address | deleted_at | created_at | updated_at | sent_email_id | received_email_id
1 | NULL | NULL | NULL | test1@test.com | NULL | 1616156920 | 1616156920 | NULL | 1
2 | NULL | NULL | NULL | test2@test.com | NULL | 1616156920 | 1616156920 | 1 | NULL
这个问题/答案可能以"打字错误"为标题,但我认为讨论起来可能很有趣。
问题就在这里:
/***
* @var Collection|EmailAddress[]
* @ORMOneToMany(targetEntity="AppEntityEmailEmailAddress", mappedBy="receivedEmail", cascade={"all"}, orphanRemoval=true)
*/
private ?Collection $recipients = null;
注释块打开中的额外星号/***导致Doctrine跳过了收件人属性。但一切似乎都很好。数据库表和索引都按预期生成。
我制作了一个简单的控制台命令来插入一个电子邮件实体,并很快注意到插入了发件人地址,但没有插入收件人地址。Triple检查了各种方法,但仍然没有通过。显式地持久化地址是有效的,但是级联选项本应该解决这个问题。当然,即使在插入实体之后,检索收件人地址也不起作用。
在某个时刻,我注意到了/***,并将其更改为/***,一切都如预期。与刷新浏览器和在调试栏中四处搜索相比,使用控制台命令进行测试有很大帮助。
从好的方面来说,如果你需要暂时删除一个注释,那么只添加一个星号基本上与注释掉它是一样的。