Php 条令/符号-同一模型上的多个一对多关系
这是我实际拥有的模式的摘录Php 条令/符号-同一模型上的多个一对多关系,php,doctrine,one-to-many,Php,Doctrine,One To Many,这是我实际拥有的模式的摘录 Software: columns: title: type: string(255) id_publisher: type: integer id_developper: type: integer Company: columns: name: type: string(255) nationality: type: string(255) 如您所见,我
Software:
columns:
title:
type: string(255)
id_publisher:
type: integer
id_developper:
type: integer
Company:
columns:
name:
type: string(255)
nationality:
type: string(255)
如您所见,我的软件模型有两个外部引用:publisher和developer。我希望为这两个引用中的每一个创建一对多关系。问题是他们都是公司
我首先在我的软件模型上尝试了如下所示的方法,但这种关系只适用于第一个本地参考id_发布者
relations:
Company:
type: one
foreignType: many
local: [id_publisher, id_developper]
foreign: id
然后我尝试了(始终在软件模型上):
但当我执行一个查询,计算与一家公司的软链接数量时
public function findAllQuery(Doctrine_Query $q = null) {
$q = Doctrine_Query::create()
->select('c.*, COUNT(s.id) AS count_software')
->from('Company c')
->leftJoin('c.Software s')
->groupBy('c.id');
return $q;
}
…在COUNT子句中只考虑发布者
最后,我的问题是,如何处理同一模型的多个一对多关系??
谢谢你的时间 也许您应该尝试添加一个外来别名,以告知在启动查询时要处理的关系:
relations:
Publisher:
class: Company
type: one
foreignType: many
foreignAlias: PublishedSoftware
local: id_publisher
foreign: id
Developer:
class: Company
type: one
foreignType: many
foreignAlias: DevelopedSoftware
local: id_developer
foreign: id
在您的查询中,您必须将两个关系合并,并将各个计数相加:
$q = Doctrine_Query::create()
->select('c.*, COUNT(ps.id)+COUNT(ds.id) AS count_software')
->from('Company c')
->leftJoin('c.PublishedSoftware ps')
->leftJoin('c.DevelopedSoftware ds')
->groupBy('c.id')
;
默认情况下,条令将使用模型名称作为关系的标识符,因此,如果对同一个模型使用多个关系,那么您确实应该至少重命名一个,以让条令了解您的意思。如果没有此功能,您将无法检索已发布软件的集合,如下所示:
$pubSoftware = $myCompany->getPublishedSoftware();
$devSoftware = $myCompany->getDevelopedSoftware();
教条不能够(IMHO)将两种关系视为同一个模型。因此,我呼吁:
$allSoftware = $myCompany->getSoftware();
不会检索多关系模型上的所有相关软件,但只检索那些可通过名为软件的关系检索的软件
希望有帮助
~~~~干杯
$allSoftware = $myCompany->getSoftware();