├── .gitignore
├── README
├── bootstrap.php
├── cli-config.php
├── close_bug.php
├── composer.json
├── config
├── xml
│ ├── Bug.dcm.xml
│ ├── Product.dcm.xml
│ └── User.dcm.xml
└── yaml
│ ├── Bug.dcm.yml
│ ├── Product.dcm.yml
│ └── User.dcm.yml
├── create_bug.php
├── create_product.php
├── create_user.php
├── dashboard.php
├── list_bugs.php
├── list_bugs_array.php
├── list_bugs_repository.php
├── list_products.php
├── products.php
├── show_bug.php
├── show_product.php
├── src
├── Bug.php
├── BugRepository.php
├── Product.php
└── User.php
└── update_product.php
/.gitignore:
--------------------------------------------------------------------------------
1 | db.sqlite
2 | vendor
3 | composer.lock
4 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | This is the code for the Doctrine 2 ORM tutorial.
2 |
3 | Last Update: Tutorial is now using Composer only
4 |
5 | You can find the tutorial at:
6 |
7 | https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/getting-started.html
8 |
9 | ## Installation:
10 |
11 | ```bash
12 | $ git clone https://github.com/doctrine/doctrine2-orm-tutorial.git # or: git clone git@github.com:doctrine/doctrine2-orm-tutorial.git
13 | $ cd doctrine2-orm-tutorial
14 | $ composer install
15 | ```
16 |
--------------------------------------------------------------------------------
/bootstrap.php:
--------------------------------------------------------------------------------
1 | 'pdo_sqlite',
16 | 'path' => __DIR__ . '/db.sqlite',
17 | );
18 |
19 | // obtaining the entity manager
20 | $entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
21 |
--------------------------------------------------------------------------------
/cli-config.php:
--------------------------------------------------------------------------------
1 | new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($entityManager)
7 | ));
8 |
9 | return $helperSet;
10 |
--------------------------------------------------------------------------------
/close_bug.php:
--------------------------------------------------------------------------------
1 | find("Bug", (int)$theBugId);
8 | $bug->close();
9 |
10 | $entityManager->flush();
11 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "require": {
3 | "php": ">=5.4",
4 | "ext-pdo_sqlite": "*",
5 | "doctrine/orm": "2.*",
6 | "symfony/yaml": "2.*"
7 | },
8 | "autoload": {
9 | "psr-0": {
10 | "": "src/"
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/config/xml/Bug.dcm.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/config/xml/Product.dcm.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/config/xml/User.dcm.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/config/yaml/Bug.dcm.yml:
--------------------------------------------------------------------------------
1 | Bug:
2 | type: entity
3 | table: bugs
4 | repositoryClass: BugRepository
5 | id:
6 | id:
7 | type: integer
8 | generator:
9 | strategy: AUTO
10 | fields:
11 | description:
12 | type: text
13 | created:
14 | type: datetime
15 | status:
16 | type: string
17 | manyToOne:
18 | reporter:
19 | targetEntity: User
20 | inversedBy: reportedBugs
21 | engineer:
22 | targetEntity: User
23 | inversedBy: assignedBugs
24 | manyToMany:
25 | products:
26 | targetEntity: Product
27 |
--------------------------------------------------------------------------------
/config/yaml/Product.dcm.yml:
--------------------------------------------------------------------------------
1 | Product:
2 | type: entity
3 | table: products
4 | id:
5 | id:
6 | type: integer
7 | generator:
8 | strategy: AUTO
9 | fields:
10 | name:
11 | type: string
12 |
--------------------------------------------------------------------------------
/config/yaml/User.dcm.yml:
--------------------------------------------------------------------------------
1 | User:
2 | type: entity
3 | table: users
4 | id:
5 | id:
6 | type: integer
7 | generator:
8 | strategy: AUTO
9 | fields:
10 | name:
11 | type: string
12 | oneToMany:
13 | reportedBugs:
14 | targetEntity: Bug
15 | mappedBy: reporter
16 | assignedBugs:
17 | targetEntity: Bug
18 | mappedBy: engineer
19 |
--------------------------------------------------------------------------------
/create_bug.php:
--------------------------------------------------------------------------------
1 | find("User", $theReporterId);
10 | $engineer = $entityManager->find("User", $theDefaultEngineerId);
11 | if (!$reporter || !$engineer) {
12 | echo "No reporter and/or engineer found for the input.\n";
13 | exit(1);
14 | }
15 |
16 | $bug = new Bug();
17 | $bug->setDescription("Something does not work!");
18 | $bug->setCreated(new DateTime("now"));
19 | $bug->setStatus("OPEN");
20 |
21 | foreach ($productIds AS $productId) {
22 | $product = $entityManager->find("Product", $productId);
23 | if (!$product) {
24 | echo "No product found for the input.\n";
25 | exit(1);
26 | }
27 | $bug->assignToProduct($product);
28 | }
29 |
30 | $bug->setReporter($reporter);
31 | $bug->setEngineer($engineer);
32 |
33 | $entityManager->persist($bug);
34 | $entityManager->flush();
35 |
36 | echo "Your new Bug Id: ".$bug->getId()."\n";
37 |
--------------------------------------------------------------------------------
/create_product.php:
--------------------------------------------------------------------------------
1 | setName($newProductName);
9 |
10 | $entityManager->persist($product);
11 | $entityManager->flush();
12 |
13 | echo "Created Product with ID " . $product->getId() . "\n";
14 |
--------------------------------------------------------------------------------
/create_user.php:
--------------------------------------------------------------------------------
1 | setName($newUsername);
9 |
10 | $entityManager->persist($user);
11 | $entityManager->flush();
12 |
13 | echo "Created User with ID " . $user->getId() . "\n";
14 |
--------------------------------------------------------------------------------
/dashboard.php:
--------------------------------------------------------------------------------
1 | createQuery($dql)
11 | ->setParameter(1, $theUserId)
12 | ->setMaxResults(15)
13 | ->getResult();
14 |
15 | echo "You have created or assigned to " . count($myBugs) . " open bugs:\n\n";
16 |
17 | foreach ($myBugs AS $bug) {
18 | echo $bug->getId() . " - " . $bug->getDescription()."\n";
19 | }
20 |
--------------------------------------------------------------------------------
/list_bugs.php:
--------------------------------------------------------------------------------
1 | createQuery($dql);
8 | $query->setMaxResults(30);
9 | $bugs = $query->getResult();
10 |
11 | foreach($bugs AS $bug) {
12 | echo $bug->getDescription()." - ".$bug->getCreated()->format('d.m.Y')."\n";
13 | echo " Reported by: ".$bug->getReporter()->getName()."\n";
14 | echo " Assigned to: ".$bug->getEngineer()->getName()."\n";
15 | foreach($bug->getProducts() AS $product) {
16 | echo " Platform: ".$product->getName()."\n";
17 | }
18 | echo "\n";
19 | }
20 |
--------------------------------------------------------------------------------
/list_bugs_array.php:
--------------------------------------------------------------------------------
1 | createQuery($dql);
8 | $bugs = $query->getArrayResult();
9 |
10 | foreach ($bugs as $bug) {
11 | echo $bug['description'] . " - " . $bug['created']->format('d.m.Y')."\n";
12 | echo " Reported by: ".$bug['reporter']['name']."\n";
13 | echo " Assigned to: ".$bug['engineer']['name']."\n";
14 | foreach ($bug['products'] as $product) {
15 | echo " Platform: ".$product['name']."\n";
16 | }
17 | echo "\n";
18 | }
19 |
--------------------------------------------------------------------------------
/list_bugs_repository.php:
--------------------------------------------------------------------------------
1 | getRepository('Bug')->getRecentBugs();
6 |
7 | foreach($bugs AS $bug) {
8 | echo $bug->getDescription()." - ".$bug->getCreated()->format('d.m.Y')."\n";
9 | echo " Reported by: ".$bug->getReporter()->getName()."\n";
10 | echo " Assigned to: ".$bug->getEngineer()->getName()."\n";
11 | foreach($bug->getProducts() AS $product) {
12 | echo " Platform: ".$product->getName()."\n";
13 | }
14 | echo "\n";
15 | }
16 |
--------------------------------------------------------------------------------
/list_products.php:
--------------------------------------------------------------------------------
1 | getRepository('Product');
6 | $products = $productRepository->findAll();
7 |
8 | foreach ($products as $product) {
9 | echo sprintf("-%s\n", $product->getName());
10 | }
11 |
--------------------------------------------------------------------------------
/products.php:
--------------------------------------------------------------------------------
1 | createQuery($dql)->getScalarResult();
8 |
9 | foreach($productBugs as $productBug) {
10 | echo $productBug['name']." has " . $productBug['openBugs'] . " open bugs!\n";
11 | }
12 |
--------------------------------------------------------------------------------
/show_bug.php:
--------------------------------------------------------------------------------
1 | find("Bug", (int)$theBugId);
8 |
9 | echo "Bug: ".$bug->getDescription()."\n";
10 | echo "Engineer: ".$bug->getEngineer()->getName()."\n";
11 |
--------------------------------------------------------------------------------
/show_product.php:
--------------------------------------------------------------------------------
1 |
3 | require_once "bootstrap.php";
4 |
5 | $id = $argv[1];
6 | $product = $entityManager->find('Product', $id);
7 |
8 | if ($product === null) {
9 | echo "No product found.\n";
10 | exit(1);
11 | }
12 |
13 | echo sprintf("-%s\n", $product->getName());
14 |
--------------------------------------------------------------------------------
/src/Bug.php:
--------------------------------------------------------------------------------
1 | products = new ArrayCollection();
44 | }
45 |
46 | public function getId()
47 | {
48 | return $this->id;
49 | }
50 |
51 | public function getDescription()
52 | {
53 | return $this->description;
54 | }
55 |
56 | public function setDescription($description)
57 | {
58 | $this->description = $description;
59 | }
60 |
61 | public function setCreated(DateTime $created)
62 | {
63 | $this->created = $created;
64 | }
65 |
66 | public function getCreated()
67 | {
68 | return $this->created;
69 | }
70 |
71 | public function setStatus($status)
72 | {
73 | $this->status = $status;
74 | }
75 |
76 | public function getStatus()
77 | {
78 | return $this->status;
79 | }
80 |
81 | public function setEngineer($engineer)
82 | {
83 | $engineer->assignedToBug($this);
84 | $this->engineer = $engineer;
85 | }
86 |
87 | public function setReporter($reporter)
88 | {
89 | $reporter->addReportedBug($this);
90 | $this->reporter = $reporter;
91 | }
92 |
93 | public function getEngineer()
94 | {
95 | return $this->engineer;
96 | }
97 |
98 | public function getReporter()
99 | {
100 | return $this->reporter;
101 | }
102 |
103 | public function assignToProduct($product)
104 | {
105 | $this->products[] = $product;
106 | }
107 |
108 | public function getProducts()
109 | {
110 | return $this->products;
111 | }
112 |
113 | public function close()
114 | {
115 | $this->status = "CLOSE";
116 | }
117 | }
118 |
119 |
120 |
--------------------------------------------------------------------------------
/src/BugRepository.php:
--------------------------------------------------------------------------------
1 | getEntityManager()->createQuery($dql);
13 | $query->setMaxResults($number);
14 | return $query->getResult();
15 | }
16 |
17 | public function getRecentBugsArray($number = 30)
18 | {
19 | $dql = "SELECT b, e, r, p FROM Bug b JOIN b.engineer e ".
20 | "JOIN b.reporter r JOIN b.products p ORDER BY b.created DESC";
21 | $query = $this->getEntityManager()->createQuery($dql);
22 | $query->setMaxResults($number);
23 | return $query->getArrayResult();
24 | }
25 |
26 | public function getUsersBugs($userId, $number = 15)
27 | {
28 | $dql = "SELECT b, e, r FROM Bug b JOIN b.engineer e JOIN b.reporter r ".
29 | "WHERE b.status = 'OPEN' AND e.id = ?1 OR r.id = ?1 ORDER BY b.created DESC";
30 |
31 | return $this->getEntityManager()->createQuery($dql)
32 | ->setParameter(1, $userId)
33 | ->setMaxResults($number)
34 | ->getResult();
35 | }
36 |
37 | public function getOpenBugsByProduct()
38 | {
39 | $dql = "SELECT p.id, p.name, count(b.id) AS openBugs FROM Bug b ".
40 | "JOIN b.products p WHERE b.status = 'OPEN' GROUP BY p.id";
41 | return $this->getEntityManager()->createQuery($dql)->getScalarResult();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/Product.php:
--------------------------------------------------------------------------------
1 | id;
15 | }
16 |
17 | public function getName()
18 | {
19 | return $this->name;
20 | }
21 |
22 | public function setName($name)
23 | {
24 | $this->name = $name;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/User.php:
--------------------------------------------------------------------------------
1 | reportedBugs = new ArrayCollection();
37 | $this->assignedBugs = new ArrayCollection();
38 | }
39 |
40 | public function getId()
41 | {
42 | return $this->id;
43 | }
44 |
45 | public function getName()
46 | {
47 | return $this->name;
48 | }
49 |
50 | public function setName($name)
51 | {
52 | $this->name = $name;
53 | }
54 |
55 | public function addReportedBug($bug)
56 | {
57 | $this->reportedBugs[] = $bug;
58 | }
59 |
60 | public function assignedToBug($bug)
61 | {
62 | $this->assignedBugs[] = $bug;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/update_product.php:
--------------------------------------------------------------------------------
1 |
3 | require_once "bootstrap.php";
4 |
5 | $id = $argv[1];
6 | $newName = $argv[2];
7 |
8 | $product = $entityManager->find('Product', $id);
9 |
10 | if ($product === null) {
11 | echo "Product $id does not exist.\n";
12 | exit(1);
13 | }
14 |
15 | $product->setName($newName);
16 |
17 | $entityManager->flush();
18 |
--------------------------------------------------------------------------------