使用Doctrine 2和YAML映射嵌套树

时间:2015-05-15 16:52:05

标签: doctrine-orm tree doctrine yaml

我们目前正在使用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连接树的示例也将受到欢迎。

1 个答案:

答案 0 :(得分:0)

我们发现在树映射文件中,我们必须将左侧命名为:右侧:列为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