find('count') 和 find('all') cakePHP 之间的行为差异
我在 CakePHP 中遇到一个奇怪的问题,它
$this->something->find('count');
工作得很好,但
$this->something->find('all');
没有返回任何内容(甚至没有空数组、任何错误或任何内容)。
编辑:结果我收到一个 sql 错误:“SQL 错误:1054:未知列” - 对于确实存在的列。 (下面的sql查询中的users.display_name):
SELECT item.id, item.name, item.description, item.user_id, users.display_name FROM item LEFT JOIN users ON (item.user_id = users.id);
我还尝试使用findAllBy以及paginate(paginate实际上是我想要做的 - 尽管从我收集的数据来看,paginate和find('all')非常相似在功能上)。
奇怪的是 find('all') 在其他地方都有效 - 只是在这个特定的控制器中它表现得很奇怪。我没有收到任何错误,只是一个空结果。
我认为我可能忽略了一些非常简单的事情,但我们将不胜感激。谢谢!
I am having an odd problem in CakePHP where
$this->something->find('count');
works perfectly, yet
$this->something->find('all');
returns nothing (not even an empty array, any errors, or anything).
edit: turns out I am getting an sql error: "SQL Error: 1054: Unknown column" - for a column that does indeed exist. (users.display_name in the sql query below):
SELECT item.id, item.name, item.description, item.user_id, users.display_name FROM item LEFT JOIN users ON (item.user_id = users.id);
I also tried using findAllBy as well as paginate (paginate is actually what I am trying to do - although from what I've gathered, paginate and find('all') are pretty similar in functionality).
The odd thing is that find('all') works everywhere else - it's just in this specific controller that it is acting odd. I am not getting any errors, simply an empty result.
I'm thinking that I may be overlooking something quite simple, but any help is appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,根据我们的讨论,您遇到的问题在于虚拟字段。查看文档,更具体地在虚拟字段和模型别名和virtualFields 的限制 部分。
从上面的描述来看,您似乎有一个指定了虚拟字段的连接,这会导致您看到的错误,因为它会在
FROM
之前添加JOIN
。如果您坚持使用虚拟字段,我建议您重写它以使用子查询。确保您的子查询仅返回 1 列。示例: (http://web-development-blog.co.uk/2011/03/08/cakephp-virtual-field-count-another-modeltable/)
或者,您可以使用
Model::beforeFind
进行绑定必要的模型(如果需要)并更改查询参数。如果您无法弄清楚,请发布您的模型,我会帮助您。
So, as per our discussion, the problem you're having is with the virtual fields. Have a look at the documentation, more specifically at the virtual fields and model aliases and limitation of virtualFields sections.
From your description above, it looks like you have a join specified your virtual field which would be causing the error you're seeing because it'll add the
JOIN
before theFROM
. If you insist on using the virtual field, I'd suggest you rewrite it to use a subquery. Make sure your subquery only returns 1 column.Example: (http://web-development-blog.co.uk/2011/03/08/cakephp-virtual-field-count-another-modeltable/)
Alternatively, you can use the
Model::beforeFind
to bind the necessary models (if necessary) and change the query parameters.If you can't figure it out, please post your model and I'll help you.
您遇到的行为差异的具体问题是
find('count')
将对数据库运行基本的COUNT(*)
查询以确定数量行数。但是,
find('all')
运行不同的查询,如果您提供的 SQL 正是它尝试使用的 SQL,则它是无效的:没有
FROM
声明(SELECT
到底是什么?),如果您没有自定义您的Item
或User
或App
模型,(设置$useTable = false
也许?)您正在处理一个不寻常的错误。如果这些模型在其他控制器中正常工作,需要注意的一件事是每个控制器中每个模型的属性的任何更改。它们不会在每个控制器上有不同的行为,除非每个控制器中有代码告诉它这样做。
The specific problem you're having with the difference in behaviour is that
find('count')
will run a basicCOUNT(*)
query on your database to determine the number of rows.find('all')
, however, runs a different query, and if the SQL you've provided is what it's trying to use, it's invalid:There's no
FROM
declaration (SELECT
from what, exactly?), and if you've not customised yourItem
orUser
orApp
models, (setting$useTable = false
maybe?) you're dealing with an unusual error.One thing to look out for, if these models work fine in other controllers, is any alteration to the properties of each model in each controller. They won't act differently on a per-controller basis unless you have code in each controller that tells it to.