Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial code

  • Loading branch information...
commit 7c0c57c188d1abd29826d7749067a7374d010a3e 0 parents
@beberlei beberlei authored
1  .gitignore
@@ -0,0 +1 @@
+db.sqlite
5 README
@@ -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
12 bootstrap.php
@@ -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";
24 bootstrap_config.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);
24 bootstrap_doctrine.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(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);
7 cli-config.php
@@ -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)
+));
10 close_bug.php
@@ -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();
21 config/xml/Bug.dcm.xml
@@ -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>
13 config/xml/Product.dcm.xml
@@ -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>
17 config/xml/User.dcm.xml
@@ -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>
26 config/yaml/Bug.dcm.yml
@@ -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
11 config/yaml/Product.dcm.yml
@@ -0,0 +1,11 @@
+Product:
+ type: entity
+ table: products
+ id:
+ id:
+ type: integer
+ generator:
+ strategy: AUTO
+ fields:
+ name:
+ type: string
18 config/yaml/User.dcm.yml
@@ -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
32 create_bug.php
@@ -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";
13 create_product.php
@@ -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";
13 create_user.php
@@ -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";
19 dashboard.php
@@ -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";
+}
119 entities/Bug.php
@@ -0,0 +1,119 @@
+<?php
+use Doctrine\Common\Collections\ArrayCollection;
+
+/**
+ * @Entity(repositoryClass="BugRepository") @Table(name="bugs")
+ */
+class Bug
+{
+ /**
+ * @Id @Column(type="integer") @GeneratedValue
+ */
+ protected $id;
+ /**
+ * @Column(type="string")
+ */
+ protected $description;
+ /**
+ * @Column(type="datetime")
+ */
+ protected $created;
+ /**
+ * @Column(type="string")
+ */
+ protected $status;
+
+ /**
+ * @ManyToOne(targetEntity="User", inversedBy="assignedBugs")
+ */
+ protected $engineer;
+
+ /**
+ * @ManyToOne(targetEntity="User", inversedBy="reportedBugs")
+ */
+ protected $reporter;
+
+ /**
+ * @ManyToMany(targetEntity="Product")
+ */
+ protected $products;
+
+ public function __construct()
+ {
+ $this->products = new ArrayCollection();
+ }
+
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ public function getDescription()
+ {
+ return $this->description;
+ }
+
+ public function setDescription($description)
+ {
+ $this->description = $description;
+ }
+
+ public function setCreated(DateTime $created)
+ {
+ $this->created = $created;
+ }
+
+ public function getCreated()
+ {
+ return $this->created;
+ }
+
+ public function setStatus($status)
+ {
+ $this->status = $status;
+ }
+
+ public function getStatus()
+ {
+ return $this->status;
+ }
+
+ public function setEngineer($engineer)
+ {
+ $engineer->assignedToBug($this);
+ $this->engineer = $engineer;
+ }
+
+ public function setReporter($reporter)
+ {
+ $reporter->addReportedBug($this);
+ $this->reporter = $reporter;
+ }
+
+ public function getEngineer()
+ {
+ return $this->engineer;
+ }
+
+ public function getReporter()
+ {
+ return $this->reporter;
+ }
+
+ public function assignToProduct($product)
+ {
+ $this->products[] = $product;
+ }
+
+ public function getProducts()
+ {
+ return $this->products;
+ }
+
+ public function close()
+ {
+ $this->status = "CLOSE";
+ }
+}
+
+
26 entities/Product.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * @Entity @Table(name="products")
+ */
+class Product
+{
+ /** @Id @Column(type="integer") @GeneratedValue */
+ protected $id;
+ /** @Column(type="string") */
+ protected $name;
+
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+}
64 entities/User.php
@@ -0,0 +1,64 @@
+<?php
+
+use Doctrine\Common\Collections\ArrayCollection;
+
+/**
+ * @Entity @Table(name="users")
+ */
+class User
+{
+ /**
+ * @Id @GeneratedValue @Column(type="integer")
+ * @var string
+ */
+ protected $id;
+
+ /**
+ * @Column(type="string")
+ * @var string
+ */
+ protected $name;
+
+ /**
+ * @OneToMany(targetEntity="Bug", mappedBy="reporter")
+ * @var Bug[]
+ */
+ protected $reportedBugs = null;
+
+ /**
+ * @OneToMany(targetEntity="Bug", mappedBy="engineer")
+ * @var Bug[]
+ */
+ protected $assignedBugs = null;
+
+ public function __construct()
+ {
+ $this->reportedBugs = new ArrayCollection();
+ $this->assignedBugs = new ArrayCollection();
+ }
+
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ public function addReportedBug($bug)
+ {
+ $this->reportedBugs[] = $bug;
+ }
+
+ public function assignedToBug($bug)
+ {
+ $this->assignedBugs[] = $bug;
+ }
+}
19 list_bugs.php
@@ -0,0 +1,19 @@
+<?php
+// list_bugs.php
+require_once "bootstrap.php";
+
+$dql = "SELECT b, e, r FROM Bug b JOIN b.engineer e JOIN b.reporter r ORDER BY b.created DESC";
+
+$query = $entityManager->createQuery($dql);
+$query->setMaxResults(30);
+$bugs = $query->getResult();
+
+foreach($bugs AS $bug) {
+ echo $bug->getDescription()." - ".$bug->getCreated()->format('d.m.Y')."\n";
+ echo " Reported by: ".$bug->getReporter()->getName()."\n";
+ echo " Assigned to: ".$bug->getEngineer()->getName()."\n";
+ foreach($bug->getProducts() AS $product) {
+ echo " Platform: ".$product->getName()."\n";
+ }
+ echo "\n";
+}
15 list_bugs_repository.php
@@ -0,0 +1,15 @@
+<?php
+// list_bugs_repository.php
+require_once "bootstrap.php";
+
+$bugs = $entityManager->getRepository('Bug')->getRecentBugs();
+
+foreach($bugs AS $bug) {
+ echo $bug->getDescription()." - ".$bug->getCreated()->format('d.m.Y')."\n";
+ echo " Reported by: ".$bug->getReporter()->getName()."\n";
+ echo " Assigned to: ".$bug->getEngineer()->getName()."\n";
+ foreach($bug->getProducts() AS $product) {
+ echo " Platform: ".$product->getName()."\n";
+ }
+ echo "\n";
+}
11 products.php
@@ -0,0 +1,11 @@
+<?php
+// products.php
+require_once "bootstrap.php";
+
+$dql = "SELECT p.id, p.name, count(b.id) AS openBugs FROM Bug b ".
+ "JOIN b.products p WHERE b.status = 'NEW' GROUP BY p.id";
+$productBugs = $entityManager->createQuery($dql)->getScalarResult();
+
+foreach($productBugs as $productBug) {
+ echo $productBug['name']." has " . $productBug['openBugs'] . " open bugs!\n";
+}
43 repositories/BugRepository.php
@@ -0,0 +1,43 @@
+<?php
+// repositories/BugRepository.php
+
+use Doctrine\ORM\EntityRepository;
+
+class BugRepository extends EntityRepository
+{
+ public function getRecentBugs($number = 30)
+ {
+ $dql = "SELECT b, e, r FROM Bug b JOIN b.engineer e JOIN b.reporter r ORDER BY b.created DESC";
+
+ $query = $this->getEntityManager()->createQuery($dql);
+ $query->setMaxResults($number);
+ return $query->getResult();
+ }
+
+ public function getRecentBugsArray($number = 30)
+ {
+ $dql = "SELECT b, e, r, p FROM Bug b JOIN b.engineer e ".
+ "JOIN b.reporter r JOIN b.products p ORDER BY b.created DESC";
+ $query = $this->getEntityManager()->createQuery($dql);
+ $query->setMaxResults($number);
+ return $query->getArrayResult();
+ }
+
+ public function getUsersBugs($userId, $number = 15)
+ {
+ $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";
+
+ return $this->getEntityManager()->createQuery($dql)
+ ->setParameter(1, $userId)
+ ->setMaxResults($number)
+ ->getResult();
+ }
+
+ public function getOpenBugsByProduct()
+ {
+ $dql = "SELECT p.id, p.name, count(b.id) AS openBugs FROM Bug b ".
+ "JOIN b.products p WHERE b.status = 'OPEN' GROUP BY p.id";
+ return $this->getEntityManager()->createQuery($dql)->getScalarResult();
+ }
+}
10 show_bug.php
@@ -0,0 +1,10 @@
+<?php
+// show_bug.php
+require_once "bootstrap.php";
+
+$theBugId = $argv[1];
+
+$bug = $entityManager->find("Bug", (int)$theBugId);
+
+echo "Bug: ".$bug->getDescription()."\n";
+echo "Engineer: ".$bug->getEngineer()->getName()."\n";
Please sign in to comment.
Something went wrong with that request. Please try again.