How to compile Propel criteria for SQL

How can I compile Propel criteria for SQL cleanup? I tried $ criteria-> toString (); but I did not expect this. I also tried ModelPeer :: doSelectStmt ($ criteria), but it returned raw sql (replacing the required parameters)

+6
sql php propel criteria
source share
5 answers

First of all, it is important to note that Propel uses PDO with prepared statements, so you won’t get a completely “embedded” SQL query in PHP. Using the criterion-> toString () is a good start, but, as Peter mentions, most of the work is actually done by the BasePeer :: createSelectSql () method.

Here is the most complete way (from Propel) to see how SQL (with placeholders) will look and the parameters that will be replaced:

$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"; 

Please note that you can only get the best mileage from registering queries at the database level. Of course, if PDO is configured (or supported) to use prepared native db statements, you can still see placeholders in db as well.

+12
source share

I believe this is a way

 $rawSql = BasePeer::createSelectSql( $criteria, $params ); 
+2
source share

I decided to work. Actually I need INSERT INTO ... SELECT. Ie - create a SELECT statement using criteria, then add INSERT INTO and execute.
So I asked BasePeer to create raw sql (BasePeer :: createSelectSql) and then add INSERT INTO forward. Since I need to fill in the values ​​of the operators (: p1 ,: p2, etc.), But the BasePeer :: populateStmtValues ​​method is private (why?) I had to copy this method to another place and call it.

0
source share

We have had the same problem recently. See http://groups.google.com/group/propel-development/browse_thread/thread/f56a5a8ee5db3b60

Now BasePeer :: populateStmtValues ​​() is publicly available from version 1.4 and higher. This is currently in dev.

0
source share

Even easier to try:

 print($criteria->toString()) ; 
0
source share

All Articles