Kohana 3 ORM 中的错误?
很抱歉问有关 Kohana 的所有问题。他们通常会被忽视。我想我刚刚发现了一个错误。我正在两个不直接相关的表之间进行联接。
$results = ORM::factory('foo')->join("bar")->on("foo.foreign_id", "=", "bar.id");
这会生成一个不会显式解析表名称的查询:
SELECT * FROM `foo` JOIN `bar` ON (`foo`.`foreign_id` = `bar`.`id`)
给出(在 phpMyAdmin 中)一个如下所示的表:
id time foreign_id blah_int id baz
4 1291851245 3 0 3 52501504
注意有两个 id
列,其中一个用于 foo
表和一个栏
。这是一个真正的问题。因为现在,在我的结果中,如果我循环...
foreach ($results as $result) {
echo $result->id; // prints 3!!!
}
因为我的结果应该是 foo 对象,所以我期望得到 4 的 id,但由于连接,它给了我 3。这是 ORM 库中的错误吗?我应该使用不同的方法来限制查询结果吗?我真的不想做两个单独的查询,在其中加载所有 bar
的 id,然后以这种方式加载我的 foo
,但看起来我必须这样做。
Sorry to ask all these questions about Kohana. They usually get ignored. I think I just found a bug. I'm making a join between two tables that are not directly related.
$results = ORM::factory('foo')->join("bar")->on("foo.foreign_id", "=", "bar.id");
This generates a query that does not resolve the table names explicitly:
SELECT * FROM `foo` JOIN `bar` ON (`foo`.`foreign_id` = `bar`.`id`)
Which gives (in phpMyAdmin) a table that looks like this:
id time foreign_id blah_int id baz
4 1291851245 3 0 3 52501504
Notice there are two id
columns, one for the foo
table and one for bar
. This is a real problem. Because now, in my results, if I loop through...
foreach ($results as $result) {
echo $result->id; // prints 3!!!
}
Because my results should be foo
objects, I expect to get an id of 4, but it's giving me 3 because of the join. Is this a bug in the ORM library? Should I be using a different method to restrict my results from the query? I really don't want to do two separate queries where I load all the bar
s id's, and then load my foo
s that way, but it looks like I have to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用 Database 对象来构建原始查询,而不是 ORM,如下所示:
您将需要指定一些列别名,但是要使查询正常工作,除非您按预期使用 ORM。
使用 ORM
如果您想使用 ORM,您需要在模型中定义关系。您提到它们与另一个表共享关系,因此在您的情况下,您可以使用这样的 has many through 关系:
尽管给定的示例表明直接的 has_many 关系可以工作:
这将允许您使用以下方式访问所有栏 如果您想学习,像 Kohana 3.1 ORM 参考指南这样的声明
是一个很好的起点有关 ORM 和关系的更多信息
使用 Kohana 数据库对象和查询构建器
如果您更喜欢即席查询并使用查询构建器进行联接,那么无论您使用 Kohana 还是只是原始的,您都可能会遇到列名称冲突查询 (pop "SELECT * FROM
foo
JOINbar
ON (foo
.foreign_id
=bar.
id
)” 到 MySQL 中,您将得到完全相同的结果)。Kohana,就像 MySQL 一样,正是出于这个原因允许您定义列别名。 (请参阅此处了解更多信息)
重写您的查询如下:
这将返回:
You have to use the Database object to build raw queries, not ORM, like this:
You will need to specific some column aliases however to make your query work unless you use ORM as it was intended.
Using ORM
If you want to use ORM, you need to define the relationships in your model. You mention that they share a relationship with another table so in your case you could use a has many through relationship like this:
Although your example as given suggests that a straight has_many relationship would work:
This would allow you to access all of the bars using a statement like
The Kohana 3.1 ORM Reference Guide is good place to start if you want to learn more about ORM and relationships
Using the Kohana database object and query builder
If you prefer ad hoc queries and are doing joins using the query builder you will likely have colliding column names regardless if you are using Kohana or just raw queries (pop "SELECT * FROM
foo
JOINbar
ON (foo
.foreign_id
=bar
.id
)" into MySQL and you will get the exact same result).Kohana, just like MySQL allows you to define column aliases for precisely this reason. (See here for more information)
Rewrite your query as follows:
This will return: