Created
November 17, 2011 10:54
-
-
Save magnusnordlander/1372903 to your computer and use it in GitHub Desktop.
Mongo ODM Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SymfonySE\Documents; | |
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; | |
/** | |
* @MongoDB\Document | |
*/ | |
class Pizza | |
{ | |
/** @MongoDB\Id */ | |
protected $id; | |
/** @MongoDB\Field(type="string") */ | |
protected $name; | |
/** @MongoDB\Field(type="integer") */ | |
protected $price; | |
public function __construct($name, $price) | |
{ | |
$this->name = $name; | |
$this->price = $price; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getPrice() | |
{ | |
return $this->price; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Finding by ID | |
$repo = $dm->getRepository('SymfonySE\Documents\Pizza'); | |
$pizza = $repo->find('4ec4fe8ff4e9031b2646c3b8'); | |
// Using the query builder | |
$qb = $repo->createQueryBuilder(); | |
$q = $qb | |
->field('name')->equals('Margarita') | |
->getQuery(); | |
$pizzas = $q->execute(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$repo = $dm->getRepository('SymfonySE\Documents\Pizza'); | |
$qb = $repo->createQueryBuilder(); | |
$q = $qb | |
->field('toppings.name')->equals('Mozzarella') | |
->field('price')->lt(110) | |
->getQuery(); | |
$pizzas = $q->execute(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SymfonySE\Documents; | |
class Pizza | |
{ | |
protected $name; | |
protected $price; | |
public function __construct($name, $price) | |
{ | |
$this->name = $name; | |
$this->price = $price; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getPrice() | |
{ | |
return $this->price; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SymfonySE\Documents; | |
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; | |
/** | |
* @MongoDB\Document | |
*/ | |
class Pizza | |
{ | |
/** @MongoDB\Id */ | |
protected $id; | |
/** @MongoDB\Field(type="string") */ | |
protected $name; | |
/** @MongoDB\Field(type="integer") */ | |
protected $price; | |
/** @MongoDB\EmbedMany(targetDocument="SymfonySE\Documents\Topping") */ | |
protected $toppings = array(); | |
public function __construct($name, $price) | |
{ | |
$this->name = $name; | |
$this->price = $price; | |
} | |
public function addTopping(Topping $topping) | |
{ | |
$this->toppings[] = $topping; | |
} | |
public function getToppings() | |
{ | |
return $this->toppings; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getPrice() | |
{ | |
return $this->price; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SymfonySE\Documents; | |
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; | |
/** | |
* @MongoDB\EmbeddedDocument | |
*/ | |
class Topping | |
{ | |
/** @MongoDB\Field(type="string") */ | |
protected $name; | |
/** @MongoDB\Field(type="string") */ | |
protected $amount; | |
public function __construct($name, $amount) | |
{ | |
$this->name = $name; | |
$this->amount = $amount; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getAmount() | |
{ | |
return $this->amount; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$margarita = new Pizza('Margarita', 79); | |
$margarita->addTopping(new Topping('Mozzarella', 'dollops')); | |
$margarita->addTopping(new Topping('Tomato', 'sort of a lot')); | |
$margarita->addTopping(new Topping('Basil', 'sprinkled')); | |
$dm->persist($margarita); | |
$dm->flush(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$margarita = new Pizza('Margarita', 79); | |
$dm->persist($margarita); | |
$dm->flush(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment