PageRenderTime 288ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/propel_14/vendor/propel/docs/guide/02-QuickStart.txt

http://github.com/eventhorizonpl/forked-php-orm-benchmark
Plain Text | 260 lines | 184 code | 76 blank | 0 comment | 0 complexity | 4d5b6eacdc0d30c0e8fb808a1fe76b41 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. = Propel Quickstart Guide =
  2. This guide aims to bring you up to speed with how Propel works by walking through the creation of a simple database and PHP classes needed to find & manipulate data in the database. This guide uses a simplified version of the Bookstore example project (a more extensive example of this project is available in the {{{generator/projects/}}} directory).
  3. == 1. Create a Working Directory ==
  4. Start by creating a {{{bookstore}}} directory for your project. The Propel generator expects to find your database definition file ({{{schema.xml}}}) and your build configuration options ({{{build.properties}}}) in this directory. This directory is also where your build SQL and PHP5 classes will be stored. (See wiki:Users/Documentation/1.3/HowTos/CustomizingBuild for information about how you can customize the build.)
  5. == 2. Describing Your Database in XML ==
  6. In order to create classes that accurately represent your tables (and the relationships between them) you need to create an XML representation of your database.
  7. === Overview of XML Datamodel Definition ===
  8. The XML schema for defining your datamodel in Propel closely follows the actual structure of the database. For a full reference of the schema, see the [wiki:Users/Documentation/1.3/Schema Schema reference] document.
  9. The top-most (root) tag of the the XMl document is the ''<database>'' tag.
  10. {{{
  11. #!xml
  12. <database name="bookstore" defaultIdMethod="native">
  13. </database>
  14. }}}
  15. The database name will be "bookstore". The ''defaultIdMethod'' attribute indicates that you want to use the database's "native" auto-increment/sequence features to handle id columns that are set to auto-increment.
  16. Within the ''<database>'' tags, you can define one or more tables that will be present in your database:
  17. {{{
  18. #!xml
  19. <database name="bookstore" defaultIdMethod="native">
  20. <table name="book">
  21. </table>
  22. <table name="author">
  23. </table>
  24. </database>
  25. }}}
  26. And within each set of ''<table>'' tags, you define the columns that belong in that table:
  27. {{{
  28. #!xml
  29. <database name="bookstore" defaultIdMethod="native">
  30. <table name="book">
  31. <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
  32. <column name="title" type="varchar" size="255" required="true" />
  33. </table>
  34. <table name="author">
  35. <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
  36. <column name="first_name" type="varchar" size="128" required="true"/>
  37. <column name="last_name" type="varchar" size="128" required="true"/>
  38. </table>
  39. </database>
  40. }}}
  41. That's the basic process of describing your database in XML for Propel. We'll look at other features like foreign-keys in the next section.
  42. === Creating your schema.xml ===
  43. Ok, now you're ready to define the complete bookstore datamodel in XML. Start by creating a new file, {{{schema.xml}}}, in the {{{bookstore}}} directory that you created in step 1.
  44. Here is the full schema that we will use. (Paste this into your {{{schema.xml}}} file and save it.)
  45. {{{
  46. #!xml
  47. <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
  48. <database name="bookstore" defaultIdMethod="native">
  49. <table name="book" description="Book Table">
  50. <column name="book_id" type="integer" primaryKey="true" autoIncrement="true" required="true" description="Book Id"/>
  51. <column name="title" type="varchar" size="255" required="true" description="Book Title"/>
  52. <column name="isbn" type="varchar" size="24" required="true" phpName="ISBN" description="ISBN Number"/>
  53. <column name="publisher_id" type="integer" required="true" description="Foreign Key for Publisher"/>
  54. <column name="author_id" type="integer" required="true" description="Foreign Key for Author"/>
  55. <foreign-key foreignTable="publisher">
  56. <reference local="publisher_id" foreign="publisher_id"/>
  57. </foreign-key>
  58. <foreign-key foreignTable="author">
  59. <reference local="author_id" foreign="author_id"/>
  60. </foreign-key>
  61. </table>
  62. <table name="publisher" description="Publisher Table">
  63. <column name="publisher_id" type="integer" required="true" primaryKey="true" autoIncrement="true" description="Publisher Id"/>
  64. <column name="name" type="varchar" size="128" required="true" description="Publisher Name"/>
  65. </table>
  66. <table name="author" description="Author Table">
  67. <column name="author_id" type="integer" required="true" primaryKey="true" autoIncrement="true" description="Author Id"/>
  68. <column name="first_name" type="varchar" size="128" required="true" description="First Name"/>
  69. <column name="last_name" type="varchar" size="128" required="true" description="Last Name"/>
  70. </table>
  71. </database>
  72. }}}
  73. Most of the XML above probably looks familiar (from previous section) or at least (hopefully) fairly intuitive.
  74. == 3. Setting Build Configuration ==
  75. In order to build your bookstore project, the Propel generator needs to know a few additional pieces of information. It expects to find this information in a {{{build.properties}}} file in your {{{bookstore}}} directory. There is a great deal of information that ''can'' be specified here (see wiki:Users/Documentation/1.3/HowTos/CustomizingBuild for some ideas) -- but at a bare minimum, Propel needs to know:
  76. 1. The name of your project,
  77. 1. What RDBMS you are using, and
  78. 1. How to connect to your database (assuming you want Propel to create the tables for you)
  79. Create a {{{build.properties}}} file in your {{{bookstore}}} directory and paste in the properties below, substituting an appropriate path for your database:
  80. {{{
  81. # The name of the project
  82. propel.project = bookstore
  83. # The database driver
  84. propel.database = sqlite
  85. # The connection parameters (optional, but required if you want to initialize db)
  86. #
  87. # This is the PDO DSN (see online docs http://www.php.net/pdo for more info)
  88. propel.database.url = sqlite:/path/to/bookstore.db
  89. # Other examples:
  90. # propel.database.url = mysql:host=localhost;dbname=test
  91. # propel.database.url = pgsql:host=localhost dbname=db-name user=db-username password=db-password
  92. #
  93. # If you are using MySQL or Oracle, you will have to specify any username and password separately
  94. # propel.database.user = db-user
  95. # propel.database.password = db-password
  96. }}}
  97. Note that these properties are for the Propel ''generator'' and don't (necessarily) have anything in common with the ''runtime'' configuration, which we will look at next.
  98. == 4. Setting Runtime Configuration ==
  99. The ''runtime'' configuration is used in your application to configure your generated object model classes and the shared Propel runtime classes. This information is not needed for building your SQL and PHP classes, but the runtime configuration is transformed into a more PHP "friendly" array format by the generator, so it makes sense to set these properties before building the project.
  100. Create a new file called {{{runtime-conf.xml}}} inside your {{{bookstore}}} directory and paste in the following configuration, substituting in the correct path to your {{{bookstore.db}}} file (as specified above in {{{build.properties}}} file).
  101. {{{
  102. #!xml
  103. <?xml version="1.0" encoding="ISO-8859-1"?>
  104. <config>
  105. <!-- Uncomment this if you have PEAR Log installed
  106. <log>
  107. <type>file</type>
  108. <name>/path/to/prople.log</name>
  109. <ident>propel-bookstore</ident>
  110. <level>7</level>
  111. </log>
  112. -->
  113. <propel>
  114. <datasources default="bookstore">
  115. <datasource id="bookstore"> <!-- this ID must match <database name=""> in schema.xml -->
  116. <adapter>sqlite</adapter> <!-- sqlite, mysql, myssql, oracle, or pgsql -->
  117. <connection>
  118. <dsn>sqlite2:/path/to/bookstore.db</dsn> <!-- the PDO connection DSN for database -->
  119. </connection>
  120. </datasource>
  121. </datasources>
  122. </propel>
  123. </config>
  124. }}}
  125. See the [wiki:Users/Documentation/1.3/RuntimeConf runtime configuration reference] for a more detailed explanation of this file.
  126. Note that if you uncomment the <log> section, Propel will attempt to instantiate the Log class (from PEAR Log package) with the specified parameters and use that to log queries. Propel's statement logging happens at the DEBUG level (7); errors and warnings are logged at the appropriate (non-debug) level.
  127. == 5. Building ==
  128. As of Propel 1.2, you can use the {{{propel-gen}}} script to generate the SQL and PHP object model for your project, whether you installed Propel using PEAR or conventional packages / SVN.
  129. You can execute the build by calling the {{{propel-gen}}} script with the path to your {{{bookstore}} project directory. The {{{propel-gen}}} script can perform many different functions, which can be selected by specifying a second ''target'' parameter. When no target is specified, the default "main" target is executed.
  130. The "main" target invokes the "sql", "om", and "convert-conf" targets which, respectively,
  131. 1. create the SQL DDL to create your database objects (tables, sequences, foreign keys, indexes, etc.),
  132. 1. create the PHP5 classes that provide an OO representation of your database, and
  133. 1. convert the XML runtime configuration into a PHP array that is easier (quicker) for PHP to parse.
  134. {{{
  135. $> propel-gen /path/to/bookstore
  136. }}}
  137. ''Note that you must use an absolute path when specifying the location of the {{{bookstore}}} directory.''
  138. You should see output from this command that lets you know in more detail what is being created and where it is being placed.
  139. Assuming that the build finished successfully, you should have the following new directories (containing files) {{{bookstore}}} directory.
  140. ||'''Path'''||'''Description'''||
  141. ||bookstore/build/classes/bookstore/*.php||The generated PHP classes that will be used to access your database.||
  142. ||bookstore/build/sql/schema.sql||The SQL DDL file that creates the database objects (tables, sequences, etc.)||
  143. ||bookstore/build/conf/bookstore-conf.php||The converted PHP array that holds your runtime configuration.||
  144. == 6. Using the Generated SQL and OM Files ==
  145. === Using the SQL Files ===
  146. The SQL definition file that we just created can be found at {{{bookstore/build/sql/schema.sql}}}. This file contains the SQL to create tables (and other objects) in an already-created database. For certain databases you can call {{{propel-gen}}} with the "create-db" target; however, it is usually easier to create the database (and set up the access permissions, etc.) outside of Propel (using your database tools) and then use Propel to setup & initialize the database.
  147. E.g. for MySQL:
  148. {{{
  149. $> mysqladmin create bookstore
  150. }}}
  151. The "insert-sql" target will apply the SQL statements from the {{{schema.sql}}} file to ''an existing'' database. '''Note that the {{{schema.sql}}} file will DROP any existing objects before creating them, which will effectively erase your database.'''
  152. {{{
  153. $> propel-gen /path/to/bookstore insert-sql
  154. }}}
  155. Depending on which RDBMS you are using, it may be normal to see some errors (e.g. "unable to DROP...") when you first run this script. This is because some databases have no way of checking to see whether a database object exists before attempting to DROP it (MySQL is a notable exception). It is safe to disregard these errors (and you can always run the script a second time to make sure that the errors are no longer present).
  156. === Using the Object Model ===
  157. For every table in the database, Propel creates 3 PHP classes:
  158. 1. an ''object'' class, which represents a row in the database;
  159. 1. a ''peer'' class, which is a static class that contains methods to operate on the table; and
  160. 1. a ''map'' class, which contains metadata information about the table that is needed for the runtime environment (you likely won't need to ever use this class).
  161. Propel also creates empty stub classes for the ''object'' and ''peer'' classes which is where you can put customized functionality. So, this means that for the "book" table, the following classes will be created:
  162. ||'''Class/Location*'''||'''Description'''||
  163. ||`bookstore/om/BaseBook.php`||Base class for representing a row from the "book" table.||
  164. ||`bookstore/om/BaseBookPeer.php`||Base static class for performing operations on "book" table.||
  165. ||`bookstore/map/BookTableMap.php`||Map class that contains a metadata represention of "book" table||
  166. ||`bookstore/Book.php`||Empty subclass for custom methods & overriding methods from `BaseBook`||
  167. ||`bookstore/BookPeer.php`||Empty peer subclass for custom methods & overriding methods from `BaseBookPeer`||
  168. * ''These paths are relative to `bookstore/build/classes/` directory.
  169. Tip: Never add any code to the classes generated by Propel in the `om/` and `map/` directories; use the stub classes instead. This is because every time you call the `propel-gen` script, Propel generates all the `Base` files again and overrides any modification.
  170. As we saw earlier, PHP classes have been created in the `bookstore/build/classes/bookstore/` directory. You can either add the `/path/to/bookstore/build/classes/` directory to your ''include_path'' or move the entire `bookstore/build/classes/bookstore/` directory to a location that is already on on your ''include_path''.
  171. For demonstration purposes, let's assume that you've added `/path/to/bookstore/build/classes/bookstore` to your ''include_path''.
  172. ==== Initializing Propel ====
  173. Before you can begin using your Propel objects, you must initialize Propel in your PHP script. You may wish to do this step in an init or setup script that included at the beginning of your PHP scripts. Initializing Propel configures the database adapter & database connection parameters and sets up any logging (if you specified a ''<log>'' section in the rutnime config file).
  174. You may also want to set your ''include_path'' in this initialization script -- if you aren't setting it in {{{php.ini}}} (or any of the other places INI options can be specified).
  175. {{{
  176. #!php
  177. <?php
  178. // Set the include_path to include your generated OM 'classes' dir.
  179. set_include_path("/path/to/bookstore/build/classes" . PATH_SEPARATOR . get_include_path());
  180. require_once 'propel/Propel.php';
  181. Propel::init("/path/to/bookstore/build/conf/bookstore-conf.php");
  182. }}}
  183. Now you are ready to include and begin using your Propel classes!
  184. Continue reading the [wiki:Users/Documentation/1.4/BasicCRUD Basic CRUD Guide] to see how to perform the basic C.R.U.D. (Create, Retrieve, Update, Delete) operations using your Propel-generated classes.