依靠 Zend_Db_Select
假设我有一个随机的 zend_db_select 对象。
如何对该对象执行计数,以便我知道满足查询的项目数量。
我尝试了以下操作:
$data->TotalRecords = $select->columns(new Zend_Db_Expr('COUNT(*)'))->query()->fetch();
但这给了我以下错误:
消息:没有为 FROM 子句指定表
查询本身工作正常并返回结果集。
Say I have a random zend_db_select object.
How can I perform a count on that object, so I know the amount of items that meet the query.
I tried the following:
$data->TotalRecords = $select->columns(new Zend_Db_Expr('COUNT(*)'))->query()->fetch();
But this gives me the following error:
Message: No table has been specifiedfor the FROM clause
The query by itself works fine and returns a resultset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有几种方法可以指定要在 Zend_Db_Select 中获取的列。以下两个产品相同的 SQL
from 方法采用第一个参数(表名)和第二个参数(要获取的列数组)。如果您使用表达式,则可以指定“key”=>表达式。
将 Zend_Db_Select 转换为 SQL 字符串以进行调试或与其他函数一起使用非常容易。
这使用了 toString 方法,该方法由 Zend_Db 获取方法自动调用:
其中 $db 是 Zend_Db 的实例。
There's a couple of ways of specifying the columns to fetch in a Zend_Db_Select. The following two product the same SQL
The from method takes a first argument, the table name, and a second argument, an array of columns to fetch. If you're using an expression, you can specify a 'key' => Expr.
It's really easy to convert a Zend_Db_Select into a SQL string for debugging or use with other functions.
This uses a toString method, which is called automatically by Zend_Db fetch methods:
Where $db is an instance of Zend_Db.
使用
$select->__toString()
方法输出生成的查询并查看它有什么问题。如果您的查询中没有 from 子句,请将
From()
方法添加到您的选择对象中。Use
$select->__toString()
method to output your generated query and see what is wrong with it.If u dont have a from clause in your query add
From()
method to your select object.如果使用 Zend_Db_Select,则必须调用 from 方法来设置表名。对于 Zend_Db_Table_Select,表将在构造函数中传递,因此您不需要调用 from。
If you use Zend_Db_Select, you have to call the from method to set the table name. With a Zend_Db_Table_Select, the table is passed in the constructor, so you don't need to call from.
我刚刚遇到同样的问题,并发现
Zend_Db_Select::columns
函数需要数组而不是对象或字符串(当第一个参数是字符串或对象时,它可能会使用这是您提供的列的主表,但我对此不确定。)。将您的代码更改为
将解决您的问题
I just encountered the same issue and found out what is going wrong
the
Zend_Db_Select::columns
functions expects an Array instead of a Object or String (when the first parameter is an String or Object it'll probably use this as main table for the columns you give but Im not sure about that.).Changing your code to
Will fix your issue