繁体   English   中英

Ramsey \\ UUID无法与Doctrine OneToOne关联一起使用

[英]Ramsey\UUID not working with Doctrine OneToOne Associations

我正在为Symfony 3中的团队构建项目管理工具。我正在使用ramsey / uuid-doctrine作为系统中的ID。

到目前为止,这对一对多或多对一关联还不是问题,但是当我尝试保持一对一关联时,Doctrine并没有将关联实体转换为其UUID,并且而是在SQL中保留一个null值。

在此示例中,我有一个WikiPage,它可以具有多个WikiPageVersions。 WikiPage与WikiPageVersion具有一对多关联( versions属性:用于页面的所有版本),但与WikiPageVersion具有一对一( 单向 )关联( 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

到目前为止的故障排除

  1. 我可以确认,在持久保存WikiPage之前,已关联WikiPageVersion。
  2. 我可以使用我的Project和WikiPageVersion实体复制相同的行为(对于Wiki主页,Project与WikiPage具有一对一关联,而WikiPageVersion与WikiPageVersion具有与自身的一对一关联)。 一对一关联不转换UUID。
  3. 尝试从Symfony Controller或从加载Doctrine固定装置持久时,我遇到相同的问题。
  4. 我尝试使用xdebug跟踪转换发生的位置,但我并不精通使用调试器,并且在调试器运行20分钟后,我找不到该字段的转换发生位置。 我要么跳过它发生的循环,要么就错过它。 在花了一个小时跳过尝试查找问题的运行之后,我不得不放弃。

这是我尝试保留WikiPage时从教义中得到的错误:

[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的构造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,则取消设置键索引。 那么当INSERT触发时,parm数组只有4个键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM