我正在为Symfony 3中的团队构建项目管理工具。我正在使用ramsey/uuid-doctrine来获取系统中的ID。
到目前为止,这对于一对多或多对一关联并不是一个问题,但当我尝试坚持一对一关联时,Doctrine并未将关联实体转换为其关联UUID,而是在SQL中留下null
值。
在这个例子中,我有一个可以有多个WikiPageVersions的WikiPage。 WikiPage与WikiPageVersion(versions
属性:对于页面的所有版本)具有一对多关联,但与WikiPageVersion(一个对一(Unidirectional)的关联( currentVersion
属性:对于,当前版本。)
WikiPage还与Project有多对一关联(用于跟踪wiki页面所针对的项目),并且该属性已正确填充。
WikiPage实体
/**
* @ORM\Table(name="wiki_page")
* @ORM\Entity(repositoryClass="AppBundle\Repository\Project\WikiPageRepository")
*/
class WikiPage
{
/**
* @var Uuid
* @ORM\Id
* @ORM\Column(type="uuid")
*/
protected $id;
/**
* @var Project
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Project\Project", inversedBy="wikiPages")
* @ORM\JoinColumn(name="project_id", referencedColumnName="id")
*/
protected $project;
/**
* @var string
* @ORM\Column(name="title", type="text")
* @Assert\NotBlank()
*/
protected $title;
/**
* @var HiveWikiPageVersion
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Project\WikiPageVersion", fetch="EAGER")
*/
protected $currentVersion;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Project\WikiPageVersion", mappedBy="wikiPage", cascade={"persist", "remove"})
*/
protected $versions;
// -- Class Methods
}
WikiPageVersion实体
/**
* @ORM\Table(name="wiki_page_version")
* @ORM\Entity(repositoryClass="AppBundle\Repository\Project\WikiPageVersionRepository")
*/
class WikiPageVersion
{
/**
* @var Uuid
* @ORM\Id
* @ORM\Column(type="uuid")
*/
protected $id;
/**
* @var WikiPage
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Project\WikiPage", inversedBy="versions")
* @ORM\JoinColumn(name="wiki_page_id", referencedColumnName="id")
* @Assert\NotBlank()
*/
protected $wikiPage;
/**
* @var string
* @ORM\Column(name="content", type="text")
* @Assert\NotBlank()
*/
protected $content;
/**
* @var string
* @ORM\Column(name="version_comment", type="string", length=255)
* @Assert\NotNull()
*/
protected $versionComment;
/**
* @var HiveWikiPageVersion
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Project\WikiPageVersion")
* @ORM\JoinColumn(name="previous_version", referencedColumnName="id")
* @Assert\Type(type="Odev\Hive\Model\Entity\Project\WikiPageVersion")
*/
protected $previousVersion;
/**
* @var \DateTimeInterface
* @ORM\Column(name="created", type="datetime")
* @Assert\NotBlank()
*/
protected $created;
/**
* @var User
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
* @Assert\NotBlank()
*/
protected $createdBy;
}
// -- Methods
这是我在试图浏览WikiPage时从Doctrine获得的错误:
[Doctrine\DBAL\Exception\NotNullConstraintViolationException]
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
Doctrine\DBAL\Driver\AbstractMySQLDriver->convertException() at /home/vagrant/hive/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:128
Doctrine\DBAL\DBALException::driverExceptionDuringQuery() at /home/vagrant/hive/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:177
Doctrine\DBAL\Statement->execute() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:281
Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1014
Doctrine\ORM\UnitOfWork->executeInserts() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:378
Doctrine\ORM\UnitOfWork->commit() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:356
Doctrine\ORM\EntityManager->flush() at /home/vagrant/hive/src/Odev/Hive/Infrastructure/AppBundle/DataFixtures/ORM/LoadProjectData.php:89
Odev\Hive\Infrastructure\AppBundle\DataFixtures\ORM\LoadProjectData->load() at /home/vagrant/hive/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php:121
Doctrine\Common\DataFixtures\Executor\AbstractExecutor->load() at /home/vagrant/hive/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php:88
Doctrine\Common\DataFixtures\Executor\ORMExecutor->Doctrine\Common\DataFixtures\Executor\{closure}() at n/a:n/a
call_user_func() at /home/vagrant/hive/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:233
Doctrine\ORM\EntityManager->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
Doctrine\Common\DataFixtures\Executor\ORMExecutor->execute() at /home/vagrant/hive/vendor/doctrine/doctrine-fixtures-bundle/Command/LoadDataFixturesDoctrineCommand.php:118
Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand->execute() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:262
Symfony\Component\Console\Command\Command->run() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:848
Symfony\Component\Console\Application->doRunCommand() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:189
Symfony\Component\Console\Application->doRun() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:80
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/vagrant/hive/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:120
Symfony\Component\Console\Application->run() at /home/vagrant/hive/bin/console:29
此时我迷失了 - 如果有任何关于我应该在哪里寻找的建议,或者是否有其他人遇到过这个问题,我会很乐意并提供可以提供的指导。
更新
按照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在Project的构造函数中生成,而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');
}
希望有所帮助。
答案 0 :(得分:0)
当WikiPage实体尝试INSERT时,它试图插入其所有属性。检查版本,如果=== null取消设置密钥索引。然后当 INSERT触发parm数组只有4个键。