我对 Doctrine 比较陌生,但对 SQL 相当精通。我似乎无法在 Symfony 2.3 应用程序中有效地将以下 MySQL 查询转换为 Doctrine:
select
asset.id as asset_id,
orgAsset.organization_id,
districtAsset.district_id,
deptAsset.department_id,
subjectAsset.subject_id,
schoolAsset.school_id,
teacherAsset.teacher_id,
sectionAsset.section_id,
syllabusAsset.syllabus_id
from asset
left join organizations_assets orgAsset on asset.id = orgAsset.asset_id
left join districts_assets districtAsset on asset.id = districtAsset.asset_id
left join departments_assets deptAsset on asset.id = deptAsset.asset_id
left join subjects_assets subjectAsset on asset.id = subjectAsset.asset_id
left join schools_assets schoolAsset on asset.id = schoolAsset.asset_id
left join teachers_assets teacherAsset on asset.id = teacherAsset.asset_id
left join sections_assets sectionAsset on asset.id = sectionAsset.asset_id
left join syllabus_assets syllabusAsset on asset.id = syllabusAsset.asset_id
where asset.id = ?;
所有*_assets
表都是单向多对多表,其中*
对象连接到Asset
对象。
我的第一个目标是让这种查询使用 Doctrine 工作。我的最终目标是让 Doctrine 只返回与资产关联的对象。这是我的单向多对多对象映射的示例。对象中没有互惠映射Asset
:
class Organization
{
...
/**
* @ORM\ManyToMany(targetEntity="Asset")
* @ORM\JoinTable(name="organizations_assets",
* joinColumns={@ORM\JoinColumn(name="organization_id", referencedColumnName="id", nullable=true)},
* inverseJoinColumns={@ORM\JoinColumn(name="asset_id", referencedColumnName="id")}
* )
*
* @var ArrayCollection|Asset[]
*/
protected $assets;
...
}
扩展的对象Asset
过于复杂,无法实际重构为单表继承。
单向映射是问题吗?我是否需要创建一个本机查询来处理这个问题?我发现很难相信 Doctrine 本身不能处理这个问题,所以我希望我忽略了一些简单的事情。