How can I compile Propel Criteria to clear SQL? I've tried $criteria->toString(); but this is not I expected. Also I've tried ModelPeer::doSelectStmt($criteria) but it returned raw sql (required parameters substitution)
First of all, it's important to note that Propel uses PDO with prepared statements, so you're not going to get a fully "built-out" SQL statement in PHP. Using the Criteria->toString() is a good start, but as Peter mentions a lot of the work is indeed done by the BasePeer::createSelectSql() method.
Here's the most complete way (from Propel) to see what the SQL will look like (with placeholders) and the parameters that will be substituted in:
$params = array(); // This will be filled with the parameters
$sql = BasePeer::createSelectSql($criteria, $params);
print "The raw SQL: " . $sql . "\n";
print "The parameters: " . print_r($params, true) . "\n";
Note that you may get better mileage from just logging the queries at the database level. Of course, if PDO is configured (or supports) to use native db prepared statements, then you may still be seeing placeholders in the db too.
I believe this is the way
$rawSql = BasePeer::createSelectSql( $criteria, $params );
PreparedStatementCommon::replaceParams()
, which unfortunately is protected so you don't have access to it from your scripts. In short, i'm not sure you CAN do this without doing the replacing yourself, or throwing the Decorator Pattern at this problem. –
Retene We had the same problem recently. See http://groups.google.com/group/propel-development/browse_thread/thread/f56a5a8ee5db3b60
Now BasePeer::populateStmtValues() is public from propel version 1.4 onwards. This is currently in dev.
I decided to work around. Actually I needed the INSERT INTO ... SELECT. I.e - create SELECT statement by means of Criteria, further append INSERT INTO and execute.
So I asked BasePeer to create raw sql (BasePeer::createSelectSql), then appended INSERT INTO ahead. Since I need populate statement's values (:p1, :p2, etc), but method BasePeer::populateStmtValues is private (why?) I had to copy'paste that method to another place and call it.
© 2022 - 2024 — McMap. All rights reserved.
wordwrap($sql)
so really long sql can fit on the page. I was converting some Propel queries back to PDO and this worked like a charm. Thanks @Hans L. – Liew