SQLBuilder

Version 4 (wesen 3000, 2011-08-15 03:08 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 4 wesen 3000
h4(#select). SELECT queries
13 2 wesen 3000
We are now ready to generate a simple SELECT query:
14 2 wesen 3000
15 2 wesen 3000
<pre class="code"><code class="php">
16 1
$builder->where("name = ?", "Hemingway");
17 1
echo $builder; /* => SELECT * FROM authors WHERE name = ? */
18 1
print_r($builder->get_where_values()); /* => array("Hemingway") */
19 3 wesen 3000
</code></pre>
20 3 wesen 3000
21 3 wesen 3000
You can also pass a hash to the where() method:
22 3 wesen 3000
23 3 wesen 3000
<pre class="code"><code class="php">
24 3 wesen 3000
$builder = new ActiveRecord\SQLBuilder($conn, "authors");                                                                                                            
25 3 wesen 3000
$builder->where(array("name" => "Hemingway",                                                                                                                         
26 3 wesen 3000
                      "country" => "USA"));
27 3 wesen 3000
echo $builder; /* => SELECT * FROM authors WHERE `name`=? AND `country`=? */
28 3 wesen 3000
print_r($builder->get_where_values()); /* => array('Hemingway', 'USA'); */
29 3 wesen 3000
30 2 wesen 3000
</code></pre>