在 Propel 中获取聚合列的值
我正在尝试在 Propel 中进行以下查询:
$criteria = new Criteria();
$criteria->clearSelectColumns();
$criteria->addSelectColumn(TableName::COLUMN1)
->addSelectColumn(TableName::COLUMN2)
->addAsColumn('alias1', 'DATE('.TableName::INIT_TIME.')')
->addAsColumn('alias2', 'COUNT('.TableName::DOWNLOAD_START_TIME.')')
->addGroupByColumn('DATE('.TableName::INIT_TIME.')')
->addGroupByColumn(TableName::DOWNLOAD_START_TIME);
$logs = TableName::doSelect($criteria);
似乎一切正常,并且根据生成的 MySQL 日志文件查询并将其保存到服务器。但是我无法获取聚合列的值。
doSelect 返回 TableName 对象的数组,该数组没有获取聚合列的方法。那么,我该怎么做呢?
PS:如果重要的话,我正在谈论 symfony 1.4 和 Propel。
I am trying to make following query in Propel:
$criteria = new Criteria();
$criteria->clearSelectColumns();
$criteria->addSelectColumn(TableName::COLUMN1)
->addSelectColumn(TableName::COLUMN2)
->addAsColumn('alias1', 'DATE('.TableName::INIT_TIME.')')
->addAsColumn('alias2', 'COUNT('.TableName::DOWNLOAD_START_TIME.')')
->addGroupByColumn('DATE('.TableName::INIT_TIME.')')
->addGroupByColumn(TableName::DOWNLOAD_START_TIME);
$logs = TableName::doSelect($criteria);
It seems that everything OK and according to MySQL log file query generated and saved to server right. However I cannot get values of aggregated columns.
doSelect returns array of TableName objects which have no methods to fetch aggregated columns. So, how can I do it?
PS: I am talking about symfony 1.4 with Propel if it matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该避免使用
Criteria
,而是使用ModelCriteria
API(文档位于:http://www.propelorm.org/reference/model-criteria.html)以下查询:
可以重写如下:
请注意,您不会同时选择两者列
alias1
和alias2
,因为您没有将它们添加到代码中的选定列。您可以通过将两个别名添加到select()
子句来做到这一点:You should avoid the use of
Criteria
, use theModelCriteria
API instead (documentation available at: http://www.propelorm.org/reference/model-criteria.html)The following query:
Can be rewritten as below:
Note you won't select both columns
alias1
andalias2
as you didn't add them to the selected columns in your code. You can do that by adding the two alias to theselect()
clause:当我遵循相同的方法,但通过
addSelectColumn
在我的criteria
中仅选择一列时,我使用此代码片段来检索和获取数据:我希望这可以帮助您。
I used this snippet to retrieve and fetch data when I followed this same approach, but selecting only one column in my
criteria
throughaddSelectColumn
:I hope this can help you.
我的方法是进行查询并迭代记录集。正如建议的,我使用 FETCH_BOTH 来获取数组位置,但也获取名称。我获取这些值并水合我的对象,但也手动填充用于保存聚合值的对象的属性。我将这些对象传递回我的应用程序,并将它们用作我视图中使用的普通对象。
My method is to make a query and iterate through a recordset. As suggested I use FETCH_BOTH to get array positions, but names as well. I take those values and hydrate my object, but also manually populate the properties of the object that are meant to hold the aggregate values. I pass those objects back to my application and use them as normal objects would be used in my view.