« Previous - Version 4/6 (diff) - Next » - Current version
wesen 3000, 2011-08-15 03:08 AM


SQLBuilder

This guide will show you how to use the php-activerecord SQL builder.

The first steps are to get the database connection and setup the SQL builder.

1 $conn = ActiveRecord\ConnectionManager::get_connection("development");                                                                                                                         
2 $builder = new ActiveRecord\SQLBuilder($conn, "authors"); 

SELECT queries
We are now ready to generate a simple SELECT query:

1 $builder->where("name = ?", "Hemingway");
2 echo $builder; /* => SELECT * FROM authors WHERE name = ? */
3 print_r($builder->get_where_values()); /* => array("Hemingway") */

You can also pass a hash to the where() method:

1 $builder = new ActiveRecord\SQLBuilder($conn, "authors");                                                                                                            
2 $builder->where(array("name" => "Hemingway",                                                                                                                         
3                       "country" => "USA"));
4 echo $builder; /* => SELECT * FROM authors WHERE `name`=? AND `country`=? */
5 print_r($builder->get_where_values()); /* => array('Hemingway', 'USA'); */
6 

Also available in: HTML TXT