| 1 |
lars |
1 |
<com:TContent ID="Main">
|
|
|
2 |
|
|
|
3 |
<h1>Establishing DB Connection</h1>
|
|
|
4 |
|
|
|
5 |
<p>
|
|
|
6 |
To use the database that we just created, we first need to establish a connection to it.
|
|
|
7 |
</p>
|
|
|
8 |
|
|
|
9 |
<p>
|
|
|
10 |
We are going to use <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.DAO">Data Access Objects (DAO)</a> to abstract our data access mechanisms. If in future we decide to use a different DBMS (e.g. PostgreSQL, Oracle) to store our blog data, we only need to change the database source name (DSN) and we can keep our PHP code intact.
|
|
|
11 |
</p>
|
|
|
12 |
|
|
|
13 |
<com:NoteBox>
|
|
|
14 |
To use DAO, you have to install and enable the <a href="http://www.php.net/manual/en/ref.pdo.php">PHP PDO extension</a> <i>and</i> a database-specific PDO driver (in our case, it is the SQLite PDO driver). This can be achieved easily on Windows by modifying the <tt>php.ini</tt> file with the following lines:
|
|
|
15 |
<com:TTextHighlighter CssClass="source">
|
|
|
16 |
extension=php_pdo.dll
|
|
|
17 |
extension=php_pdo_sqlite.dll
|
|
|
18 |
</com:TTextHighlighter>
|
|
|
19 |
</com:NoteBox>
|
|
|
20 |
|
|
|
21 |
<p>
|
|
|
22 |
To further abstract the actual database tables, we will also use the <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.ActiveRecord">Active Record</a> (AR) feature. Each data record will be represented as an Active Record object which is capable of performing query, saving and deletion without writing SQL statements.
|
|
|
23 |
</p>
|
|
|
24 |
|
|
|
25 |
<p>
|
|
|
26 |
We modify our application configuration file <tt>protected/application.xml</tt> by inserting the following lines, which tells Active Record how to connect to our newly created database:
|
|
|
27 |
</p>
|
|
|
28 |
|
|
|
29 |
<com:TTextHighlighter CssClass="source" Language="xml">
|
|
|
30 |
<modules>
|
|
|
31 |
<module id="db" class="System.Data.TDataSourceConfig">
|
|
|
32 |
<database ConnectionString="sqlite:protected/data/blog.db" />
|
|
|
33 |
</module>
|
|
|
34 |
<module class="System.Data.ActiveRecord.TActiveRecordConfig" ConnectionID="db" />
|
|
|
35 |
</modules>
|
|
|
36 |
</com:TTextHighlighter>
|
|
|
37 |
|
|
|
38 |
<p>
|
|
|
39 |
The configuration above shows that we are adding two <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Modules">modules</a> to our application. The <tt>TDataSourceConfig</tt> module is configured with the connection string <tt>sqlite:protected/data/blog.db</tt> which points to our SQLite database. This connection is used by the <tt>TActiveRecordConfig</tt> module which is required by Active Record.
|
|
|
40 |
</p>
|
|
|
41 |
|
|
|
42 |
<com:InfoBox>
|
|
|
43 |
One may set up two or more DB connections in the application configuration. For more details, see the <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.ActiveRecord">Active Record documentation</a>. And of course, one may also explicitly create a DB connection in PHP code using the <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.DAO">TDbConnection</a> component in PDO.
|
|
|
44 |
</com:InfoBox>
|
|
|
45 |
|
|
|
46 |
</com:TContent>
|