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 

You can add ordering information:

1 $builder = new ActiveRecord\SQLBuilder($conn, "authors");
2 $builder->order('id DESC');
3 echo $builder."\n"; /* => SELECT * FROM authors ORDER BY id DESC */

Cool

Also available in: HTML TXT