duanpuluan0480
duanpuluan0480
2014-10-23 20:13
浏览 42
已采纳

你能用模数和Propel Criteria吗?

I want to skip 'N' rows when I pull data. Using raw SQL I can accomplish this usingWHERE 'id' %%%u=0 where 'u' is my interval. Can I accomplish this using the Criteria object? I've tried this: $c->getNewCriterion(GaugeDataPeer::ID%$interval, 0, Criteria::EQUAL); but to no avail.

This is on Symfony 1.4. Not sure what Propel version.

Here's the full query I'm trying to recreate:

$query  = sprintf("SELECT *
                          FROM `processed_gauge_data`
                          WHERE `id` %%%u=0 AND `processed_gauge_data`.`gauge_id` IN (%s) AND `processed_gauge_data`.`stamp` >= '%s' AND `processed_gauge_data`.`stamp` <= '%s';",
                  $interval,
                  implode(',', $gauge),
                  mysql_real_escape_string(date('Y-m-d H:i:s', $start)),
                  mysql_real_escape_string(date('Y-m-d H:i:s', $end))
              );
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanquezhan7268
    duanquezhan7268 2014-11-12 18:29
    已采纳

    Just use ->where

    ->where('ProcessedGaugeData.Id %%%u = 0')
    

    and better use ActiveQuery: http://propelorm.org/blog/2010/08/03/refactoring-to-propel-1-5-from-peer-classes-to-query-classes.html

    点赞 评论
  • dongpeng0127
    dongpeng0127 2014-10-24 22:29

    Found a solution!

    MY OLD CODE:

    $c = new Criteria();
                $c->add(GaugeDataPeer::GAUGE_ID, $this->getId(), Criteria::EQUAL);
    
                $start_c = $c->getNewCriterion(GaugeDataPeer::STAMP, $dates['start']->format('Y-m-d H:i:s'), Criteria::GREATER_EQUAL);
                $end_c = $c->getNewCriterion(GaugeDataPeer::STAMP, $dates['end']->format('Y-m-d H:i:s'), Criteria::LESS_EQUAL);
                $start_c->addAnd($end_c);
                $c->add($start_c);
    
                $c->addAscendingOrderByColumn(GaugeDataPeer::STAMP);
                $data_array = GaugeDataPeer::doSelect($c);
    

    MY NEW CODE

    $query  = sprintf("SELECT *
                              FROM `gauge_data`
                              WHERE `id` %%%u=0 AND `gauge_data`.`gauge_id` IN (%s) AND `gauge_data`.`stamp` >= '".$dates['start']->format('Y-m-d H:i:s')."' AND `gauge_data`.`stamp` <= '".$dates['end']->format('Y-m-d H:i:s')."';",
                      $interval,
                      implode(',', array($this->getId()))
                  );                
    
                $conn = Propel::getConnection();
                $st = $conn->prepare($query);
                $st->execute();
                $data_array = $st->fetchAll(PDO::FETCH_CLASS, GaugeData);
    
    点赞 评论

相关推荐