Doctrine orm 许多关系问题
我试图将条令实体和关系添加到现有的数据库模式中,但遇到了一些问题 我有4张桌子:Doctrine orm 许多关系问题,doctrine-orm,many-to-many,entity-relationship,Doctrine Orm,Many To Many,Entity Relationship,我试图将条令实体和关系添加到现有的数据库模式中,但遇到了一些问题 我有4张桌子: +-------------+ +-----------+ +-----------------+ +-------------------+ | customers | | acl_roles | | acl_permissions | | acl_customer_role | --------------- ------------- ------------------- ------
+-------------+ +-----------+ +-----------------+ +-------------------+
| customers | | acl_roles | | acl_permissions | | acl_customer_role |
--------------- ------------- ------------------- ---------------------
| customer_id | | id | | role_id | | customer_id |
+-------------+ | name | | resource_id | | acl_role_id |
+------------ | flags | +--------------------
+------------------
在我的ACL中,客户可以有许多角色,每个角色可以有许多权限。客户/角色的映射通过acl\u customer\u role
表完成
我目前在处理这段关系时遇到了一些问题。这些是我的实体(为了简洁起见,删除了一些标准注释):
如您所见,在我的客户实体中,我定义了$roles
。这是一种多人关系,因为许多角色可能属于许多客户。我正在将联接表设置为acl\u customer\u role
,并指定需要进行联接的列。但是,我得到以下错误:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'acl_customer_role.aclrole_id' in 'on clause'' in vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:641
Doctrine的查询似乎试图加入到显然不存在的“acl\u customer\u role.aclrole\u id”上
如何正确指定关系
更新:
不知何故,条令似乎在修改我的列名。当我指定acl\u role\u id
时,原则去掉第一个下划线,并假设列名为aclrole\u id
(如上面问题中的错误消息所示)。但是,当我添加两个下划线时,例如acl\uu role\u id
它将所有下划线都保留在那里,并给出几乎相同的错误,除了现在它无法在acl\uu role\u id
上加入
我几乎不知所措。我知道这个问题很老,但最近我遇到了同样的错误/问题,我找到了解决办法 默认情况下,条令使用
DefaultNamingStrategy
类生成joinColumn、joinTableName、propertyToColumnName等名称:
...
public function joinColumnName($propertyName)
{
return $propertyName . '_' . $this->referenceColumnName();
}
这就是为什么在您的案例中会生成aclrole\u id
列的原因
要更改该行为,您只需在app/config.yml文件中将Doctrine2的命名策略更改为下划线:
doctrine:
orm:
# ...
naming_strategy: doctrine.orm.naming_strategy.underscore
下划线策略
类
...
public function joinColumnName($propertyName)
{
return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
}
这将生成:acl\u角色\u id
您还可以实施自定义命名策略:
注意:这是在版本2.3中引入的
...
public function joinColumnName($propertyName)
{
return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
}