summaryrefslogtreecommitdiff
path: root/demos/blog-tutorial/protected/pages/Day2/ConnectDB.page
blob: 90db11ef660167f321c296a5dfb3fc603156e93a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<com:TContent ID="Main">

<h1>Establishing DB Connection</h1>

<p>
To use the database that we just created, we first need to establish a connection to it.
</p>

<p>
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.
</p>

<com:NoteBox>
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:
<com:TTextHighlighter CssClass="source">
extension=php_pdo.dll
extension=php_pdo_sqlite.dll
</com:TTextHighlighter>
</com:NoteBox>

<p>
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.
</p>

<p>
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:
</p>

<com:TTextHighlighter CssClass="source" Language="xml">
<modules>
  <module class="System.Data.ActiveRecord.TActiveRecordConfig">
    <database ConnectionString="sqlite:protected/data/blog.db" />
  </module>
</modules>
</com:TTextHighlighter>

<p>
The configuration above shows that we are adding to our application a <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Modules">module</a> whose class is specified in the <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Components">namespace</a> format as <tt>System.Data.ActiveRecord.TActiveRecordConfig</tt>. Through this module, Active Record will automatically establish a DB connection by using the connection information given in <tt>ConnectionString</tt>.
</p>

<com:InfoBox>
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.
</com:InfoBox>

</com:TContent>