标准双向连接?
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许我不太明白你需要什么......你有一个表的两个外键,它们都链接到同一个表,所以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?
只是大声思考 - 这看起来在功能上与此相同:
如果相同(显然要检查),那么构建查询应该更容易。另外,如果可以的话,尝试使用 ModelCriteria - 我怀疑 Criteria 将在 Propel 2 中被弃用,为此提前计划是个好主意。
Just thinking out aloud - that looks functionally identical to this:
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.
避免使用
Criteria
,正如 halfer 所说,最好使用ModelCriteria
及其 API。阅读以下文档了解更多信息:http://www.propelorm.org/reference/model -criteria.htmlAvoid the use of
Criteria
, as halfer said, it's better to use theModelCriteria
and its API. Read the following doc for more information: http://www.propelorm.org/reference/model-criteria.html最后我找到了一种使用标准来做到这一点的方法:
也许不是最好的方法,但工作正常。
非常感谢您的解答!!
at last i found a way to do it using criteria:
maybe not the best way but working fine.
Thank you very much for your answers!!