在 Propel 中获取聚合列的值

发布于 2024-12-19 07:26:13 字数 683 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(3

云雾 2024-12-26 07:26:13

您应该避免使用 Criteria,而是使用 ModelCriteria API(文档位于:http://www.propelorm.org/reference/model-criteria.html)

以下查询:

<?php

$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);

可以重写如下:

$query = TableNameQuery::create()
    ->select(array('Column1', 'Column2'))
    ->withColumn('DATE(InitTime)', 'alias1')
    ->withColumn('COUNT(DownloadStartTime)', 'alias2')
    ->groupBy('alias1')
    ->groupByDownloadStartTime()
    ;
$logs = $query->find();

请注意,您不会同时选择两者列alias1alias2,因为您没有将它们添加到代码中的选定列。您可以通过将两个别名添加到 select() 子句来做到这一点:

    ->select(array('Column1', 'Column2', 'alias1', 'alias2'))

You should avoid the use of Criteria, use the ModelCriteria API instead (documentation available at: http://www.propelorm.org/reference/model-criteria.html)

The following query:

<?php

$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);

Can be rewritten as below:

$query = TableNameQuery::create()
    ->select(array('Column1', 'Column2'))
    ->withColumn('DATE(InitTime)', 'alias1')
    ->withColumn('COUNT(DownloadStartTime)', 'alias2')
    ->groupBy('alias1')
    ->groupByDownloadStartTime()
    ;
$logs = $query->find();

Note you won't select both columns alias1 and alias2 as you didn't add them to the selected columns in your code. You can do that by adding the two alias to the select() clause:

    ->select(array('Column1', 'Column2', 'alias1', 'alias2'))
要走干脆点 2024-12-26 07:26:13

当我遵循相同的方法,但通过 addSelectColumn 在我的 criteria 中仅选择一列时,我使用此代码片段来检索和获取数据:

$logs = TableName::doSelectStmt($criteria); 
$data = $logs->fetchAll(PDO::FETCH_COLUMN, 0);

我希望这可以帮助您。

I used this snippet to retrieve and fetch data when I followed this same approach, but selecting only one column in my criteria through addSelectColumn:

$logs = TableName::doSelectStmt($criteria); 
$data = $logs->fetchAll(PDO::FETCH_COLUMN, 0);

I hope this can help you.

装纯掩盖桑 2024-12-26 07:26:13

我的方法是进行查询并迭代记录集。正如建议的,我使用 FETCH_BOTH 来获取数组位置,但也获取名称。我获取这些值并水合我的对象,但也手动填充用于保存聚合值的对象的属性。我将这些对象传递回我的应用程序,并将它们用作我视图中使用的普通对象。

        $stmt = self::doSelectStmt($c);

        // empty array for now
        $results = array();
        // lets iterate through this
        while($row = $stmt->fetch(PDO::FETCH_BOTH)) {
           // we need to create a new object
           $division = new Division();
           // and we'll hydrate each row turning into an object so we can use it easily
           $division->hydrate($row);
           // now here's the magic, this object did not exist from our tables, we'll fix that
           $division->setNumPlayers($row['num_players']);
           $division->setNumTeams($row['teams']);
           // set the output object
           $results[] = $division;
           } 
return $results; // array of objects including aggregate values

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.

        $stmt = self::doSelectStmt($c);

        // empty array for now
        $results = array();
        // lets iterate through this
        while($row = $stmt->fetch(PDO::FETCH_BOTH)) {
           // we need to create a new object
           $division = new Division();
           // and we'll hydrate each row turning into an object so we can use it easily
           $division->hydrate($row);
           // now here's the magic, this object did not exist from our tables, we'll fix that
           $division->setNumPlayers($row['num_players']);
           $division->setNumTeams($row['teams']);
           // set the output object
           $results[] = $division;
           } 
return $results; // array of objects including aggregate values
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文