quickstart-create-model.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.quickstart.create-model">
  4. <title>Create a Model and Database Table</title>
  5. <para>
  6. Before we get started, let's consider something: where will these classes live, and how will
  7. we find them? The default project we created instantiates an autoloader. We can attach other
  8. autoloaders to it so that it knows where to find different classes. Typically, we want our
  9. various MVC classes grouped under the same tree -- in this case,
  10. <filename>application/</filename> -- and most often using a common prefix.
  11. </para>
  12. <para>
  13. <classname>Zend_Controller_Front</classname> has a notion of "modules", which are individual
  14. mini-applications. Modules mimic the directory structure that the <command>zf</command>
  15. tool sets up under <filename>application/</filename>, and all classes inside them are
  16. assumed to begin with a common prefix, the module name. <filename>application/</filename>
  17. is itself a module -- the "default" or "application" module. As such, we'll want to setup autoloading for resources
  18. within this directory.
  19. </para>
  20. <para>
  21. <classname>Zend_Application_Module_Autoloader</classname> provides the functionality needed
  22. to map the various resources under a module to the appropriate directories, and provides a
  23. standard naming mechanism as well. An instance of the class is created by default during
  24. initialization of the bootstrap object; your application bootstrap will be default use the
  25. module prefix "Application". As such, our models, forms, and table classes will all begin
  26. with the class prefix "Application_".
  27. </para>
  28. <para>
  29. Now, let's consider what makes up a guestbook. Typically, they are simply a list of entries
  30. with a <emphasis>comment</emphasis>, <emphasis>timestamp</emphasis>, and, often,
  31. <emphasis>email address</emphasis>. Assuming we store them in a database, we may also want a
  32. <emphasis>unique identifier</emphasis> for each entry. We'll likely want to be able to save
  33. an entry, fetch individual entries, and retrieve all entries. As such, a simple guestbook
  34. model API might look something like this:
  35. </para>
  36. <programlisting language="php"><![CDATA[
  37. // application/models/Guestbook.php
  38. class Application_Model_Guestbook
  39. {
  40. protected $_comment;
  41. protected $_created;
  42. protected $_email;
  43. protected $_id;
  44. public function __set($name, $value);
  45. public function __get($name);
  46. public function setComment($text);
  47. public function getComment();
  48. public function setEmail($email);
  49. public function getEmail();
  50. public function setCreated($ts);
  51. public function getCreated();
  52. public function setId($id);
  53. public function getId();
  54. public function save();
  55. public function find($id);
  56. public function fetchAll();
  57. }
  58. ]]></programlisting>
  59. <para>
  60. <methodname>__get()</methodname> and <methodname>__set()</methodname> will provide a
  61. convenience mechanism for us to access the individual entry properties, and proxy to the
  62. other getters and setters. They also will help ensure that only properties we whitelist will
  63. be available in the object.
  64. </para>
  65. <para>
  66. <methodname>find()</methodname> and <methodname>fetchAll()</methodname> provide the ability
  67. to fetch a single entry or all entries.
  68. </para>
  69. <para>
  70. Now from here, we can start thinking about setting up our database.
  71. </para>
  72. <para>
  73. First we need to initialize our <classname>Db</classname> resource. As with the
  74. <classname>Layout</classname> and <classname>View</classname> resource, we can provide
  75. configuration for the <classname>Db</classname> resource. In your
  76. <filename>application/configs/application.ini</filename> file, add the following lines in
  77. the appropriate sections.
  78. </para>
  79. <programlisting language="ini"><![CDATA[
  80. ; application/configs/application.ini
  81. ; Add these lines to the appropriate sections:
  82. [production]
  83. resources.db.adapter = "PDO_SQLITE"
  84. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"
  85. [testing : production]
  86. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-testing.db"
  87. [development : production]
  88. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"
  89. ]]></programlisting>
  90. <para>
  91. Your final configuration file should look like the following:
  92. </para>
  93. <programlisting language="ini"><![CDATA[
  94. ; application/configs/application.ini
  95. [production]
  96. phpSettings.display_startup_errors = 0
  97. phpSettings.display_errors = 0
  98. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  99. bootstrap.class = "Bootstrap"
  100. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  101. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  102. resources.view[] =
  103. resources.db.adapter = "PDO_SQLITE"
  104. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"
  105. [staging : production]
  106. [testing : production]
  107. phpSettings.display_startup_errors = 1
  108. phpSettings.display_errors = 1
  109. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-testing.db"
  110. [development : production]
  111. phpSettings.display_startup_errors = 1
  112. phpSettings.display_errors = 1
  113. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"
  114. ]]></programlisting>
  115. <para>
  116. Note that the database(s) will be stored in <filename>data/db/</filename>. Create those
  117. directories, and make them world-writeable. On unix-like systems, you can do that as
  118. follows:
  119. </para>
  120. <programlisting language="shell"><![CDATA[
  121. % mkdir -p data/db; chmod -R a+rwX data
  122. ]]></programlisting>
  123. <para>
  124. On Windows, you will need to create the directories in Explorer and set the permissions to
  125. allow anyone to write to the directory.
  126. </para>
  127. <para>
  128. At this point we have a connection to a database; in our case, its a connection to a Sqlite
  129. database located inside our <filename>application/data/</filename> directory. So, let's
  130. design a simple table that will hold our guestbook entries.
  131. </para>
  132. <programlisting language="sql"><![CDATA[
  133. -- scripts/schema.sqlite.sql
  134. --
  135. -- You will need load your database schema with this SQL.
  136. CREATE TABLE guestbook (
  137. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  138. email VARCHAR(32) NOT NULL DEFAULT 'noemail@test.com',
  139. comment TEXT NULL,
  140. created DATETIME NOT NULL
  141. );
  142. CREATE INDEX "id" ON "guestbook" ("id");
  143. ]]></programlisting>
  144. <para>
  145. And, so that we can have some working data out of the box, lets create a few rows of
  146. information to make our application interesting.
  147. </para>
  148. <programlisting language="sql"><![CDATA[
  149. -- scripts/data.sqlite.sql
  150. --
  151. -- You can begin populating the database with the following SQL statements.
  152. INSERT INTO guestbook (email, comment, created) VALUES
  153. ('ralph.schindler@zend.com',
  154. 'Hello! Hope you enjoy this sample zf application!',
  155. DATETIME('NOW'));
  156. INSERT INTO guestbook (email, comment, created) VALUES
  157. ('foo@bar.com',
  158. 'Baz baz baz, baz baz Baz baz baz - baz baz baz.',
  159. DATETIME('NOW'));
  160. ]]></programlisting>
  161. <para>
  162. Now that we have both the schema and some data defined. Lets get a script together that we
  163. can now execute to build this database. Naturally, this is not needed in production, but
  164. this script will help developers build out the database requirements locally so they can
  165. have the fully working application. Create the script as
  166. <filename>scripts/load.sqlite.php</filename> with the following contents:
  167. </para>
  168. <programlisting language="php"><![CDATA[
  169. // scripts/load.sqlite.php
  170. /**
  171. * Script for creating and loading database
  172. */
  173. // Initialize the application path and autoloading
  174. defined('APPLICATION_PATH')
  175. || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
  176. set_include_path(implode(PATH_SEPARATOR, array(
  177. APPLICATION_PATH . '/../library',
  178. get_include_path(),
  179. )));
  180. require_once 'Zend/Loader/Autoloader.php';
  181. Zend_Loader_Autoloader::getInstance();
  182. // Define some CLI options
  183. $getopt = new Zend_Console_Getopt(array(
  184. 'withdata|w' => 'Load database with sample data',
  185. 'env|e-s' => 'Application environment for which to create database (defaults to development)',
  186. 'help|h' => 'Help -- usage message',
  187. ));
  188. try {
  189. $getopt->parse();
  190. } catch (Zend_Console_Getopt_Exception $e) {
  191. // Bad options passed: report usage
  192. echo $e->getUsageMessage();
  193. return false;
  194. }
  195. // If help requested, report usage message
  196. if ($getopt->getOption('h')) {
  197. echo $getopt->getUsageMessage();
  198. return true;
  199. }
  200. // Initialize values based on presence or absence of CLI options
  201. $withData = $getopt->getOption('w');
  202. $env = $getopt->getOption('e');
  203. defined('APPLICATION_ENV')
  204. || define('APPLICATION_ENV', (null === $env) ? 'development' : $env);
  205. // Initialize Zend_Application
  206. $application = new Zend_Application(
  207. APPLICATION_ENV,
  208. APPLICATION_PATH . '/configs/application.ini'
  209. );
  210. // Initialize and retrieve DB resource
  211. $bootstrap = $application->getBootstrap();
  212. $bootstrap->bootstrap('db');
  213. $dbAdapter = $bootstrap->getResource('db');
  214. // let the user know whats going on (we are actually creating a
  215. // database here)
  216. if ('testing' != APPLICATION_ENV) {
  217. echo 'Writing Database Guestbook in (control-c to cancel): ' . PHP_EOL;
  218. for ($x = 5; $x > 0; $x--) {
  219. echo $x . "\r"; sleep(1);
  220. }
  221. }
  222. // Check to see if we have a database file already
  223. $options = $bootstrap->getOption('resources');
  224. $dbFile = $options['db']['params']['dbname'];
  225. if (file_exists($dbFile)) {
  226. unlink($dbFile);
  227. }
  228. // this block executes the actual statements that were loaded from
  229. // the schema file.
  230. try {
  231. $schemaSql = file_get_contents(dirname(__FILE__) . '/schema.sqlite.sql');
  232. // use the connection directly to load sql in batches
  233. $dbAdapter->getConnection()->exec($schemaSql);
  234. chmod($dbFile, 0666);
  235. if ('testing' != APPLICATION_ENV) {
  236. echo PHP_EOL;
  237. echo 'Database Created';
  238. echo PHP_EOL;
  239. }
  240. if ($withData) {
  241. $dataSql = file_get_contents(dirname(__FILE__) . '/data.sqlite.sql');
  242. // use the connection directly to load sql in batches
  243. $dbAdapter->getConnection()->exec($dataSql);
  244. if ('testing' != APPLICATION_ENV) {
  245. echo 'Data Loaded.';
  246. echo PHP_EOL;
  247. }
  248. }
  249. } catch (Exception $e) {
  250. echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
  251. echo $e->getMessage() . PHP_EOL;
  252. return false;
  253. }
  254. // generally speaking, this script will be run from the command line
  255. return true;
  256. ]]></programlisting>
  257. <para>
  258. Now, let's execute this script. From a terminal or the DOS command line, do the following:
  259. </para>
  260. <programlisting language="shell"><![CDATA[
  261. % php scripts/load.sqlite.php --withdata
  262. ]]></programlisting>
  263. <para>
  264. You should see output like the following:
  265. </para>
  266. <programlisting language="text"><![CDATA[
  267. path/to/ZendFrameworkQuickstart/scripts$ php load.sqlite.php --withdata
  268. Writing Database Guestbook in (control-c to cancel):
  269. 1
  270. Database Created
  271. Data Loaded.
  272. ]]></programlisting>
  273. <para>
  274. Now we have a fully working database and table for our guestbook application. Our next few
  275. steps are to build out our application code. This includes building a data source (in our
  276. case, we will use <classname>Zend_Db_Table</classname>), and a data mapper to connect that
  277. data source to our domain model. Finally we'll also create the controller that will interact
  278. with this model to both display existing entries and process new entries.
  279. </para>
  280. <para>
  281. We'll use a <ulink url="http://martinfowler.com/eaaCatalog/tableDataGateway.html">Table Data
  282. Gateway</ulink> to connect to our data source; <classname>Zend_Db_Table</classname>
  283. provides this functionality. To get started, lets create a
  284. <classname>Zend_Db_Table</classname>-based table class. First, create the directory
  285. <filename>application/models/DbTable/</filename>. Then create and edit a file
  286. <filename>Guestbook.php</filename> within it, and add the following contents:
  287. </para>
  288. <programlisting language="php"><![CDATA[
  289. // application/models/DbTable/Guestbook.php
  290. /**
  291. * This is the DbTable class for the guestbook table.
  292. */
  293. class Application_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
  294. {
  295. /** Table name */
  296. protected $_name = 'guestbook';
  297. }
  298. ]]></programlisting>
  299. <para>
  300. Note the class prefix: <classname>Application_Model_DbTable</classname>. The class prefix
  301. for our module, "Application", is the first segment, and then we have the component,
  302. "Model_DbTable"; the latter is mapped to the <filename>models/DbTable/</filename> directory
  303. of the module.
  304. </para>
  305. <para>
  306. All that is truly necessary when extending <classname>Zend_Db_Table</classname> is to
  307. provide a table name and optionally the primary key (if it is not "id").
  308. </para>
  309. <para>
  310. Now let's create a <ulink url="http://martinfowler.com/eaaCatalog/dataMapper.html">Data
  311. Mapper</ulink>. A <emphasis>Data Mapper</emphasis> maps a domain object to the database.
  312. In our case, it will map our model, <classname>Application_Model_Guestbook</classname>, to
  313. our data source, <classname>Application_Model_DbTable_Guestbook</classname>. A typical API
  314. for a data mapper is as follows:
  315. </para>
  316. <programlisting language="php"><![CDATA[
  317. // application/models/GuestbookMapper.php
  318. class Application_Model_GuestbookMapper
  319. {
  320. public function save($model);
  321. public function find($id, $model);
  322. public function fetchAll();
  323. }
  324. ]]></programlisting>
  325. <para>
  326. In addition to these methods, we'll add methods for setting and retrieving the Table Data
  327. Gateway. The final class, located in
  328. <filename>application/models/GuestbookMapper.php</filename>, looks like this:
  329. </para>
  330. <programlisting language="php"><![CDATA[
  331. // application/models/GuestbookMapper.php
  332. class Application_Model_GuestbookMapper
  333. {
  334. protected $_dbTable;
  335. public function setDbTable($dbTable)
  336. {
  337. if (is_string($dbTable)) {
  338. $dbTable = new $dbTable();
  339. }
  340. if (!$dbTable instanceof Zend_Db_Table_Abstract) {
  341. throw new Exception('Invalid table data gateway provided');
  342. }
  343. $this->_dbTable = $dbTable;
  344. return $this;
  345. }
  346. public function getDbTable()
  347. {
  348. if (null === $this->_dbTable) {
  349. $this->setDbTable('Application_Model_DbTable_Guestbook');
  350. }
  351. return $this->_dbTable;
  352. }
  353. public function save(Application_Model_Guestbook $guestbook)
  354. {
  355. $data = array(
  356. 'email' => $guestbook->getEmail(),
  357. 'comment' => $guestbook->getComment(),
  358. 'created' => date('Y-m-d H:i:s'),
  359. );
  360. if (null === ($id = $guestbook->getId())) {
  361. unset($data['id']);
  362. $this->getDbTable()->insert($data);
  363. } else {
  364. $this->getDbTable()->update($data, array('id = ?' => $id));
  365. }
  366. }
  367. public function find($id, Application_Model_Guestbook $guestbook)
  368. {
  369. $result = $this->getDbTable()->find($id);
  370. if (0 == count($result)) {
  371. return;
  372. }
  373. $row = $result->current();
  374. $guestbook->setId($row->id)
  375. ->setEmail($row->email)
  376. ->setComment($row->comment)
  377. ->setCreated($row->created);
  378. }
  379. public function fetchAll()
  380. {
  381. $resultSet = $this->getDbTable()->fetchAll();
  382. $entries = array();
  383. foreach ($resultSet as $row) {
  384. $entry = new Application_Model_Guestbook();
  385. $entry->setId($row->id)
  386. ->setEmail($row->email)
  387. ->setComment($row->comment)
  388. ->setCreated($row->created)
  389. ->setMapper($this);
  390. $entries[] = $entry;
  391. }
  392. return $entries;
  393. }
  394. }
  395. ]]></programlisting>
  396. <para>
  397. Now it's time to update our model class slightly, to accomodate the data mapper. Just like
  398. the data mapper contains a reference to the data source, the model contains a reference to
  399. the data mapper. Additionally, we'll make it easy to populate the model by passing an array
  400. of data either to the constructor or a <methodname>setOptions()</methodname> method. The
  401. final model class, located in <filename>application/models/Guestbook.php</filename>, looks
  402. like this:
  403. </para>
  404. <programlisting language="php"><![CDATA[
  405. // application/models/Guestbook.php
  406. class Application_Model_Guestbook
  407. {
  408. protected $_comment;
  409. protected $_created;
  410. protected $_email;
  411. protected $_id;
  412. protected $_mapper;
  413. public function __construct(array $options = null)
  414. {
  415. if (is_array($options)) {
  416. $this->setOptions($options);
  417. }
  418. }
  419. public function __set($name, $value)
  420. {
  421. $method = 'set' . $name;
  422. if (('mapper' == $name) || !method_exists($this, $method)) {
  423. throw new Exception('Invalid guestbook property');
  424. }
  425. $this->$method($value);
  426. }
  427. public function __get($name)
  428. {
  429. $method = 'get' . $name;
  430. if (('mapper' == $name) || !method_exists($this, $method)) {
  431. throw new Exception('Invalid guestbook property');
  432. }
  433. return $this->$method();
  434. }
  435. public function setOptions(array $options)
  436. {
  437. $methods = get_class_methods($this);
  438. foreach ($options as $key => $value) {
  439. $method = 'set' . ucfirst($key);
  440. if (in_array($method, $methods)) {
  441. $this->$method($value);
  442. }
  443. }
  444. return $this;
  445. }
  446. public function setComment($text)
  447. {
  448. $this->_comment = (string) $text;
  449. return $this;
  450. }
  451. public function getComment()
  452. {
  453. return $this->_comment;
  454. }
  455. public function setEmail($email)
  456. {
  457. $this->_email = (string) $email;
  458. return $this;
  459. }
  460. public function getEmail()
  461. {
  462. return $this->_email;
  463. }
  464. public function setCreated($ts)
  465. {
  466. $this->_created = $ts;
  467. return $this;
  468. }
  469. public function getCreated()
  470. {
  471. return $this->_created;
  472. }
  473. public function setId($id)
  474. {
  475. $this->_id = (int) $id;
  476. return $this;
  477. }
  478. public function getId()
  479. {
  480. return $this->_id;
  481. }
  482. public function setMapper($mapper)
  483. {
  484. $this->_mapper = $mapper;
  485. return $this;
  486. }
  487. public function getMapper()
  488. {
  489. if (null === $this->_mapper) {
  490. $this->setMapper(new Application_Model_GuestbookMapper());
  491. }
  492. return $this->_mapper;
  493. }
  494. public function save()
  495. {
  496. $this->getMapper()->save($this);
  497. }
  498. public function find($id)
  499. {
  500. $this->getMapper()->find($id, $this);
  501. return $this;
  502. }
  503. public function fetchAll()
  504. {
  505. return $this->getMapper()->fetchAll();
  506. }
  507. }
  508. ]]></programlisting>
  509. <para>
  510. Lastly, to connect these elements all together, lets create a guestbook controller that will
  511. both list the entries that are currently inside the database.
  512. </para>
  513. <para>
  514. To create a new controller, open a terminal or DOS console, navigate to your project
  515. directory, and enter the following:
  516. </para>
  517. <programlisting language="shell"><![CDATA[
  518. # Unix-like systems:
  519. % zf.sh create controller guestbook
  520. # DOS/Windows:
  521. C:> zf.bat create controller guestbook
  522. ]]></programlisting>
  523. <para>
  524. This will create a new controller, <classname>GuestbookController</classname>, in
  525. <filename>application/controllers/GuestbookController.php</filename>, with a single action
  526. method, <methodname>indexAction()</methodname>. It will also create a view script directory
  527. for the controller, <filename>application/views/scripts/guestbook/</filename>, with a view
  528. script for the index action.
  529. </para>
  530. <para>
  531. We'll use the "index" action as a landing page to view all guestbook entries.
  532. </para>
  533. <para>
  534. Now, let's flesh out the basic application logic. On a hit to
  535. <methodname>indexAction()</methodname>, we'll display all guestbook entries. This would look
  536. like the following:
  537. </para>
  538. <programlisting language="php"><![CDATA[
  539. // application/controllers/GuestbookController.php
  540. class GuestbookController extends Zend_Controller_Action
  541. {
  542. public function indexAction()
  543. {
  544. $guestbook = new Application_Model_Guestbook();
  545. $this->view->entries = $guestbook->fetchAll();
  546. }
  547. }
  548. ]]></programlisting>
  549. <para>
  550. And, of course, we need a view script to go along with that. Edit
  551. <filename>application/views/scripts/guestbook/index.phtml</filename> to read as follows:
  552. </para>
  553. <programlisting language="php"><![CDATA[
  554. <!-- application/views/scripts/guestbook/index.phtml -->
  555. <p><a href="<?php echo $this->url(
  556. array(
  557. 'controller' => 'guestbook',
  558. 'action' => 'sign'
  559. ),
  560. 'default',
  561. true) ?>">Sign Our Guestbook</a></p>
  562. Guestbook Entries: <br />
  563. <dl>
  564. <?php foreach ($this->entries as $entry): ?>
  565. <dt><?php echo $this->escape($entry->email) ?></dt>
  566. <dd><?php echo $this->escape($entry->comment) ?></dd>
  567. <?php endforeach ?>
  568. </dl>
  569. ]]></programlisting>
  570. <note>
  571. <title>Checkpoint</title>
  572. <para>
  573. Now browse to "http://localhost/guestbook". You should see the following in your
  574. browser:
  575. </para>
  576. <para>
  577. <inlinegraphic width="525" scale="100" align="center" valign="middle"
  578. fileref="figures/learning.quickstart.create-model.png" format="PNG" />
  579. </para>
  580. </note>
  581. <note>
  582. <title>Using the data loader script</title>
  583. <para>
  584. The data loader script introduced in this section
  585. (<filename>scripts/load.sqlite.php</filename>) can be used to create the database for
  586. each environment you have defined, as well as to load it with sample data. Internally,
  587. it utilizes <classname>Zend_Console_Getopt</classname>, which allows it to provide a
  588. number of command line switches. If you pass the "-h" or "--help" switch, it will give
  589. you the available options:
  590. </para>
  591. <programlisting language="php"><![CDATA[
  592. Usage: load.sqlite.php [ options ]
  593. --withdata|-w Load database with sample data
  594. --env|-e [ ] Application environment for which to create database
  595. (defaults to development)
  596. --help|-h Help -- usage message)]]
  597. ]]></programlisting>
  598. <para>
  599. The "-e" switch allows you to specify the value to use for the constant
  600. <constant>APPLICATION_ENV</constant> -- which in turn allows you to create a SQLite
  601. database for each environment you define. Be sure to run the script for the environment
  602. you choose for your application when deploying.
  603. </para>
  604. </note>
  605. </sect1>