使用Propel ORM获取按条件分组的联接结果


Get join results grouped by condition with Propel ORM

我正在尝试用一些关节数据填充一些推进对象。

问题:

我有两个表,称它们为表a和表b,表a是表b的父表,关系是一对多,其中一个父表可以有多个子表。

我想对children表执行查询,获取它的父表,并按父表id对结果进行分组,这样以后我就可以用html将每个children打印到它的父选项卡中。

这是我的模式

<table name="countries">
    <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" />
    <column name="name" type="VARCHAR" size="128" required="true" />
    <column name="country_code" phpName="CountryCode" type="VARCHAR" size="7" required="true" />
    <column name="status" phpName="Status" type="BOOLEAN" required="true" defaultValue="false" />
    <behavior name="timestampable" />
    <behavior name="soft_delete" />
</table>
<table name="banks">
    <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" />
    <column name="name" phpName="Name" type="VARCHAR" size="128" required="true" />
    <column name="status" phpName="Status" type="BOOLEAN" required="true" defaultValue="false" />
    <column name="country_id" phpName="CountryId" type="INTEGER" required="true" />
    <foreign-key foreignTable="countries" phpName="Countries" onDelete="setnull" onUpdate="cascade"><!-- country parent -->
        <reference local="country_id" foreign="id" />
    </foreign-key>
    <behavior name="timestampable" />
    <behavior name="soft_delete" />
</table>

我一直在读很多书,但运气不好,老实说,我没有做太多的测试,因为我似乎无法摆脱困境。

<?php
public function banks(Application $app)
{
    $banks = BanksQuery::create()
        ->where('banks.status = ?', true)
        ->find();
    $nav_options = $this->getNav($app);
    $admin_level = false;
    return $app['twig']->render('admin/banks.twig', array
        (
            'admin_level' => $admin_level,
            'nav_options' => $nav_options,
            'banks' => $banks
            )
        );
}

还尝试了其他方法来构建查询,没有骰子。

<?php
$banks = BanksQuery::create()
        ->orderByCountryId()
        ->joinWithCountries('banks.country_id')
        ->where('banks.status = ?', true)
        ->find();

整个想法是让每个孩子都按每个父母分组,所以如果我有3个父母,每个父母都有1到N个孩子,我可以用html打印,每个父母作为一个选项卡,其孩子作为每个选项卡的内容。

如果我要查询每个父项,使用useCountriesQuery()->filterById(ID-HERE)是可行的,但我希望在一次扫描中进行此操作。

有办法做我想做的事吗?

ok我找到了一个不同的查询解决方案。leftJoinWithBanks('Banks.CountryId')->orderById()->find();通过查询加入银行的国家,我得到了我需要的信息,所以这很有效。