当前位置:  开发笔记 > 后端 > 正文

Symfony Propel标准

如何解决《SymfonyPropel标准》经验,为你挑选了1个好方法。

有没有可能的方法将MySQL对象转换为条件对象?我试过这个查询:

select 
  p.disrepid, 
  p.subject, p.body, 
  c.disrepid as disrepid1, 
  c.subject as subject1, 
  c.body as body1 
from discusreply as p, discusreply as c 
where p.distopid=' . $this->id . ' 
  and (c.disrepid = p.parentid or c.parentid = p.distopid) 
order by p.disrepid ASC

我尝试了很多将这个查询转换为a Criteria,但没有任何反应.我想要这个标准对象将它传递给Pager类以完成分页. $pager->setCriteria($c);.



1> stereoscott..:

您可以使用自己的SQL执行查询,但没有自动方法将sql转换为Criteria对象.

$con = Propel::getConnection(DATABASE_NAME);
$sql = "SELECT books.* FROM books 
    WHERE NOT EXISTS (SELECT id FROM review WHERE book_id = book.id)";
$stmt = $con->createStatement();
$rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);
$books = BookPeer::populateObjects($rs);

这会将Criterion对象全部绕过.您提到需要一个条件对象,以便将其提供给寻呼机.您可以在寻呼机中设置自定义选择方法,然后执行自定义查询.如果你需要将参数传递给它,我建议使用你自己的pager类扩展sfPropel,该类可以选择将参数传递给你的peer select方法,这样你就不必使用Criteria对象了.作为一种快速替代方案,您可以使用Criteria作为选择参数的容器来执行此类操作:

$c = new Criteria();
$c->add(DiscussreplyPeer::ID, $myId);
$pager = new sfPropelPager();
$pager->setCriteria($c);
$pager->setPeerMethod('getReplies');

然后在你的同伴班:

public static function getReplies(Criteria $c) {
    $map = $c->getMap();
    $replyId = $map[DiscussreplyPeer::ID]->getValue();

    $con = Propel::getConnection(DATABASE_NAME);
    $sql = "select p.disrepid, p.subject, p.body, c.disrepid as disrepid1, c.subject as subject1, c.body as body1 from discusreply as p, discusreply as c where p.distopid=? and (c.disrepid = p.parentid or c.parentid = p.distopid) order by p.disrepid ASC";

    $stmt = $con->prepareStatement($sql);
    $stmt->setString(1, $replyId);

    $rs = $stmt->executeQuery();

    $results = array();
    while ($rs->next()) {
      // for example
        $results['disrepid'] = $rs->getInt('disrepid');
    }

    return $results;
}

有关推进和symfony的更多提示,请访问:http: //stereointeractive.com/blog/2007/06/12/propel-queries-using-custom-sql-peer-classes-and-criterion-objects/

推荐阅读
linjiabin43
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有