如何通过DQL选择属性的特定属性

时间:2016-10-21 12:18:32

标签: symfony doctrine-orm dql doctrine-query

我有以下实体。首要的是:设计源自遗留数据库,为了清晰起见,它已在此处进行了简化。

enter image description here

我想要做的是选择所有widgets及其填充的varsSelection(此时Widget仅包含PhyVarSelection个实例,以便我们专注于他们应该有teir phyVar水合物。不得使用Phyvar属性填充ewCfgVar

我试图通过使用DQL来实现。我成功选择了所有小部件及其相对varsSelection,但我无法填充其相对phyVar。那可能吗?以下是我使用的DQL:

        $sql = <<<EOS
        SELECT wid, partial phyvarsel.{id, phyVar, start}
        FROM Belka\\TsBundle\\Entity\\Widget wid
        LEFT JOIN Belka\\TsBundle\\Entity\\PhyVarSelection phyvarsel WITH wid.id = phyvarsel.widget
        LEFT JOIN Belka\\TsBundle\\Entity\\PhyVar phyvar WITH phyvarsel.phyVar = phyvar.id
EOS;

        $query = $this->getEntityManager()->createQuery($sql);

如果我转储结果,WidgetvarsSelection已正确填充,但PhyVarSelection::phyVar设置为NULL。

它生成的查询DQL实际上是正确的,如果我查询它,我会获得所有PhyVar的属性:

die(var_dump($query->getSQL()));

它生成:

SELECT a0_.id AS id0, a0_.title AS title1, a0_."order" AS order2, a0_.span_cols AS span_cols3, a0_.description AS description4, a1_.id AS id5, a1_.start AS start6, a2_.id AS id7, a0_.part_of_section AS part_of_section8, a1_.vartype AS vartype9, a1_.part_of_widget AS part_of_widget10, a1_.phy_var_sel AS phy_var_sel11, a2_.vartype AS vartype12, a2_.part_of_phy_meter AS part_of_phy_meter13, a2_.varname AS varname14, a2_.id_device AS id_device15 FROM app_t.widget a0_ LEFT JOIN app_t.var_selection a1_ ON (a0_.id = a1_.part_of_widget) AND a1_.vartype IN ('phy') LEFT JOIN app_t.variable a2_ ON (a1_.phy_var_sel = a2_.id) AND a2_.vartype IN ('phy');

我不会粘贴Entities&#39;代码,但如果您需要,请通过以下评论告诉我,我会更新问题。

更新

我还尝试过以下方法:

        $sql2 = <<<EOS
        SELECT phyvarsel, phyvar
        FROM Belka\\TsBundle\\Entity\\PhyVarSelection phyvarsel
        LEFT JOIN Belka\\TsBundle\\Entity\\PhyVar phyvar
        WHERE phyvarsel.id = :phyvarselid
EOS;


        $query2 = $this->getEntityManager()->createQuery($sql2);
        $query2->setParameter('phyvarselid', '0');
        $query2->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
        $varSel = $query2->getResult();

再次,在这种情况下,我得到PhyVarSelection,但其属性phyVar仍然设置为NULL。

0 个答案:

没有答案