gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:36:01 25 4
gpt4 key购买 nike

我在 Symfony2 中有一个使用 Propel ORM 1.6 和 PostgreSQL 的项目。我正在尝试从查询中获取行数 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 是一个对象而不是数组,我得到“ fatal error :无法将 DebugPDOStatement 类型的对象用作数组...”

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

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

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

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

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

最佳答案

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

关于php - 如何使用 Propel 1.6 的自定义 SQL 通过查询获取行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11474623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com