标准双向连接?

发布于 2024-12-19 14:55:26 字数 1047 浏览 1 评论 0原文

我正在开发一个 symfony1.4 项目,使用 propel ORM。在我的模型中,我有一个表,其中的元素可以通过另一个表(链接表)与同一个表的许多元素链接。一些代码会更好地解释:

table1:
    id_element: integer;
    [...]

和链接表:

 link_table:
     id1: fk to table1;
     id2: fk to table1;

我需要使用 Propel Criteria 构建一个查询,它返回与特定元素相关的所有元素。问题是我想要指定的元素可以在链接表的 id1 字段中,也可以在 id2 字段中。

现在我的一些标准定义代码(显然不起作用)

$c = new Criteria();
$c->addJoin($linkTable::ID1,$table::ID);
$c->addJoin($linkTable::ID2,$table::ID);
$c->addOr($linkTable::ID1,$specific_id);
$c->addOr($linkTable::ID2,$specific_id);
$result = $table->doSelect($c);

,这是一个像我想要生成的 SQL:

SELECT * FROM table
    WHERE table.ID IN
        (SELECT link_table.ID1 FROM link_table
            WHERE link_table.ID2 = "the id that I want"
        )
    OR table.ID IN 
        (SELECT link_table.ID2 FROM link_table
            WHERE link_table.ID1 = "the id that I want"
        )

那么我必须执行 2 个连接,链接表的每一侧一个连接吗?有没有办法进行“or-join”?

I'm working on a symfony1.4 project, with propel ORM. In my model, I have a table where its elements can be linked with many elements of the same table, by another table (link table). Some code will explain better:

table1:
    id_element: integer;
    [...]

and the link table:

 link_table:
     id1: fk to table1;
     id2: fk to table1;

I need to build a query with Propel Criteria that returns me all the related elements with an specific element. The problem is that the element I want to specify, can be as in id1 field as in id2 field of the link table.

now some of my criteria definition code (not working obviously)

$c = new Criteria();
$c->addJoin($linkTable::ID1,$table::ID);
$c->addJoin($linkTable::ID2,$table::ID);
$c->addOr($linkTable::ID1,$specific_id);
$c->addOr($linkTable::ID2,$specific_id);
$result = $table->doSelect($c);

and this is a SQL like that I want to generate:

SELECT * FROM table
    WHERE table.ID IN
        (SELECT link_table.ID1 FROM link_table
            WHERE link_table.ID2 = "the id that I want"
        )
    OR table.ID IN 
        (SELECT link_table.ID2 FROM link_table
            WHERE link_table.ID1 = "the id that I want"
        )

So must I do 2 joins, one for each side of the link table? is there a way to do an "or-join"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

[旋木] 2024-12-26 14:55:26

也许我不太明白你需要什么......你有一个表的两个外键,它们都链接到同一个表,所以id,你想要一个或连接。为什么不分成两个标准,从而两个选择呢?

Maybe I don't well understand what you need... you have two foreign keys of a table that are both linked to the same table, and so id, and you want an or-join. Why not split in two criteria and so two selections?

云胡 2024-12-26 14:55:26

只是大声思考 - 这看起来在功能上与此相同:

SELECT
    *
FROM
    my_table
LEFT JOIN
    link_table lt1 ON (lt1.id1 = my_table.id)
LEFT JOIN
    link_table lt2 ON (lt2.id2 = my_table.id)
WHERE
    lt1.id2 = X
OR
    lt2.id1 = X

如果相同(显然要检查),那么构建查询应该更容易。另外,如果可以的话,尝试使用 ModelCriteria - 我怀疑 Criteria 将在 Propel 2 中被弃用,为此提前计划是个好主意。

Just thinking out aloud - that looks functionally identical to this:

SELECT
    *
FROM
    my_table
LEFT JOIN
    link_table lt1 ON (lt1.id1 = my_table.id)
LEFT JOIN
    link_table lt2 ON (lt2.id2 = my_table.id)
WHERE
    lt1.id2 = X
OR
    lt2.id1 = X

If that is identical (obviously do check) then that should be much easier to build a query for. Also, try to use ModelCriteria if you can - I suspect Criteria will be deprecated in Propel 2, and it is a good idea to plan ahead for that.

铁轨上的流浪者 2024-12-26 14:55:26

避免使用 Criteria,正如 halfer 所说,最好使用 ModelCriteria 及其 API。阅读以下文档了解更多信息:http://www.propelorm.org/reference/model -criteria.html

Avoid the use of Criteria, as halfer said, it's better to use the ModelCriteria and its API. Read the following doc for more information: http://www.propelorm.org/reference/model-criteria.html

各自安好 2024-12-26 14:55:26

最后我找到了一种使用标准来做到这一点的方法:

$c = new Criteria();
    $q1 = new Criteria();
    $q1->add($linkPeer::ID1,$my_value);
    result1 = $linkPeer->doSelect($q1);
    foreach($result1 as $result){
        ids1[] = $result->getID();
    }
    $q2 = new Criteria();
    $q2->add($linkPeer::ID2,$my_value);
    result2 = $linkPeer->doSelect($q2);
    foreach($result2 as $result){
        ids2[] = $result->getID();
    }
$ids = array_merge($ids1,$ids2);
$c->add($tablePeer::ID,$ids,Criteria::IN);
$totalResult = $tablePeer->doSelect($c);

也许不是最好的方法,但工作正常。

非常感谢您的解答!!

at last i found a way to do it using criteria:

$c = new Criteria();
    $q1 = new Criteria();
    $q1->add($linkPeer::ID1,$my_value);
    result1 = $linkPeer->doSelect($q1);
    foreach($result1 as $result){
        ids1[] = $result->getID();
    }
    $q2 = new Criteria();
    $q2->add($linkPeer::ID2,$my_value);
    result2 = $linkPeer->doSelect($q2);
    foreach($result2 as $result){
        ids2[] = $result->getID();
    }
$ids = array_merge($ids1,$ids2);
$c->add($tablePeer::ID,$ids,Criteria::IN);
$totalResult = $tablePeer->doSelect($c);

maybe not the best way but working fine.

Thank you very much for your answers!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文