【问题标题】:Mapping Nested Tree With Doctrine 2 and YAML使用 Doctrine 2 和 YAML 映射嵌套树
【发布时间】:2025-12-02 18:35:01
【问题描述】:

我们目前正在使用DoctrineExtensions 模块实现嵌套树。我们相信我们已经正确连接了所有东西,但是当我们使用 Doctrine 进行刷新时,我们不断收到异常。

SyntaxErrorException in AbstractMySQLDriver.php line 90: An exception         
occurred while executing 'INSERT INTO ProductTree (left, right,     
rootProductTreeID, level, componentProductComponentID) VALUES (?, ?, ?, 
?, ?)':

You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near 'left, 
right, rootProductTreeID, level, componentProductComponentID) VALUES (?, 
?' at line 1

我们使用的触发这个问题的代码是

$productTree->setRootProductComponentID($productComponent->getID());
$productTree->setComponent($productComponent);

$em->persist($productTree); 
$em->flush();

我们的 YAML 映射看起来就是这样

OS\Domain\Entity\Products\ProductTree:
    type: entity
    repositoryClass: Gedmo\Tree\Entity\Repository\NestedTreeRepository
    table: ProductTree
    gedmo:
      tree:
        type: nested
    indexes:
        rootProductFK_idx:
            columns:
                - rootProductComponentID
        componentProductFK_idx:
            columns:
                - componentProductComponentID
    id:
        id:
            type: integer
            nullable: false
            options:
                unsigned: false
            id: true
            generator:
                strategy: IDENTITY
    fields:
        left:
            type: integer
            nullable: false
            options:
                unsigned: false
            gedmo:
              - treeLeft
        right:
            type: integer
            nullable: false
            options:
                unsigned: false
            gedmo:
              - treeRight
        rootProductTreeID:
            type: integer
            nullable: false
            options:
                unsigned: false
            gedmo:
              - treeRoot
        level:
            type: integer
            gedmo:
              - treeLevel
    manyToOne:
#        parent:
#            targetEntity: OS\Domain\Entity\Products\ProductComponent
#            inversedBy: children
#            joinColumn:
#              name: parentProductComponentID
#              referencedColumnName: id
#              onDelete: CASCADE
#            gedmo:
#              - treeParent
        root:
            targetEntity: OS\Domain\Entity\Products\ProductTree
            cascade: {  }
            fetch: LAZY
            mappedBy: null
            inversedBy: null
            joinColumns:
                rootProductTreeID:
                    referencedColumnName: id
            orphanRemoval: false
            gedmo:
              - treeParent
        component:
            targetEntity: OS\Domain\Entity\Products\ProductComponent
            cascade: {  }
            fetch: LAZY
            mappedBy: null
            inversedBy: null
            joinColumns:
                componentProductComponentID:
                    referencedColumnName: id
            orphanRemoval: false
    oneToMany:
      children:
        targetEntity: OS\Domain\Entity\Products\ProductComponent
        mappedBy: parent
        orderBy:
          left: ASC
    lifecycleCallbacks: {  }

我们对Doctrine的配置添加在Tree Driver中:

                  $metadataDriver = new MappingDriverChain();
                $configuredDriver = $this->createMetadataDriver($doctrineConfig, $metadataConfig);
                $treeDriver = $doctrineConfig->newDefaultAnnotationDriver(
                    '/vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity'
                );
                $metadataDriver->addDriver($configuredDriver,'OS');
                $metadataDriver->addDriver($treeDriver,'Gedmo');
                DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($metadataDriver);

我们在创建 Doctrine Entity Manager 之前注册树监听器:

                $eventManager->addEventSubscriber(new TreeListener());

            return EntityManager::create(config('doctrine.connection'), $doctrineConfig, $eventManager);

我们已经尝试了几个小时来连接这个东西,但它总是在持久性上失败,我们找不到原因。我们已经对所有不同的元素进行了代码跟踪,但是我们看不到这个错误是从哪里得到的。任何帮助将不胜感激,或者我们如何使用 YAML 连接树的示例也将受到欢迎。

【问题讨论】:

    标签: doctrine-orm tree doctrine yaml


    【解决方案1】:

    我们发现在 Tree 映射文件中,我们必须将 left: 和 right: 列命名为 lft: 和 rgt:,这与 github 存储库中用于 yaml 映射的示例相同。

    fields:
        lft: <----
            type: integer
            nullable: false
            options:
                unsigned: false
            gedmo:
              - treeLeft
        rgt: <-----
            type: integer
            nullable: false
            options:
                unsigned: false
            gedmo:
              - treeRight
    

    【讨论】: