Skip to content

Instantly share code, notes, and snippets.

@magnusnordlander
Created November 17, 2011 10:54
Show Gist options
  • Save magnusnordlander/1372903 to your computer and use it in GitHub Desktop.
Save magnusnordlander/1372903 to your computer and use it in GitHub Desktop.
Mongo ODM Samples
<?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;
}
}
<?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();
<?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();
<?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;
}
}
<?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;
}
}
<?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;
}
}
<?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();
<?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