Commit a342ab97 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Setup to run the demo code:
create a folder for the working files for this course /new/path/to/working/files
do a recursive copy from /path/to/working/files/* to /new/path/to/working/files
cd /new/path/to/working/files/
update composer.json as required
run composer install
unzip /path/to/working/files/chapter1/public.zip into /new/path/to/working/files
(NOTE: this should create a directory /new/path/to/working/files/public + populate it with CSS, images, etc.)
create a directory /new/path/to/working/files/data
create 2 subdirectories data/cache and data/proxy
create a MySQL database "sweetscomplete"
create a database use "test" with a password of "password" with rights to "sweetscomplete"
restore from the file /path/to/working/files/chapter1/sweetscomplete.sql
login names + password files are in /path/to/working/files/chapter1/passwords.txt
For each chapter XXX:
cd /new/path/to/working/files/chapterXXX
create a symbolic link between /path/to/working/files/vendor and /new/path/to/working/files/chapterXXX/vendor
create a symbolic link between /path/to/working/files/public and /new/path/to/working/files/chapterXXX/public
create a symbolic link between /path/to/working/files/data and /new/path/to/working/files/chapterXXX/data
run php -S localhost:8080
from your browser: http://localhost:8080/
{
"name": "unlikelysource.com/doctrine-training",
"description": "Sample composer.json file for doctrine training",
"version": "0.0.1",
"type": "library",
"homepage": "http://unlikelysource.com/",
"time": "2015-04-01",
"license": "MIT",
"authors": [
{
"name": "Doug Bierer",
"email": "doug@unlikelysource.com",
"homepage": "http://unlikelysource.com/",
"role": "Developer"
}
],
"require": {
"doctrine/orm": "*",
"symfony/yaml": "*",
"doctrine/mongodb-odm": "dev-master",
"doctrine/couchdb": "dev-master",
"doctrine/couchdb-odm": "dev-master"
}
}
+-------------------------------+------------------+
| email | password |
+-------------------------------+------------------+
| conrad.perry@fastmedia.com | listened8591uncl |
| lonnie.knapp@cablecom.com | on1432God |
| darrel.roman@southcom.net | impaled1933by |
| morgan.avila@northmedia.com | spot9723be |
| lee.mccray@southnet.com | new5220and |
| spencer.sanford@cablenet.net | is3300for |
| thomas.kirby@southcom.com | sighed1323cut |
| brian.crawford@fastcom.net | it1592a |
| armando.barlow@cablecom.com | old3103said |
| jess.rocha@eastcom.net | behind4206Her |
| felix.blevins@southcom.net | Terrible7767a |
| jose.carter@westcom.net | asked3261of |
| orlando.fulton@northcom.net | wind3692water |
| mitchell.roth@eastnet.com | and2675withdrawn |
| eduardo.wright@fastmedia.com | to5023a |
| marc.ellis@westnet.com | the335the |
| joaquin.moses@eastcom.com | with446it |
| morris.vargas@fastnet.com | huge9477at |
| gene.cruz@eastcom.com | the7222from |
| samuel.harding@southmedia.net | pope5267made |
| lauri.grimes@cablecom.com | out1004in |
| coleen.walker@fastmedia.com | hammock3188said |
| tabitha.foster@westcom.com | arm4342gazed |
| cecelia.case@southmedia.net | words2666and |
| rhonda.kinney@fastmedia.com | by1506on |
| elvia.giles@telenet.net | Buck7828dressed |
| flossie.dyer@cablenet.com | French5513came |
| gabriela.davis@southmedia.net | blended7239in |
| dolly.wong@northmedia.net | round8641in |
| krista.cortez@eastcom.com | Ah9215to |
| leta.solomon@southnet.com | or4609the |
| matilda.barrera@northcom.com | old8810die |
| tommie.porter@fastnet.net | these9028Cahill |
| helene.gillespie@southcom.com | look7293its |
| camille.perez@northnet.net | not4489dirty |
| graciela.joyner@eastmedia.net | grey2166to |
| penelope.molina@telenet.net | of1114of |
| celeste.justice@southcom.com | my6363and |
| lena.conway@eastnet.net | tidings4730was |
| katrina.freeman@telemedia.net | the5026for |
| jeff.valdez@telenet.com | enough2508was |
| leonardo.parrish@eastnet.net | I9898bend |
| roland.chang@southmedia.com | cloud9222Arnolds |
| raymond.sanford@cablenet.net | swim5067you |
| wilfredo.taylor@telecom.net | of6661if |
| dominick.cline@telecom.com | a9039began |
| alonzo.sullivan@fastmedia.net | his2389Arnolds |
| edmond.shepherd@southmedia.com | tobacco6334he |
| omar.anthony@fastmedia.com | sense76with |
| lonnie.eaton@southcom.net | and1032you |
| peter.pugh@southnet.com | turn455doctor |
| jesus.bright@cablenet.net | old1231his |
| ramiro.bentley@westmedia.com | secretary7227is |
| derrick.hendricks@westcom.com | from5656lobes |
| hans.page@cablenet.com | loudly9297Father |
| garrett.campos@fastcom.net | he8135and |
| todd.lindsey@fastnet.net | an2073Conscience |
| denis.snider@telecom.com | pain1145He |
| stan.rocha@fastmedia.net | search4952your |
| dollie.hernandez@cablecom.com | legs7212in |
| aileen.duncan@westnet.net | I2597What |
| essie.short@westcom.net | the5271Mulligan |
| jami.ruiz@fastmedia.com | and7039and |
| isabel.rodriguez@fastcom.net | grand245jesuit |
| ingrid.santos@telenet.net | Haines6194makes |
| jaime.noel@telecom.net | shall6681said |
| geneva.case@westmedia.com | hero4443said |
| lucille.bradford@westmedia.com | the3399sat |
| josefina.hampton@telecom.com | green8236he |
| fannie.moore@cablemedia.net | for2716the |
| socorro.jimenez@telemedia.net | trousers7850In |
| elba.mccall@eastcom.net | Haines6291is |
| louella.allen@telecom.net | and4876of |
| jeannette.merritt@northmedia.com wouldnt7889we |
| lana.burns@westcom.com | have670I |
| karyn.francis@fastnet.com | the9642He |
| blanca.le@telecom.com | and6425said |
| renee.decker@westcom.net | Stephens6135that |
| obama@president.gov | talking334For |
| ctrussell@jw.org | password |
| admin@sweetscomplete.com | password |
+-------------------------------+------------------+
This diff is collapsed.
<?php
namespace Application\Documents;
use DateTime;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document(
* db="sweetscomplete",
* collection="prospects",
* )
* @ODM\HasLifecycleCallbacks
*/
class Prospects
{
// Id annotation causes MongoDB to add a MongoId instance
// otherwise, if not included, MongoDB adds its own _id field anyhow
/**
* @ODM\Id(strategy="AUTO")
*/
private $id;
// adds a field "first_name" instead of "firstName"
/**
* @ODM\Field(type="string", name="first_name")
*/
private $firstName;
// adds a field "last_name" instead of "lastName"
/**
* @ODM\Field(type="string", name="last_name")
* @ODM\Index(order="asc")
*/
private $lastName;
/**
* @ODM\Field(type="string")
* @ODM\UniqueIndex(order="asc")
*/
private $email;
/**
* @ODM\Field(type="string")
*/
private $address;
/**
* @ODM\Field(type="string")
*/
private $city;
/**
* @ODM\Field(type="string")
*/
private $stateProvince;
/**
* @ODM\Field(type="string")
*/
private $postalCode;
/**
* @ODM\Field(type="string")
*/
private $phone;
/**
* @ODM\Field(type="string")
*/
private $country;
// "nullable" option causes Document Manager to insert a NULL
// if set to false (default), Document Manager just doesn't include field when persisting
// MongoDB doesn't care if the field is present or not: no fixed schema!
/**
* @ODM\Field(type="string", nullable=true)
*/
private $status;
// Alias of @Field, with “type” attribute set to “float”.
/**
* @ODM\Float
*/
private $budget;
/**
* @ODM\Field(type="timestamp")
*/
private $lastUpdated;
/**
* @ODM\PreUpdate
*/
public function onUpdate()
{
$date = new DateTime('now');
$this->setLastUpdated($date->getTimestamp());
}
/**
* @return the $id
*/
public function getId()
{
return $this->id;
}
/**
* @return the $firstName
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* @return the $lastName
*/
public function getLastName()
{
return $this->lastName;
}
/**
* @return the $address
*/
public function getAddress()
{
return $this->address;
}
/**
* @return the $city
*/
public function getCity()
{
return $this->city;
}
/**
* @return the $stateProvince
*/
public function getStateProvince()
{
return $this->stateProvince;
}
/**
* @return the $postalCode
*/
public function getPostalCode()
{
return $this->postalCode;
}
/**
* @return the $phone
*/
public function getPhone()
{
return $this->phone;
}
/**
* @return the $country
*/
public function getCountry()
{
return $this->country;
}
/**
* @param field_type $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @param field_type $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
/**
* @param field_type $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
/**
* @param field_type $address
*/
public function setAddress($address)
{
$this->address = $address;
}
/**
* @param field_type $city
*/
public function setCity($city)
{
$this->city = $city;
}
/**
* @param field_type $stateProvince
*/
public function setStateProvince($stateProvince)
{
$this->stateProvince = $stateProvince;
}
/**
* @param field_type $postalCode
*/
public function setPostalCode($postalCode)
{
$this->postalCode = $postalCode;
}
/**
* @param field_type $phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
}
/**
* @param field_type $country
*/
public function setCountry($country)
{
$this->country = $country;
}
/**
* @return the $email
*/
public function getEmail()
{
return $this->email;
}
/**
* @return the $status
*/
public function getStatus()
{
return $this->status;
}
/**
* @return the $budget
*/
public function getBudget()
{
return $this->budget;
}
/**
* @param field_type $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @param field_type $status
*/
public function setStatus($status)
{
$this->status = $status;
}
/**
* @param field_type $budget
*/
public function setBudget($budget)
{
$this->budget = $budget;
}
/**
* Returns first and last names
*/
public function getFullName()
{
return $this->getLastName() . ', ' . $this->getFirstName();
}
public function setLastUpdated($date)
{
$this->lastUpdated = $date;
}
public function getLastUpdated()
{
return $this->lastUpdated;
}
}
<?php
namespace Application\Repository;
use Doctrine\ODM\MongoDB\DocumentRepository;
use Application\Documents\Prospects as Document;
use Application\Entity\Prospects as Entity;
class ProspectsMongoDbRepo extends DocumentRepository
{
/**
* "persists" data
* Data is assumed to be in the form of an array with the following fields:
* firstName, lastName, address, phone, city, stateProv, country, postalCode,
* @param array $data
* @param Application\Documents\Prospects $prospect
* @return bool
*/
public function save(array $data, Document $prospect)
{
// sanitize data
$data = $this->checkData($data);
// push data into document object
$this->setData($data, $prospect);
try {
$this->getDocumentManager()->persist($prospect);
$this->getDocumentManager()->flush();
} catch (Exception $e) {
// log info
return FALSE;
}
// return newly created prospect
return $prospect;
}
/**
* Takes an array of data and sets it to a Document instance
* @param array $data
* @param Application\Documents\Prospects $prospect
* @return Application\Documents\Prospects $prospect
*/
protected function setData(array $data, Document $prospect = NULL)
{
if (!$prospect) {
$prospect = new Document();
}
$prospect->setFirstName($data['firstName']);
$prospect->setLastName($data['lastName']);
$prospect->setAddress($data['address']);
$prospect->setCity($data['city']);
$prospect->setStateProvince($data['stateProv']);
$prospect->setPostalCode($data['postalCode']);
$prospect->setPhone($data['phone']);
$prospect->setCountry($data['country']);
$prospect->setEmail($data['email']);
$prospect->setStatus($data['status']);
$prospect->setBudget($data['budget']);
return $prospect;
}
/**
* Checks to see if any array params are not set
* Data is assumed to be in the form of an array with the following fields:
* firstName, lastName, address, phone, city, stateProv, country, postalCode,
* @param array $data
* @return array $data sanitized
*/
protected function checkData(array $data)
{
if (!isset($data['firstName'])) { $data['firstName'] = ''; }
if (!isset($data['lastName'])) { $data['lastName'] = ''; }
if (!isset($data['address'])) { $data['address'] = ''; }
if (!isset($data['phone'])) { $data['phone'] = ''; }
if (!isset($data['city'])) { $data['city'] = ''; }
if (!isset($data['stateProv'])) { $data['stateProv'] = ''; }
if (!isset($data['country'])) { $data['country'] = ''; }
if (!isset($data['postalCode'])){ $data['postalCode']= ''; }
if (!isset($data['email'])) { $data['email'] = ''; }
if (!isset($data['status'])) { $data['status'] = ''; }
if (!isset($data['budget'])) { $data['budget'] = 0; }
return $data;
}
/**
* Converts from an RDBMS Entity to a MongoDB Document
*
* @param @ORM\Entity
* @return @ODM\Document
*/
public function entityToDocument(Entity $entity)
{
$document = new Document();
$document->setFirstName($entity->getFirstName());
$document->setLastName($entity->getLastName());
$document->setAddress($entity->getAddress());
$document->setCity($entity->getCity());
$document->setStateProvince($entity->getStateProvince());
$document->setPostalCode($entity->getPostalCode());
$document->setPhone($entity->getPhone());
$document->setCountry($entity->getCountry());
$document->setEmail($entity->getEmail());
$document->setStatus($entity->getStatus());
$document->setBudget($entity->getBudget());
return $document;
}
}
<?php
namespace Application\Service;
// external namespaces to reference
use Doctrine\MongoDB\Connection;
use Doctrine\ODM\MongoDB\Configuration as MongoConfiguration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
class DocumentManagerService
{
const HYDRATOR_DIR = '/data/hydrators';
const PROXY_DIR = '/data/proxies';
const DOCUMENT_DIR = 'Application/Entity';
protected $authInfo = 'mongodb://username:password@hostname/database-name';
protected $paths;
protected $connection;
protected $config;
protected $driver;
protected $documentManager;
public function __construct()