|
|
@@ -2,37 +2,40 @@
|
|
|
<!-- EN-Revision: 19777 -->
|
|
|
<!-- Reviewed: no -->
|
|
|
<sect1 id="learning.quickstart.create-model">
|
|
|
- <title>Create a Model and Database Table</title>
|
|
|
+ <title>Ein Modell und eine Datenbank Tabelle erstellen</title>
|
|
|
|
|
|
<para>
|
|
|
- Before we get started, let's consider something: where will these classes live, and how will
|
|
|
- we find them? The default project we created instantiates an autoloader. We can attach other
|
|
|
- autoloaders to it so that it knows where to find different classes. Typically, we want our
|
|
|
- various MVC classes grouped under the same tree -- in this case,
|
|
|
- <filename>application/</filename> -- and most often using a common prefix.
|
|
|
+ Bevor wir anfangen nehmen wie etwas an: Wo werden diese Klassen leben, und wie werden wir
|
|
|
+ Sie finden? Das Standardprojekt welches wir erstellt haben instanziert einen Autoloader. Wir
|
|
|
+ können Ihm andere Autoloader anhängen damit er weiss wo andere Klassen zu finden sind.
|
|
|
+ Typischerweise wollen wir das unsere verschiedenen MVC Klassen im selben Baum gruppiert sind
|
|
|
+ -- in diesem Fall <filename>application/</filename> -- und meistens einen gemeinsamen Präfix
|
|
|
+ verwenden.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- <classname>Zend_Controller_Front</classname> has a notion of "modules", which are individual
|
|
|
- mini-applications. Modules mimic the directory structure that the <command>zf</command>
|
|
|
- tool sets up under <filename>application/</filename>, and all classes inside them are
|
|
|
- assumed to begin with a common prefix, the module name. <filename>application/</filename>
|
|
|
- is itself a module -- the "default" module. As such, let's setup autoloading for resources
|
|
|
- within this directory, giving them a prefix of "Default". We can do this by creating another
|
|
|
- bootstrap resource.
|
|
|
+ <classname>Zend_Controller_Front</classname> hat den Begriff von "Modulen", welche
|
|
|
+ individuelle Mini-Anwendungen sind. Module mimen die Verzeichnisstruktur welche das
|
|
|
+ <command>zf</command> Tool unter <filename>application/</filename> einrichtet, und von allen
|
|
|
+ Klassen in Ihm wird angenommen das Sie mit einen gemeinsamen Präfix beginnen, dem Namen des
|
|
|
+ Moduls. <filename>application/</filename> selbst ist ein Modul -- das "default" (Standard-)
|
|
|
+ Modul. Als solches richten wir das Autoloading für Ressourcen in diesem Verzeichnis ein, und
|
|
|
+ geben Ihm den Präfix "Default". Das kann getan werden indem eine weitere Bootstrap Ressource
|
|
|
+ erstellt wird.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- <classname>Zend_Application_Module_Autoloader</classname> provides the functionality needed
|
|
|
- to map the various resources under a module to the appropriate directories, and provides a
|
|
|
- standard naming mechanism as well. In our bootstrap resource, we'll instantiate this, and be
|
|
|
- done. The method looks like this:
|
|
|
+ <classname>Zend_Application_Module_Autoloader</classname> bietet die Funktionalität welche
|
|
|
+ benötigt wird um die verschiedenen Ressourcen unter einem Modul mit den richtigen
|
|
|
+ Verzeichnissen zu verbinden, und auch einen standardmäßigen Namensmechanismus. In unserer
|
|
|
+ Bootstrap Ressource instanzieren wir dass, und das wars schon. Diese Methode sieht wie folgt
|
|
|
+ aus:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
// application/Bootstrap.php
|
|
|
|
|
|
-// Add this method to the Bootstrap class:
|
|
|
+// Diese Methode in der Bootstrap Klasse hinzufügen:
|
|
|
|
|
|
protected function _initAutoload()
|
|
|
{
|
|
|
@@ -45,7 +48,7 @@
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- The final bootstrap class will look as follows:
|
|
|
+ Die endgültige Bootstrap Klasse wird wie folgt aussehen:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
@@ -72,12 +75,13 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Now, let's consider what makes up a guestbook. Typically, they are simply a list of entries
|
|
|
- with a <emphasis>comment</emphasis>, <emphasis>timestamp</emphasis>, and, often,
|
|
|
- <emphasis>email address</emphasis>. Assuming we store them in a database, we may also want a
|
|
|
- <emphasis>unique identifier</emphasis> for each entry. We'll likely want to be able to save
|
|
|
- an entry, fetch individual entries, and retrieve all entries. As such, a simple guestbook
|
|
|
- model API might look something like this:
|
|
|
+ Nehmen wir jetzt also an was ein Guestbook ausmacht. Typischerweise sind Sie einfach eine
|
|
|
+ Liste ein Einträgen mit einem <emphasis>Kommentar</emphasis> (comment), einem
|
|
|
+ <emphasis>Zeitpunkt</emphasis> (timestamp) und oft einer <emphasis>Email Adresse</emphasis>.
|
|
|
+ Angenommen wir speichern diese in einer Datenbank, dann wollen wir auch einen
|
|
|
+ <emphasis>eindeutigen Identifikator</emphasis> für jeden Eintrag. Wir wollen in der Lage
|
|
|
+ sein einen Eintrag zu speichern, individuelle Einträge zu holen, und alle Einträge zu
|
|
|
+ empfangen. Als solches könnte das Modell einer einfachen Guestbook API wie folgt aussehen:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
@@ -112,33 +116,33 @@ class Default_Model_Guestbook
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- <methodname>__get()</methodname> and <methodname>__set()</methodname> will provide a
|
|
|
- convenience mechanism for us to access the individual entry properties, and proxy to the
|
|
|
- other getters and setters. They also will help ensure that only properties we whitelist will
|
|
|
- be available in the object.
|
|
|
+ <methodname>__get()</methodname> und <methodname>__set()</methodname> bieten uns bequeme
|
|
|
+ Mechanismen an um auf individuelle Eigenschaften von Einträgen zuzugreifen und auf andere
|
|
|
+ Getter und Setter zu verweisen. Sie stellen auch sicher das nur Eigenschaften im Objekt
|
|
|
+ vorhanden sind die wir freigegeben haben.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- <methodname>find()</methodname> and <methodname>fetchAll()</methodname> provide the ability
|
|
|
- to fetch a single entry or all entries.
|
|
|
+ <methodname>find()</methodname> und <methodname>fetchAll()</methodname> bieten die Fähigkeit
|
|
|
+ einen einzelnen Eintrag oder alle Einträge zu holen.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- Now from here, we can start thinking about setting up our database.
|
|
|
+ Von hier an können wir über die Einrichtung unserer Datenbank nachdenken.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- First we need to initialize our <classname>Db</classname> resource. As with the
|
|
|
- <classname>Layout</classname> and <classname>View</classname> resource, we can provide
|
|
|
- configuration for the <classname>Db</classname> resource. In your
|
|
|
- <filename>application/configs/application.ini</filename> file, add the following lines in
|
|
|
- the appropriate sections.
|
|
|
+ Zuerst muss unsere <classname>Db</classname> Ressource initialisiert werden. Wie bei der
|
|
|
+ <classname>Layout</classname> und <classname>View</classname> kann die Konfiguration für die
|
|
|
+ <classname>Db</classname> Ressource angegeben werden. In der Datei
|
|
|
+ <filename>application/configs/application.ini</filename> müssen die folgenden Zeilen in den
|
|
|
+ richtigen Sektionen hinzugefügt werden.
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="ini"><![CDATA[
|
|
|
; application/configs/application.ini
|
|
|
|
|
|
-; Add these lines to the appropriate sections:
|
|
|
+; Diese Zeile sind in den richtigen Sektionen hinzuzufügen:
|
|
|
[production]
|
|
|
resources.db.adapter = "PDO_SQLITE"
|
|
|
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"
|
|
|
@@ -151,7 +155,7 @@ resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Your final configuration file should look like the following:
|
|
|
+ Die endgültige Konfigurationsdatei sollte wie folgt aussehen:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="ini"><![CDATA[
|
|
|
@@ -182,9 +186,9 @@ resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Note that the database(s) will be stored in <filename>data/db/</filename>. Create those
|
|
|
- directories, and make them world-writeable. On unix-like systems, you can do that as
|
|
|
- follows:
|
|
|
+ Es ist zu beachten das die Datenbank(en) unter <filename>data/db/</filename> gespeichert
|
|
|
+ wird. Diese Verzeichnisse sind zu erstellen und weltweit-schreibbar zu machen. Auf
|
|
|
+ Unix-artigen Systemen kann man das wie folgt durchführen:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="shell"><![CDATA[
|
|
|
@@ -192,20 +196,21 @@ resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- On Windows, you will need to create the directories in Explorer and set the permissions to
|
|
|
- allow anyone to write to the directory.
|
|
|
+ Unter Windows muss man die Verzeichnisse im Explorer erstellen und die Zugriffsrechte so zu
|
|
|
+ setzen das jeder in das Verzeichnis schreiben darf.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- At this point we have a connection to a database; in our case, its a connection to a Sqlite
|
|
|
- database located inside our <filename>application/data/</filename> directory. So, let's
|
|
|
- design a simple table that will hold our guestbook entries.
|
|
|
+ Ab diesem Punkt haben wir eine Verbindung zu einer Datenbank; in unserem Fall ist es eine
|
|
|
+ verbindung zu einer Sqlite Datenbank die in unserem <filename>application/data/</filename>
|
|
|
+ Verzeichnis ist. Designen wir also eine einfache Tabelle die unsere Guestbook Einträge
|
|
|
+ enthalten wird.
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="sql"><![CDATA[
|
|
|
-- scripts/schema.sqlite.sql
|
|
|
--
|
|
|
--- You will need load your database schema with this SQL.
|
|
|
+-- Man muss das Datenbank Schema mit diesem SQL laden.
|
|
|
|
|
|
CREATE TABLE guestbook (
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
|
@@ -218,18 +223,19 @@ CREATE INDEX "id" ON "guestbook" ("id");
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- And, so that we can have some working data out of the box, lets create a few rows of
|
|
|
- information to make our application interesting.
|
|
|
+ Und damit wir gleich einige Arbeitsdaten haben, erstellen wir ein paar Zeilen an Information
|
|
|
+ um unsere Anwendung interessant zu machen.
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="sql"><![CDATA[
|
|
|
-- scripts/data.sqlite.sql
|
|
|
--
|
|
|
--- You can begin populating the database with the following SQL statements.
|
|
|
+-- Man kann damit beginnen die Datenbank zu befüllen indem die folgenden SQL
|
|
|
+-- Anweisungen ausgeführt werden.
|
|
|
|
|
|
INSERT INTO guestbook (email, comment, created) VALUES
|
|
|
('ralph.schindler@zend.com',
|
|
|
- 'Hello! Hope you enjoy this sample zf application!',
|
|
|
+ 'Hallo! Hoffentlich geniesst Ihr dieses Beispiel einer ZF Anwendung!
|
|
|
DATETIME('NOW'));
|
|
|
INSERT INTO guestbook (email, comment, created) VALUES
|
|
|
('foo@bar.com',
|
|
|
@@ -238,21 +244,22 @@ INSERT INTO guestbook (email, comment, created) VALUES
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Now that we have both the schema and some data defined. Lets get a script together that we
|
|
|
- can now execute to build this database. Naturally, this is not needed in production, but
|
|
|
- this script will help developers build out the database requirements locally so they can
|
|
|
- have the fully working application. Create the script as
|
|
|
- <filename>scripts/load.sqlite.php</filename> with the following contents:
|
|
|
+ Jetzt haben wir sowohl das Schema als auch einige Daten definiert. Schreiben wir also ein
|
|
|
+ Skript das wir jetzt ausführen können um diese Datenbank zu erstellen. Natürlich wird das
|
|
|
+ nicht in der Produktion benötigt, aber dieses Skriupt hilft Entwicklern die Notwendigkeiten
|
|
|
+ der Datenbank lokal zu erstellen damit Sie eine voll funktionsfähige Anwendung haben. Das
|
|
|
+ Skript ist als <filename>scripts/load.sqlite.php</filename> mit dem folgenden Inhalt zu
|
|
|
+ erstellen:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
// scripts/load.sqlite.php
|
|
|
|
|
|
/**
|
|
|
- * Script for creating and loading database
|
|
|
+ * Skript für das erstellen und Laden der Datenbank
|
|
|
*/
|
|
|
|
|
|
-// Initialize the application path and autoloading
|
|
|
+// Initialisiert den Pfad und das Autoloading der Anwendung
|
|
|
defined('APPLICATION_PATH')
|
|
|
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
|
|
|
set_include_path(implode(PATH_SEPARATOR, array(
|
|
|
@@ -262,95 +269,97 @@ set_include_path(implode(PATH_SEPARATOR, array(
|
|
|
require_once 'Zend/Loader/Autoloader.php';
|
|
|
Zend_Loader_Autoloader::getInstance();
|
|
|
|
|
|
-// Define some CLI options
|
|
|
+// Definiert einige CLI Optionen
|
|
|
$getopt = new Zend_Console_Getopt(array(
|
|
|
- 'withdata|w' => 'Load database with sample data',
|
|
|
- 'env|e-s' => 'Application environment for which to create database (defaults to development)',
|
|
|
- 'help|h' => 'Help -- usage message',
|
|
|
+ 'withdata|w' => 'Datenbank mit einigen Daten laden',
|
|
|
+ 'env|e-s' => "Anwendungsumgebung für welche die Datenbank "
|
|
|
+ . "erstellt wird (Standard ist Development)",
|
|
|
+ 'help|h' => 'Hilfe -- Verwendung',
|
|
|
));
|
|
|
try {
|
|
|
$getopt->parse();
|
|
|
} catch (Zend_Console_Getopt_Exception $e) {
|
|
|
- // Bad options passed: report usage
|
|
|
+ // Schlechte Option übergeben: Verwendung ausgeben
|
|
|
echo $e->getUsageMessage();
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-// If help requested, report usage message
|
|
|
+// Wenn Hilfe angefragt wurde, Verwendung ausgeben
|
|
|
if ($getopt->getOption('h')) {
|
|
|
echo $getopt->getUsageMessage();
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-// Initialize values based on presence or absence of CLI options
|
|
|
+// Werte basierend auf Ihrer Anwesenheit oder Abwesenheit von CLI Optionen initialisieren
|
|
|
$withData = $getopt->getOption('w');
|
|
|
$env = $getopt->getOption('e');
|
|
|
defined('APPLICATION_ENV')
|
|
|
|| define('APPLICATION_ENV', (null === $env) ? 'development' : $env);
|
|
|
|
|
|
-// Initialize Zend_Application
|
|
|
+// Zend_Application initialisieren
|
|
|
$application = new Zend_Application(
|
|
|
APPLICATION_ENV,
|
|
|
APPLICATION_PATH . '/configs/application.ini'
|
|
|
);
|
|
|
|
|
|
-// Initialize and retrieve DB resource
|
|
|
+// Die DB Ressource initialisieren und empfangen
|
|
|
$bootstrap = $application->getBootstrap();
|
|
|
$bootstrap->bootstrap('db');
|
|
|
$dbAdapter = $bootstrap->getResource('db');
|
|
|
|
|
|
-// let the user know whats going on (we are actually creating a
|
|
|
-// database here)
|
|
|
+// Den Benutzer informieren was abgeht
|
|
|
+// (wir erstellen hier aktuell eine Datenbank)
|
|
|
if ('testing' != APPLICATION_ENV) {
|
|
|
- echo 'Writing Database Guestbook in (control-c to cancel): ' . PHP_EOL;
|
|
|
+ echo 'Schreiben in die Guestbook Datenbank (control-c um abzubrechen): ' . PHP_EOL;
|
|
|
for ($x = 5; $x > 0; $x--) {
|
|
|
echo $x . "\r"; sleep(1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Check to see if we have a database file already
|
|
|
+// Prüfen um zu sehen ob wie bereits eine Datenbankdatei haben
|
|
|
$options = $bootstrap->getOption('resources');
|
|
|
$dbFile = $options['db']['params']['dbname'];
|
|
|
if (file_exists($dbFile)) {
|
|
|
unlink($dbFile);
|
|
|
}
|
|
|
|
|
|
-// this block executes the actual statements that were loaded from
|
|
|
-// the schema file.
|
|
|
+// Dieser Block führt die aktuellen Statements aus welche von der Schemadatei
|
|
|
+// geladen werden.
|
|
|
try {
|
|
|
$schemaSql = file_get_contents(dirname(__FILE__) . '/schema.sqlite.sql');
|
|
|
- // use the connection directly to load sql in batches
|
|
|
+ // Die Verbindung direkt verwenden um SQL im Block zu laden
|
|
|
$dbAdapter->getConnection()->exec($schemaSql);
|
|
|
chmod($dbFile, 0666);
|
|
|
|
|
|
if ('testing' != APPLICATION_ENV) {
|
|
|
echo PHP_EOL;
|
|
|
- echo 'Database Created';
|
|
|
+ echo 'Datenbank erstellt';
|
|
|
echo PHP_EOL;
|
|
|
}
|
|
|
|
|
|
if ($withData) {
|
|
|
$dataSql = file_get_contents(dirname(__FILE__) . '/data.sqlite.sql');
|
|
|
- // use the connection directly to load sql in batches
|
|
|
+ // Die Verbindung direkt verwenden um SQL in Blöcken zu laden
|
|
|
$dbAdapter->getConnection()->exec($dataSql);
|
|
|
if ('testing' != APPLICATION_ENV) {
|
|
|
- echo 'Data Loaded.';
|
|
|
+ echo 'Daten geladen.';
|
|
|
echo PHP_EOL;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
- echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
|
|
|
+ echo 'EIN FEHLER IST AUFGETRETEN:' . PHP_EOL;
|
|
|
echo $e->getMessage() . PHP_EOL;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-// generally speaking, this script will be run from the command line
|
|
|
+// Generell gesprochen wird dieses Skript von der Kommandozeile aus aufgerufen
|
|
|
return true;
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Now, let's execute this script. From a terminal or the DOS command line, do the following:
|
|
|
+ Jetzt führen wir dieses Skript aus. Von einem Terminal oder der DOS Kommandozeile ist das
|
|
|
+ folgende zu tun:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="shell"><![CDATA[
|
|
|
@@ -358,65 +367,71 @@ return true;
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- You should see output like the following:
|
|
|
+ Man sollte eine ähnliche Ausgabe wie folgt sehen:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="text"><![CDATA[
|
|
|
path/to/ZendFrameworkQuickstart/scripts$ php load.sqlite.php --withdata
|
|
|
-Writing Database Guestbook in (control-c to cancel):
|
|
|
+Schreiben in die Guestbook Datenbank (control-c um abzubrechen):
|
|
|
1
|
|
|
-Database Created
|
|
|
-Data Loaded.
|
|
|
+Datenbank erstellt
|
|
|
+Daten geladen.
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Now we have a fully working database and table for our guestbook application. Our next few
|
|
|
- steps are to build out our application code. This includes building a data source (in our
|
|
|
- case, we will use <classname>Zend_Db_Table</classname>), and a data mapper to connect that
|
|
|
- data source to our domain model. Finally we'll also create the controller that will interact
|
|
|
- with this model to both display existing entries and process new entries.
|
|
|
+ Jetzt haben wir eine voll funktionsfähige Datenbank und eine Tabelle für unsere Guestbook
|
|
|
+ Anwendung. Unsere nächsten paar Schritte sind die Ausarbeitung unseres Anwendungscodes. Das
|
|
|
+ inkludiert das Bauen einer Datenquelle (in unserem Fall verwenden wir
|
|
|
+ <classname>Zend_Db_Table</classname>), und einen Daten Mapper um diese Datenquelle mit
|
|
|
+ unserem Domain Modell zu verbinden. Letztendlich erstellen wir den Controller der mit diesem
|
|
|
+ Modell interagiert damit sowohl existierende Einträge angezeigt als auch neue Einträge
|
|
|
+ bearbeitet werden.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- We'll use a <ulink url="http://martinfowler.com/eaaCatalog/tableDataGateway.html">Table Data
|
|
|
- Gateway</ulink> to connect to our data source; <classname>Zend_Db_Table</classname>
|
|
|
- provides this functionality. To get started, lets create a
|
|
|
- <classname>Zend_Db_Table</classname>-based table class. First, create the directory
|
|
|
- <filename>application/models/DbTable/</filename>. Then create and edit a file
|
|
|
- <filename>Guestbook.php</filename> within it, and add the following contents:
|
|
|
+ Wir verwenden ein <ulink
|
|
|
+ url="http://martinfowler.com/eaaCatalog/tableDataGateway.html">Table Data
|
|
|
+ Gateway</ulink> um uns mit unserer Datenquelle zu verbinden;
|
|
|
+ <classname>Zend_Db_Table</classname> bietet diese Funktionalität. Um anzufangen erstellen wir
|
|
|
+ eine <classname>Zend_Db_Table</classname>-basierende Tabellenklasse. Zuerst erstellen wir
|
|
|
+ das Verzeichnis <filename>application/models/DbTable/</filename>. Dann erstellen und
|
|
|
+ bearbeiten wir die Datei <filename>Guestbook.php</filename> in Ihm und fügen die folgenden
|
|
|
+ Inhalte ein:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
// application/models/DbTable/Guestbook.php
|
|
|
|
|
|
/**
|
|
|
- * This is the DbTable class for the guestbook table.
|
|
|
+ * Das ist die DbTable Klasse für die Guestbook Tabelle.
|
|
|
*/
|
|
|
class Default_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
|
|
|
{
|
|
|
- /** Table name */
|
|
|
+ /** Tabellenname */
|
|
|
protected $_name = 'guestbook';
|
|
|
}
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Note the class prefix: <classname>Default_Model_DbTable</classname>. The class prefix
|
|
|
- "Default" from our autoloader is the first segment, and then we have the component,
|
|
|
- "Model_DbTable"; the latter is mapped to the <filename>models/DbTable/</filename> directory
|
|
|
- of the module.
|
|
|
+ Der Klassenpräfix ist zu beachten: <classname>Default_Model_DbTable</classname>. Der
|
|
|
+ Klassenpräfix "Default" von unserem Autoloader ist das erste Segment, und dann haben wir die
|
|
|
+ Komponente "Model_DbTable"; die letztere verweist auf das Verzeichnis
|
|
|
+ <filename>models/DbTable/</filename> des Moduls.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- All that is truly necessary when extending <classname>Zend_Db_Table</classname> is to
|
|
|
- provide a table name and optionally the primary key (if it is not "id").
|
|
|
+ Alles das ist wirklich notwendig wenn <classname>Zend_Db_Table</classname> erweitert wird
|
|
|
+ um einen Tabellennamen anzubieten und optional den primären Schlüssel (wenn es nicht die
|
|
|
+ "id" ist).
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- Now let's create a <ulink url="http://martinfowler.com/eaaCatalog/dataMapper.html">Data
|
|
|
- Mapper</ulink>. A <emphasis>Data Mapper</emphasis> maps a domain object to the database.
|
|
|
- In our case, it will map our model, <classname>Default_Model_Guestbook</classname>, to our
|
|
|
- data source, <classname>Default_Model_DbTable_Guestbook</classname>. A typical API for a
|
|
|
- data mapper is as follows:
|
|
|
+ Jetzt erstellen wir einen <ulink
|
|
|
+ url="http://martinfowler.com/eaaCatalog/dataMapper.html">Daten Mapper</ulink>. Ein
|
|
|
+ <emphasis>Daten Mapper</emphasis> bildet ein Domain Objekt in der Datenbank ab. In unserem
|
|
|
+ Fall bildet es unser Modell <classname>Default_Model_Guestbook</classname> auf unsere
|
|
|
+ Datenquelle, <classname>Default_Model_DbTable_Guestbook</classname>, ab. Eine typische API
|
|
|
+ für einen Daten Mapper ist wie folgt:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
@@ -431,9 +446,10 @@ class Default_Model_GuestbookMapper
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- In addition to these methods, we'll add methods for setting and retrieving the Table Data
|
|
|
- Gateway. The final class, located in
|
|
|
- <filename>application/models/GuestbookMapper.php</filename>, looks like this:
|
|
|
+ Zusätzlich zu diesen Methoden, fügen wir Methoden für das Setzen und Holen des Table Data
|
|
|
+ Gateways hinzu. Die endgültige Klasse, welche unter
|
|
|
+ <filename>application/models/GuestbookMapper.php</filename> platziert ist, sieht wie folgt
|
|
|
+ aus:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
@@ -449,7 +465,7 @@ class Default_Model_GuestbookMapper
|
|
|
$dbTable = new $dbTable();
|
|
|
}
|
|
|
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
|
|
|
- throw new Exception('Invalid table data gateway provided');
|
|
|
+ throw new Exception('Ungültiges Table Data Gateway angegeben');
|
|
|
}
|
|
|
$this->_dbTable = $dbTable;
|
|
|
return $this;
|
|
|
@@ -511,12 +527,12 @@ class Default_Model_GuestbookMapper
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Now it's time to update our model class slightly, to accomodate the data mapper. Just like
|
|
|
- the data mapper contains a reference to the data source, the model contains a reference to
|
|
|
- the data mapper. Additionally, we'll make it easy to populate the model by passing an array
|
|
|
- of data either to the constructor or a <methodname>setOptions()</methodname> method. The
|
|
|
- final model class, located in <filename>application/models/Guestbook.php</filename>, looks
|
|
|
- like this:
|
|
|
+ Jetzt ist es Zeit unsere Modellklasse leicht zu aktualisieren, um den Daten Mapper
|
|
|
+ aufzunehmen. So wie der Daten Mapper eine Referenz zur Datenquelle enthält, enthält das
|
|
|
+ Modell eine Referenz zum Daten Mapper. Zusätzlich machen wir es einfach das Modell
|
|
|
+ bekanntzumachen indem ein Array an Daten entweder an den Constructor oder an die
|
|
|
+ <methodname>setOptions()</methodname> Methode übergeben wird. Das endgültige Modell, welches
|
|
|
+ in <filename>application/models/Guestbook.php</filename> ist, sieht wie folgt aus:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
@@ -541,7 +557,7 @@ class Default_Model_Guestbook
|
|
|
{
|
|
|
$method = 'set' . $name;
|
|
|
if (('mapper' == $name) || !method_exists($this, $method)) {
|
|
|
- throw new Exception('Invalid guestbook property');
|
|
|
+ throw new Exception('Ungültige Guestbook Eigenschaft');
|
|
|
}
|
|
|
$this->$method($value);
|
|
|
}
|
|
|
@@ -550,7 +566,7 @@ class Default_Model_Guestbook
|
|
|
{
|
|
|
$method = 'get' . $name;
|
|
|
if (('mapper' == $name) || !method_exists($this, $method)) {
|
|
|
- throw new Exception('Invalid guestbook property');
|
|
|
+ throw new Exception('Ungültige Guestbook Eigenschaft');
|
|
|
}
|
|
|
return $this->$method();
|
|
|
}
|
|
|
@@ -644,17 +660,17 @@ class Default_Model_Guestbook
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- Lastly, to connect these elements all together, lets create a guestbook controller that will
|
|
|
- both list the entries that are currently inside the database.
|
|
|
+ Letztendlich, um diese Elemente alle zusammen zu verbinden, erstellen wir einen Guestbook
|
|
|
+ Controller der die Einträge auflistet welche aktuell in der Datenbank sind.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- To create a new controller, open a terminal or DOS console, navigate to your project
|
|
|
- directory, and enter the following:
|
|
|
+ Um einen neuen Controller zu erstellen muss ein Terminal oder eine DOS Konsole geöffnet,
|
|
|
+ in das Projektverzeichnis navigiert und das folgende eingegeben werden:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="shell"><![CDATA[
|
|
|
-# Unix-like systems:
|
|
|
+# Unix-artige Systeme:
|
|
|
% zf.sh create controller guestbook
|
|
|
|
|
|
# DOS/Windows:
|
|
|
@@ -662,21 +678,21 @@ C:> zf.bat create controller guestbook
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- This will create a new controller, <classname>GuestbookController</classname>, in
|
|
|
- <filename>application/controllers/GuestbookController.php</filename>, with a single action
|
|
|
- method, <methodname>indexAction()</methodname>. It will also create a view script directory
|
|
|
- for the controller, <filename>application/views/scripts/guestbook/</filename>, with a view
|
|
|
- script for the index action.
|
|
|
+ Das erstellt einen neuen Controller, <classname>GuestbookController</classname>, in
|
|
|
+ <filename>application/controllers/GuestbookController.php</filename> mit einer einzelnen
|
|
|
+ Aktions Methode, <methodname>indexAction()</methodname>. Er erstellt auch ein View Skript
|
|
|
+ Verzeichnis für den Controller, <filename>application/views/scripts/guestbook/</filename>,
|
|
|
+ mit einem View Skript für die Index Aktion.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- We'll use the "index" action as a landing page to view all guestbook entries.
|
|
|
+ Wir verwenden die "index" Aktion als Landeseite um alle Guestbook Einträge anzusehen.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
- Now, let's flesh out the basic application logic. On a hit to
|
|
|
- <methodname>indexAction()</methodname>, we'll display all guestbook entries. This would look
|
|
|
- like the following:
|
|
|
+ Jetzt betrachten wir die grundsätzliche Anwendungslogik. Bei einem Treffer auf
|
|
|
+ <methodname>indexAction()</methodname> zeigen wir alle Guestbook Einträge an. Das würde wie
|
|
|
+ folgt aussehen:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
@@ -693,8 +709,9 @@ class GuestbookController extends Zend_Controller_Action
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- And, of course, we need a view script to go along with that. Edit
|
|
|
- <filename>application/views/scripts/guestbook/index.phtml</filename> to read as follows:
|
|
|
+ Und natürlich benötigen wir ein View Skript um damit weiterzumachen.
|
|
|
+ <filename>application/views/scripts/guestbook/index.phtml</filename> ist zu bearbeiten damit
|
|
|
+ Sie wie folgt aussieht:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
@@ -706,9 +723,9 @@ class GuestbookController extends Zend_Controller_Action
|
|
|
'action' => 'sign'
|
|
|
),
|
|
|
'default',
|
|
|
- true) ?>">Sign Our Guestbook</a></p>
|
|
|
+ true) ?>">Im Guestbook eintragen</a></p>
|
|
|
|
|
|
-Guestbook Entries: <br />
|
|
|
+Guestbook Einträge: <br />
|
|
|
<dl>
|
|
|
<?php foreach ($this->entries as $entry): ?>
|
|
|
<dt><?php echo $this->escape($entry->email) ?></dt>
|
|
|
@@ -721,8 +738,8 @@ Guestbook Entries: <br />
|
|
|
<title>Checkpoint</title>
|
|
|
|
|
|
<para>
|
|
|
- Now browse to "http://localhost/guestbook". You should see the following in your
|
|
|
- browser:
|
|
|
+ Jetzt gehen wir auf "http://localhost/guestbook". Man sollte das folgende im Browser
|
|
|
+ sehen:
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
@@ -732,30 +749,31 @@ Guestbook Entries: <br />
|
|
|
</note>
|
|
|
|
|
|
<note>
|
|
|
- <title>Using the data loader script</title>
|
|
|
+ <title>Das Datenlade Skript verwenden</title>
|
|
|
|
|
|
<para>
|
|
|
- The data loader script introduced in this section
|
|
|
- (<filename>scripts/load.sqlite.php</filename>) can be used to create the database for
|
|
|
- each environment you have defined, as well as to load it with sample data. Internally,
|
|
|
- it utilizes <classname>Zend_Console_Getopt</classname>, which allows it to provide a
|
|
|
- number of command line switches. If you pass the "-h" or "--help" switch, it will give
|
|
|
- you the available options:
|
|
|
+ Das Datenlade Skript welches in diesem Kapitel beschrieben wird
|
|
|
+ (<filename>scripts/load.sqlite.php</filename>) kann verwendet werden um die Datenbank,
|
|
|
+ für jede Umgebung die man definiert hat, zu erstellen sowie Sie mit Beispieldaten zu
|
|
|
+ laden. Intern verwendet es <classname>Zend_Console_Getopt</classname>, was es erlaubt
|
|
|
+ eine Anzahl von Kommandozeilen Schalter anzubieten. Wenn man den "-h" oder "--help"
|
|
|
+ Schalter übergibt, werden die folgenden Optionen angegeben:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
Usage: load.sqlite.php [ options ]
|
|
|
---withdata|-w Load database with sample data
|
|
|
---env|-e [ ] Application environment for which to create database
|
|
|
- (defaults to development)
|
|
|
---help|-h Help -- usage message)]]
|
|
|
+--withdata|-w Datenbank mit einigen Daten laden
|
|
|
+--env|-e [ ] Anwendungsumgebung für welche die Datenbank erstellt wird
|
|
|
+ (Standard ist Development)
|
|
|
+--help|-h Hilfe -- Verwendung)]]
|
|
|
]]></programlisting>
|
|
|
|
|
|
<para>
|
|
|
- The "-e" switch allows you to specify the value to use for the constant
|
|
|
- <constant>APPLICATION_ENV</constant> -- which in turn allows you to create a SQLite
|
|
|
- database for each environment you define. Be sure to run the script for the environment
|
|
|
- you choose for your application when deploying.
|
|
|
+ Der "-e" Schalter erlaubt es den Wert zu spezifizieren der für die Konstante
|
|
|
+ <constant>APPLICATION_ENV</constant> verwendet wird -- welcher es erlaubt eine SQLite
|
|
|
+ Datenbank für jede Umgebung zu erstellen die man definiert. Man sollte sicherstellen
|
|
|
+ dass das Skript für die Umgebung gestartet wird welche man für die eigene Anwendung
|
|
|
+ ausgewählt hat wenn man in Betrieb geht.
|
|
|
</para>
|
|
|
</note>
|
|
|
</sect1>
|