如何使用Propel 1.6的自定义SQL获取查询行数?

时间:2012-07-13 16:34:49

标签: php sql postgresql propel symfony-2.1

我使用Propel ORM 1.6和PostgreSQL在Symfony2中有一个项目。我试图通过以下方式从查询using Custom SQL as shown in this link获取行数:

$con = Propel::getConnection(VerbNounPeer::DATABASE_NAME);
$countSql = "SELECT COUNT(*) FROM ("
               ." SELECT DISTINCT ON (fk_noun_id) *"
               ." FROM verb_noun"
               ." ORDER BY fk_noun_id, verb_noun_vote_count DESC"
               .") inner_tb";
$countSqlStmt = $con->prepare($countSql);
$countSqlStmt->execute();

以上查询工作正常。但是,如何从stmt(上面的$ countSqlStmt)对象获取行计数的整数值? 我尝试使用:

$recordsCount = $countSqlStmt[0];

但是,鉴于$ countSqlStmt是一个对象而不是一个数组,我得到“致命错误:不能使用DebugPDOStatement类型的对象作为数组......”

我还尝试使用Propel的格式化程序将其转换为数组:

$countSqlFormatter = new PropelArrayFormatter();
$countSqlRow = $countSqlFormatter->format($countSqlStmt);

但是,这不起作用,因为我需要为数组指定一个条件,而且我不知道要放什么,因为我的查询结果只是一个带有计数值而不是类的行。如果是我会使用的课程:

$formatter->setClass('VendorName\NameBundle\Model\VerbNoun');

有什么想法吗?在this link there is a use of PropelArrayFormatter()但对我来说没什么帮助......

1 个答案:

答案 0 :(得分:1)

为了从PDOStatement中获取数据,您必须在其上调用fetch方法。

相关问题