使用 Doctrine 2 和 YAML 映射嵌套树
Mapping Nested Tree With Doctrine 2 and 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 实体管理器之前注册树监听器:
$eventManager->addEventSubscriber(new TreeListener());
return EntityManager::create(config('doctrine.connection'), $doctrineConfig, $eventManager);
我们已经尝试了几个小时来连接这个东西,但它总是因持久性而失败,我们也找不到原因。我们已经通过所有不同的元素跟踪了代码,但是我们看不到我们从哪里得到这个错误。非常感谢任何帮助,或者也欢迎我们如何使用 YAML 连接树的示例。
我们发现在 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
我们目前正在使用 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 实体管理器之前注册树监听器:
$eventManager->addEventSubscriber(new TreeListener());
return EntityManager::create(config('doctrine.connection'), $doctrineConfig, $eventManager);
我们已经尝试了几个小时来连接这个东西,但它总是因持久性而失败,我们也找不到原因。我们已经通过所有不同的元素跟踪了代码,但是我们看不到我们从哪里得到这个错误。非常感谢任何帮助,或者也欢迎我们如何使用 YAML 连接树的示例。
我们发现在 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