quickstart-create-model.xml 24 KB

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