Permalink
Browse files

Initial Doctrine Example Sandbox

  • Loading branch information...
0 parents commit 2bc8d53948ee9f036987d0cf48f8e2ce1e70395c @beberlei committed Jan 30, 2012
@@ -0,0 +1,2 @@
+vendor
+config.yml
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,10 @@
+<?php
+
+require_once "vendor/.composer/autoload.php";
+
+$configFile = __DIR__ . "/config.yml";
+if ( ! file_exists($configFile)) {
+ $configFile = __DIR__ . "/config.yml-dist";
+}
+
+$entityManager = \CarFramework\Application::bootstrap($configFile);
@@ -0,0 +1,17 @@
+{
+ "name": "beberlei/doctrine-example-app",
+ "require": {
+ "doctrine/orm": "*",
+ "symfony/console": "*",
+ "symfony/yaml": "*",
+ "symfony/finder": "*"
+ },
+ "autoload": {
+ "psr-0": {
+ "CarFramework\\Tests": "tests",
+ "CarDealer\\Tests": "tests",
+ "CarFramework": "lib",
+ "CarDealer": "lib"
+ }
+ }
+}
Oops, something went wrong.
@@ -0,0 +1,7 @@
+# Configure the Database here, by default we use SQLite in Memory
+driver: pdo_sqlite
+memory: true
+#driver: pdo_mysql
+#user: root
+#password: ~
+#dbname: exampleapp
@@ -0,0 +1,42 @@
+#!/usr/bin/php
+<?php
+
+use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
+use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
+use Doctrine\ORM\Tools\Console\ConsoleRunner;
+use Symfony\Component\Console\Helper\HelperSet;
+use Symfony\Component\Console\Application;
+use Symfony\Component\Finder\Finder;
+
+require_once __DIR__ . '/bootstrap.php';
+
+$helperSet = new HelperSet(array(
+ 'db' => new ConnectionHelper($entityManager->getConnection()),
+ 'em' => new EntityManagerHelper($entityManager)
+));
+
+$finder = new Finder();
+
+$iterator = $finder
+ ->files()
+ ->name('*Scenario.php')
+ ->in(__DIR__ . '/lib/CarDealer');
+
+foreach ($iterator as $file) {
+ require_once $file->getRealpath();
+}
+
+
+$cli = new Application('Doctrine Command Line Interface', \Doctrine\ORM\Version::VERSION);
+$cli->setCatchExceptions(true);
+$cli->setHelperSet($helperSet);
+
+$classes = get_declared_classes();
+foreach ($classes as $class) {
+ if (is_subclass_of($class, 'CarFramework\\ConsoleScenario')) {
+ $cli->add(new $class($entityManager));
+ }
+}
+
+ConsoleRunner::addCommands($cli);
+$cli->run();
@@ -0,0 +1,111 @@
+<?php
+
+namespace CarDealer\Basic;
+
+/**
+ * @Entity
+ */
+class Vehicle
+{
+ /** @Column(type="integer") @Id @GeneratedValue **/
+ protected $id;
+ /** @Column(type="string") **/
+ protected $offer;
+ /** @Column(type="integer") **/
+ protected $price;
+ /** @Column(type="integer") **/
+ protected $kilometres;
+ /** @Column(type="date") **/
+ protected $admission;
+
+ /**
+ * Get id.
+ *
+ * @return id.
+ */
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ /**
+ * Get offer.
+ *
+ * @return offer.
+ */
+ public function getOffer()
+ {
+ return $this->offer;
+ }
+
+ /**
+ * Set offer.
+ *
+ * @param offer the value to set.
+ */
+ public function setOffer($offer)
+ {
+ $this->offer = $offer;
+ }
+
+ /**
+ * Get price.
+ *
+ * @return price.
+ */
+ public function getPrice()
+ {
+ return $this->price;
+ }
+
+ /**
+ * Set price.
+ *
+ * @param price the value to set.
+ */
+ public function setPrice($price)
+ {
+ $this->price = $price;
+ }
+
+ /**
+ * Get kilometres.
+ *
+ * @return kilometres.
+ */
+ public function getKilometres()
+ {
+ return $this->kilometres;
+ }
+
+ /**
+ * Set kilometres.
+ *
+ * @param kilometres the value to set.
+ */
+ public function setKilometres($kilometres)
+ {
+ $this->kilometres = $kilometres;
+ }
+
+ /**
+ * Get admission.
+ *
+ * @return admission.
+ */
+ public function getAdmission()
+ {
+ return $this->admission;
+ }
+
+ /**
+ * Set admission.
+ *
+ * @param DateTime $admission admission the value to set.
+ */
+ public function setAdmission(\DateTime $admission)
+ {
+ $this->admission = $admission;
+ }
+}
+
@@ -0,0 +1,26 @@
+<?php
+namespace CarDealer\Basic;
+
+use Doctrine\ORM\EntityManager;
+use CarFramework\ConsoleScenario;
+
+class VehicleScenario extends ConsoleScenario
+{
+ public function play(EntityManager $entityManager, $args)
+ {
+ $vehicle = new Vehicle();
+ $vehicle->setOffer("Brand New Audi A8 for just 80.000 €");
+ $vehicle->setPrice(80000);
+ $vehicle->setAdmission(new \DateTime("2012-01-01"));
+ $vehicle->setKilometres(400);
+
+ $entityManager->persist($vehicle);
+ $entityManager->flush();
+ $entityManager->clear();
+
+ $vehicle = $entityManager->find('CarDealer\Basic\Vehicle', $vehicle->getId());
+
+ echo "The price is: " . $vehicle->getPrice() . "\n";
+ }
+}
+
@@ -0,0 +1,27 @@
+<?php
+
+namespace CarFramework;
+
+use Doctrine\ORM\Tools\Setup;
+use Doctrine\ORM\Tools\SchemaTool;
+use Doctrine\ORM\EntityManager;
+use Symfony\Component\Yaml\Yaml;
+
+class Application
+{
+ public static function bootstrap($configFile)
+ {
+ $dbParams = Yaml::parse($configFile);
+ $isDevMode = true;
+
+ $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/../CarDealer"), $isDevMode);
+ $entityManager = EntityManager::create($dbParams, $config);
+
+ if ($dbParams['driver'] == "pdo_sqlite" && isset($dbParams['memory'])) {
+ $schemaTool = new SchemaTool($entityManager);
+ $schemaTool->createSchema($entityManager->getMetadataFactory()->getAllMetadata());
+ }
+
+ return $entityManager;
+ }
+}
@@ -0,0 +1,22 @@
+<?php
+
+namespace CarFramework;
+
+class Application
+{
+ public static bootstrap($configFile)
+ {
+ $dbParams = Yaml::parse($configFile);
+ $isDevMode = true;
+
+ $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/lib/CarDealer"), $isDevMode);
+ $entityManager = EntityManager::create($dbParams, $config);
+
+ if ($dbParams['driver'] == "pdo_sqlite" && isset($dbParams['memory'])) {
+ $schemaTool = new SchemaTool($entityManager);
+ $schemaTool->createSchema($entityManager->getMetadataFactory()->getAllMetadata());
+ }
+
+ return $entityManager;
+ }
+}
@@ -0,0 +1,46 @@
+<?php
+
+namespace CarFramework;
+
+use Doctrine\DBAL\Logging\SQLLogger;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class ConsoleSQLLogger implements SQLLogger
+{
+ /**
+ * @var array
+ */
+ private $queries = array();
+
+ public function startQuery($sql, array $params = null, array $types = null)
+ {
+ $this->queries[] = array('sql' => $sql, 'params' => $params, 'types' => $types, 'start' => microtime(true));
+ }
+
+ public function stopQuery()
+ {
+ $this->queries[count($this->queries)-1]['diff'] =
+ number_format(microtime(true) - $this->queries[count($this->queries)-1]['start'], 6);
+ }
+
+ public function flush(OutputInterface $output)
+ {
+ $output->writeln('');
+ $output->writeln('');
+ foreach ($this->queries as $query) {
+ $output->writeln(sprintf("Query: <comment>%s</comment>\n Took <info>%s</info> seconds.", $query['sql'], $query['diff']));
+ foreach ($query['params'] as $idx => $param) {
+ $type = (isset($query['types'][$idx])) ?$query['types'][$idx] : "unknown";
+ if (is_object($param) && ! method_exists($param, '__toString')) {
+ if ($param instanceof \DateTime) {
+ $param = "<DateTime:".$param->format('Y-m-d H:i:s') . ">";
+ } else {
+ $param = "<Object:".get_class($param).">";
+ }
+ }
+ $output->writeln(sprintf(' * %s: %s', $type, substr(str_replace(array("\r", "\n"), " ", $param), 0, 100)));
+ }
+ }
+ }
+}
+
Oops, something went wrong.

0 comments on commit 2bc8d53

Please sign in to comment.