I tried to use the new QueryBuilder functions in version 2021, but I must be doing something wrong. In the Server Events->Global Code, I created a function:function myFunction($email) {
$query = QueryBuilder()->select(“COUNT()")->from(“users”)->where(“email = ?”)->setParameter(0, $email);
return ExecuteScalar($query);
}However, when the function executes, I get the following error:Call to undefined method Doctrine\DBAL\Connection::getQueryBuilder()If I convert the function to building the query manually using AdjustSql(), it works fine:function myFunction($email) {
$query = "SELECT COUNT() FROM users WHERE email = '” . AdjustSql($email) . “'”;
return ExecuteScalaer($query);
}
Try:
$query = CurrentPage()->getQueryBuilder()->select("COUNT(*)")->from("users")->where("email = ?")->setParameter(0, $email);
bkonia wrote:
$query = QueryBuilder()->select(“COUNT(*)”)->from(“users”)->where(“email = ?”)->setParameter(0, $email);
The result $query is not a SQL, it is still a QueryBuilder, you need get the SQL by getSQL() or you may execute the SQL directly by execute(). See:
https://github.com/doctrine/dbal/blob/2.12.x/lib/Doctrine/DBAL/Query/QueryBuilder.php
I modified the code to use getSQL() to convert the QueryBuilder to a string, but it’s still returning the same error message. The error occurs on line 5780 of phpfn.php:
return $conn->getQueryBuilder();
which suggests it’s failing when it attempts to create the QueryBuilder object, not when subsequently calling ExecuteScalar().
You may use this instead:
$qb = Conn()->createQueryBuilder();
Try QueryBuilder() again with v2017.0.7.
Yeah, it works if you do this:
$count = QueryBuilder()->select("COUNT(*)")->from("users")->where("email = ?")->setParameter(0, $email)->execute()->fetchOne();