Permalink
Browse files

Initial code

  • Loading branch information...
0 parents commit 7c0c57c188d1abd29826d7749067a7374d010a3e @beberlei beberlei committed Aug 27, 2011
@@ -0,0 +1 @@
+db.sqlite
@@ -0,0 +1,5 @@
+This is the code for the Doctrine 2 ORM tutorial.
+
+You can find the tutorial at:
+
+http://www.doctrine-project.org/docs/orm/2.1/en/tutorials/getting-started-xml-edition.html
@@ -0,0 +1,12 @@
+<?php
+// boostrap.php
+
+require_once "entities/User.php";
+require_once "entities/Product.php";
+require_once "entities/Bug.php";
+
+if (!class_exists("Doctrine\Common\Version", false)) {
+ require_once "bootstrap_doctrine.php";
+}
+
+require_once "repositories/BugRepository.php";
@@ -0,0 +1,24 @@
+<?php
+// bootstrap_doctrine.php
+
+// See :doc:`Configuration <../reference/configuration>` for up to date autoloading details.
+use Doctrine\ORM\Tools\Setup;
+
+require_once "Doctrine\ORM\Tools\Setup.php";
+Setup::registerAutoloadPEAR();
+
+// Create a simple "default" Doctrine ORM configuration for XML Mapping
+$isDevMode = true;
+$config = Setup::createXMLMetadataConfiguration(__DIR__."/config/mappings/xml", $isDevMode);
+// or if you prefer yaml or annotations
+//$config = Setup::createAnnotationMetadataConfiguration(__DIR__."/entities", $isDevMode);
+//$config = Setup::createYAMLMetadataConfiguration(__DIR__."/config/mappings/yml", $isDevMode);
+
+// database configuration parameters
+$conn = array(
+ 'driver' => 'pdo_sqlite',
+ 'path' => __DIR__ . '/db.sqlite',
+);
+
+// obtaining the entity manager
+$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
@@ -0,0 +1,24 @@
+<?php
+// bootstrap_doctrine.php
+
+// See :doc:`Configuration <../reference/configuration>` for up to date autoloading details.
+use Doctrine\ORM\Tools\Setup;
+
+require_once "Doctrine/ORM/Tools/Setup.php";
+Setup::registerAutoloadPEAR();
+
+// Create a simple "default" Doctrine ORM configuration for XML Mapping
+$isDevMode = true;
+$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
+// or if you prefer yaml or annotations
+//$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/entities"), $isDevMode);
+//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);
+
+// database configuration parameters
+$conn = array(
+ 'driver' => 'pdo_sqlite',
+ 'path' => __DIR__ . '/db.sqlite',
+);
+
+// obtaining the entity manager
+$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
@@ -0,0 +1,7 @@
+<?php
+// cli-config.php
+require_once "bootstrap.php";
+
+$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
+ 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($entityManager)
+));
@@ -0,0 +1,10 @@
+<?php
+// close_bug.php
+require_once "bootstrap.php";
+
+$theBugId = $argv[1];
+
+$bug = $entityManager->find("Bug", (int)$theBugId);
+$bug->close();
+
+$entityManager->flush();
@@ -0,0 +1,21 @@
+<!-- config/xml/Bug.dcm.xml -->
+ <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
+ http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
+
+ <entity name="Bug" table="bugs" repository-class="BugRepository">
+ <id name="id" type="integer">
+ <generator strategy="AUTO" />
+ </id>
+
+ <field name="description" type="text" />
+ <field name="created" type="datetime" />
+ <field name="status" type="string" />
+
+ <many-to-one target-entity="User" field="reporter" inversed-by="reportedBugs" />
+ <many-to-one target-entity="User" field="engineer" inversed-by="assignedBugs" />
+
+ <many-to-many target-entity="Product" field="products" />
+ </entity>
+ </doctrine-mapping>
@@ -0,0 +1,13 @@
+<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
+ http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
+
+ <entity name="Product" table="products">
+ <id name="id" type="integer">
+ <generator strategy="AUTO" />
+ </id>
+
+ <field name="name" type="string" />
+ </entity>
+ </doctrine-mapping>
@@ -0,0 +1,17 @@
+<!-- config/xml/User.dcm.xml -->
+ <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
+ http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
+
+ <entity name="User" table="users">
+ <id name="id" type="integer">
+ <generator strategy="AUTO" />
+ </id>
+
+ <field name="name" type="string" />
+
+ <one-to-many target-entity="Bug" field="reportedBugs" mapped-by="reporter" />
+ <one-to-many target-entity="Bug" field="assignedBugs" mapped-by="engineer" />
+ </entity>
+ </doctrine-mapping>
@@ -0,0 +1,26 @@
+Bug:
+ type: entity
+ table: bugs
+ repositoryClass: BugRepository
+ id:
+ id:
+ type: integer
+ generator:
+ strategy: AUTO
+ fields:
+ description:
+ type: text
+ created:
+ type: datetime
+ status:
+ type: string
+ manyToOne:
+ reporter:
+ targetEntity: User
+ inversedBy: reportedBugs
+ engineer:
+ targetEntity: User
+ inversedBy: assignedBugs
+ manyToMany:
+ products:
+ targetEntity: Product
@@ -0,0 +1,11 @@
+Product:
+ type: entity
+ table: products
+ id:
+ id:
+ type: integer
+ generator:
+ strategy: AUTO
+ fields:
+ name:
+ type: string
@@ -0,0 +1,18 @@
+User:
+ type: entity
+ table: users
+ id:
+ id:
+ type: integer
+ generator:
+ strategy: AUTO
+ fields:
+ name:
+ type: string
+ oneToMany:
+ reportedBugs:
+ targetEntity: Bug
+ mappedBy: reporter
+ assignedBugs:
+ targetEntity: Bug
+ mappedBy: engineer
@@ -0,0 +1,32 @@
+<?php
+// create_bug.php
+require_once "bootstrap.php";
+
+$theReporterId = $argv[1];
+$theDefaultEngineerId = (isset($argv[2])) ? $argv[2] : 1;
+$productIds = explode(",", $argv[3]);
+
+$reporter = $entityManager->find("User", $theReporterId);
+$engineer = $entityManager->find("User", $theDefaultEngineerId);
+if (!$reporter || !$engineer) {
+ echo "No reporter and/or engineer found for the input.\n";
+ exit(1);
+}
+
+$bug = new Bug();
+$bug->setDescription("Something does not work!");
+$bug->setCreated(new DateTime("now"));
+$bug->setStatus("OPEN");
+
+foreach ($productIds AS $productId) {
+ $product = $entityManager->find("Product", $productId);
+ $bug->assignToProduct($product);
+}
+
+$bug->setReporter($reporter);
+$bug->setEngineer($engineer);
+
+$entityManager->persist($bug);
+$entityManager->flush();
+
+echo "Your new Bug Id: ".$bug->getId()."\n";
@@ -0,0 +1,13 @@
+<?php
+// create_product.php
+require_once "bootstrap.php";
+
+$newProductName = $argv[1];
+
+$product = new Product();
+$product->setName($newProductName);
+
+$entityManager->persist($product);
+$entityManager->flush();
+
+echo "Created Product with ID " . $product->getId() . "\n";
@@ -0,0 +1,13 @@
+<?php
+// create_user.php
+require_once "bootstrap.php";
+
+$newUsername = $argv[1];
+
+$user = new User();
+$user->setName($newUsername);
+
+$entityManager->persist($user);
+$entityManager->flush();
+
+echo "Created User with ID " . $user->getId() . "\n";
@@ -0,0 +1,19 @@
+<?php
+// dashboard.php
+require_once "bootstrap.php";
+
+$theUserId = $argv[1];
+
+$dql = "SELECT b, e, r FROM Bug b JOIN b.engineer e JOIN b.reporter r ".
+ "WHERE b.status = 'OPEN' AND (e.id = ?1 OR r.id = ?1) ORDER BY b.created DESC";
+
+$myBugs = $entityManager->createQuery($dql)
+ ->setParameter(1, $theUserId)
+ ->setMaxResults(15)
+ ->getResult();
+
+echo "You have created or assigned to " . count($myBugs) . " open bugs:\n\n";
+
+foreach ($myBugs AS $bug) {
+ echo $bug->getId() . " - " . $bug->getDescription()."\n";
+}
Oops, something went wrong.

0 comments on commit 7c0c57c

Please sign in to comment.