SQLBuilder

Version 3 (wesen 3000, 2011-08-15 03:07 AM)

1 1
h2. SQLBuilder
2 1
3 1
This guide will show you how to use the php-activerecord SQL builder.
4 1
5 1
The first steps are to get the database connection and setup the SQL builder.
6 1
7 1
<pre class="code"><code class="php">
8 3 wesen 3000
$conn = ActiveRecord\ConnectionManager::get_connection("development");                                                                                                                         
9 1
$builder = new ActiveRecord\SQLBuilder($conn, "authors"); 
10 2 wesen 3000
</code></pre>
11 1
12 2 wesen 3000
We are now ready to generate a simple SELECT query:
13 2 wesen 3000
14 2 wesen 3000
<pre class="code"><code class="php">
15 1
$builder->where("name = ?", "Hemingway");
16 1
echo $builder; /* => SELECT * FROM authors WHERE name = ? */
17 1
print_r($builder->get_where_values()); /* => array("Hemingway") */
18 3 wesen 3000
</code></pre>
19 3 wesen 3000
20 3 wesen 3000
You can also pass a hash to the where() method:
21 3 wesen 3000
22 3 wesen 3000
<pre class="code"><code class="php">
23 3 wesen 3000
$builder = new ActiveRecord\SQLBuilder($conn, "authors");                                                                                                            
24 3 wesen 3000
$builder->where(array("name" => "Hemingway",                                                                                                                         
25 3 wesen 3000
                      "country" => "USA"));
26 3 wesen 3000
echo $builder; /* => SELECT * FROM authors WHERE `name`=? AND `country`=? */
27 3 wesen 3000
print_r($builder->get_where_values()); /* => array('Hemingway', 'USA'); */
28 3 wesen 3000
29 2 wesen 3000
</code></pre>