依靠 Zend_Db_Select

发布于 2024-09-27 04:39:12 字数 312 浏览 3 评论 0原文

假设我有一个随机的 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 技术交流群。

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

发布评论

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

评论(5

超可爱的懒熊 2024-10-04 04:39:12

有几种方法可以指定要在 Zend_Db_Select 中获取的列。以下两个产品相同的 SQL

$select = $db->select()
             ->from('myTable', array())
             ->columns(array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));

$select = $db->select()
             ->from('myTable', array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));

from 方法采用第一个参数(表名)和第二个参数(要获取的列数组)。如果您使用表达式,则可以指定“key”=>表达式。

将 Zend_Db_Select 转换为 SQL 字符串以进行调试或与其他函数一起使用非常容易。

echo $select; // prints SELECT COUNT(*) AS `TotalRecords` FROM `myTable`

这使用了 toString 方法,该方法由 Zend_Db 获取方法自动调用:

$total = $db->fetchOne($select); 

echo $total; //prints the number of rows matching the query

其中 $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

$select = $db->select()
             ->from('myTable', array())
             ->columns(array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));

$select = $db->select()
             ->from('myTable', array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));

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.

echo $select; // prints SELECT COUNT(*) AS `TotalRecords` FROM `myTable`

This uses a toString method, which is called automatically by Zend_Db fetch methods:

$total = $db->fetchOne($select); 

echo $total; //prints the number of rows matching the query

Where $db is an instance of Zend_Db.

掀纱窥君容 2024-10-04 04:39:12

使用 $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.

爱冒险 2024-10-04 04:39:12

如果使用 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.

苏大泽ㄣ 2024-10-04 04:39:12
$select = $db->select();

$select->from(
    'table_name',
    array('cnt' => 'count(1)')
);
$select = $db->select();

$select->from(
    'table_name',
    array('cnt' => 'count(1)')
);
涙—继续流 2024-10-04 04:39:12

我刚刚遇到同样的问题,并发现

Zend_Db_Select::columns 函数需要数组而不是对象或字符串(当第一个参数是字符串或对象时,它可能会使用这是您提供的列的主表,但我对此不确定。)。

将您的代码更改为

$data->TotalRecords = $select->columns(array(new Zend_Db_Expr('COUNT(*)')))->query()->fetch();

将解决您的问题

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

$data->TotalRecords = $select->columns(array(new Zend_Db_Expr('COUNT(*)')))->query()->fetch();

Will fix your issue

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