我正在Symfony 3中为我的团队构建项目管理工具。
到目前为止,这并不是一对多或多对一关联的问题,但是当我试图坚持一对一的关联时,学说并没有将关联实体转换为其UUID,而是在SQL中留下null
值。
在此示例中,我有一个可以具有多个wikipageversions的Wikipage。Wikipage与WikipageVersion(versions
属性:对于页面的所有版本)有一对一的关联,但也与WikipageVersion(currentVersion
属性:wikipageversion:work wor to the wikipageversion)有一对一的(单向)关联当前版本)。
Wikipage还与项目有多一对一的关联(跟踪Wiki页面的哪个项目),并且该属性正确填充了。
wikipage实体
/**
* @ORMTable(name="wiki_page")
* @ORMEntity(repositoryClass="AppBundleRepositoryProjectWikiPageRepository")
*/
class WikiPage
{
/**
* @var Uuid
* @ORMId
* @ORMColumn(type="uuid")
*/
protected $id;
/**
* @var Project
* @ORMManyToOne(targetEntity="AppBundleEntityProjectProject", inversedBy="wikiPages")
* @ORMJoinColumn(name="project_id", referencedColumnName="id")
*/
protected $project;
/**
* @var string
* @ORMColumn(name="title", type="text")
* @AssertNotBlank()
*/
protected $title;
/**
* @var HiveWikiPageVersion
* @ORMOneToOne(targetEntity="AppBundleEntityProjectWikiPageVersion", fetch="EAGER")
*/
protected $currentVersion;
/**
* @var ArrayCollection
* @ORMOneToMany(targetEntity="AppBundleEntityProjectWikiPageVersion", mappedBy="wikiPage", cascade={"persist", "remove"})
*/
protected $versions;
// -- Class Methods
}
wikipageversion实体
/**
* @ORMTable(name="wiki_page_version")
* @ORMEntity(repositoryClass="AppBundleRepositoryProjectWikiPageVersionRepository")
*/
class WikiPageVersion
{
/**
* @var Uuid
* @ORMId
* @ORMColumn(type="uuid")
*/
protected $id;
/**
* @var WikiPage
* @ORMManyToOne(targetEntity="AppBundleEntityProjectWikiPage", inversedBy="versions")
* @ORMJoinColumn(name="wiki_page_id", referencedColumnName="id")
* @AssertNotBlank()
*/
protected $wikiPage;
/**
* @var string
* @ORMColumn(name="content", type="text")
* @AssertNotBlank()
*/
protected $content;
/**
* @var string
* @ORMColumn(name="version_comment", type="string", length=255)
* @AssertNotNull()
*/
protected $versionComment;
/**
* @var HiveWikiPageVersion
* @ORMOneToOne(targetEntity="AppBundleEntityProjectWikiPageVersion")
* @ORMJoinColumn(name="previous_version", referencedColumnName="id")
* @AssertType(type="OdevHiveModelEntityProjectWikiPageVersion")
*/
protected $previousVersion;
/**
* @var DateTimeInterface
* @ORMColumn(name="created", type="datetime")
* @AssertNotBlank()
*/
protected $created;
/**
* @var User
* @ORMManyToOne(targetEntity="AppBundleEntityUser")
* @ORMJoinColumn(name="created_by", referencedColumnName="id")
* @AssertNotBlank()
*/
protected $createdBy;
}
// -- Methods
到目前为止的故障排除
- 我可以确认在坚持Wikipage之前,Wikipageversion已关联。
- 我可以通过我的项目和Wikipageversion Entites复制相同的行为(项目与Wikipage的Wiki Homepage和WikipageVersion与Wikipage有一对一的关联,而Wikipageversion与以前版本的自身有一对一的关联)。唯一的一对一关联不会转换UUID。
- 试图从Symfony Controller或加载学说固定装置时,我也有同样的问题。
- 我试图追踪使用Xdebug进行转换的位置,但是我并不是专注于使用调试器的精通,并且经过20分钟的登上调试器,我找不到该字段的转换发生的位置。我要么跳过它发生的循环,要么只是缺少它。浪费了一个小时,试图找到问题,我不得不放弃。
这是我尝试蠕动Wikipage时从学说中得出的错误:
[DoctrineDBALExceptionNotNullConstraintViolationException]
An exception occurred while executing 'INSERT INTO wiki_page (id, project_home, title, project_id, current_version_id) VALUES (?, ?, ?, ?, ?)' with params ["ddc1f51a-f5d9-489f-89bb-cd79f3393af0", 1
, "Technical Reviews Wiki", "5138b185-b10b-48ac-a102-bdea1139c911", null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'current_version_id' cannot be null
和异常跟踪(此异常来自固定装置期间的保存):
Exception trace:
() at /home/vagrant/hive/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:112
DoctrineDBALDriverAbstractMySQLDriver->convertException() at /home/vagrant/hive/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:128
DoctrineDBALDBALException::driverExceptionDuringQuery() at /home/vagrant/hive/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:177
DoctrineDBALStatement->execute() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:281
DoctrineORMPersistersEntityBasicEntityPersister->executeInserts() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1014
DoctrineORMUnitOfWork->executeInserts() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:378
DoctrineORMUnitOfWork->commit() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:356
DoctrineORMEntityManager->flush() at /home/vagrant/hive/src/Odev/Hive/Infrastructure/AppBundle/DataFixtures/ORM/LoadProjectData.php:89
OdevHiveInfrastructureAppBundleDataFixturesORMLoadProjectData->load() at /home/vagrant/hive/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php:121
DoctrineCommonDataFixturesExecutorAbstractExecutor->load() at /home/vagrant/hive/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php:88
DoctrineCommonDataFixturesExecutorORMExecutor->DoctrineCommonDataFixturesExecutor{closure}() at n/a:n/a
call_user_func() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:233
DoctrineORMEntityManager->transactional() at /dev/shm/app/cache/dev/appDevDebugProjectContainer.php:5645
DoctrineORMEntityManager_00000000015ce1f5000000002a2c79364ae5d79093a662a969d1540330e84087->transactional() at /home/vagrant/hive/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php:90
DoctrineCommonDataFixturesExecutorORMExecutor->execute() at /home/vagrant/hive/vendor/doctrine/doctrine-fixtures-bundle/Command/LoadDataFixturesDoctrineCommand.php:118
DoctrineBundleFixturesBundleCommandLoadDataFixturesDoctrineCommand->execute() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:262
SymfonyComponentConsoleCommandCommand->run() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:848
SymfonyComponentConsoleApplication->doRunCommand() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:189
SymfonyComponentConsoleApplication->doRun() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:80
SymfonyBundleFrameworkBundleConsoleApplication->doRun() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:120
SymfonyComponentConsoleApplication->run() at /home/vagrant/hive/bin/console:29
在这一点上,我迷路了 - 如果有任何建议我应该寻找的地方,或者如果其他人遇到了这个问题,我会喜欢和指导。
update
按Matteo的要求:
这是固定装置的load
方法,可创建记录并抛出错误。
/**
* {@inheritDoc}
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager)
{
// Create ODEV Project Section
$section = new ProjectSection(
Uuid::uuid4(),
'ODEV',
'ODEV specific projects'
);
$manager->persist($section);
$this->addReference('section-odev', $section);
// Create Technical Review Project -> public
$project = new Project(
Uuid::uuid4(),
'techreview',
$section,
'Technical Reviews',
'Technical Reviews for Work Requests',
false,
false,
$this->getReference('user-system'),
$this->getReference('user-system'),
'',
$this->getReference('user-system')
);
// VarDumper::dump($project->getWikiHome()->getId());
// VarDumper::dump($project->getCreatedBy()->getId());
$manager->persist($project);
$this->addReference('project-tech-review', $project);
$manager->flush();
}
两个VarDumper::dump()
命令是确认关联正在创建。
在项目的构造函数中生成了实际的Wikipage,并且Wikipageversion是在Wikipages的构造函数中生成的。
这是Project
的构造函数:
public function __construct(
string $id,
string $identifier,
ProjectSection $section,
string $name,
string $description,
bool $private,
bool $sensitive,
User $owner,
User $contact,
string $homepage,
User $createdBy
) {
$this->id = Uuid::fromString($id);
$this->identifier = $identifier;
$this->projectSection = $section;
$this->name = $name;
$this->description = $description;
$this->private = $private;
$this->sensitive = $sensitive;
$this->owner = $owner;
$this->contact = $contact;
$this->homepage = $homepage;
$this->createdBy = $createdBy;
$this->created = new DateTimeImmutable();
$this->updatedBy = $createdBy;
$this->updated = new DateTimeImmutable();
$this->archived = false;
$this->workRequest = null;
// set up collections
$this->teamMembers = new ArrayCollection();
$this->issues = new ArrayCollection();
$this->folders = new ArrayCollection($this->defaultFolders());
$this->wikiHome = $this->defaultWikiPage();
$this->wikiPages = new ArrayCollection([$this->wikiHome]);
$this->labels = new ArrayCollection($this->defaultLabels());
$this->milestones = new ArrayCollection($this->defaultMilestones());
}
protected function defaultWikiPage(): WikiPage
{
return new WikiPage(Uuid::uuid4(), $this, $this->name.' Wiki', '', $this->createdBy);
}
和WikiPage
的构造函数:
public function __construct(string $id, Project $project, string $title, string $content, User $createdBy)
{
$this->id = Uuid::fromString($id);
$this->project = $project;
$this->title = $title;
$this->content = $content;
$this->created = new DateTimeImmutable();
$this->createdBy = $createdBy;
$this->currentVersion = $this->createFirstVersion($content, $createdBy);
$this->versions = new ArrayCollection([$this->currentVersion]);
}
protected function createFirstVersion(string $content, User $createdBy)
{
return new WikiPageVersion(Uuid::uuid4(), $this, $content, $createdBy, 'Page Created');
}
希望会有所帮助。
wikipage实体试图插入它正在尝试插入其所有属性时。进行检查版本,如果=== null未设置密钥索引。然后插入射击PARM阵列只有4个键。