Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Created October 22, 2013 13:30
Show Gist options
  • Save dadamssg/7100829 to your computer and use it in GitHub Desktop.
Save dadamssg/7100829 to your computer and use it in GitHub Desktop.
<?php
class DoctrineProductRepository implements ProductRepository {
//entity manager
protected $em;
public function addCategory(ProductInterface $product, CategoryInterface $category)
{
$product->setCategory($category);
$this->em->persist($category);
$this->em->persist($product);
}
public function flush()
{
$this->em->flush();
}
}
class PropelProductRepository implements ProductRepository {
protected $toSave = [];
public function addCategory(ProductInterface $product, CategoryInterface $category)
{
$product->setCategory($category);
$this->soSave[] = $product;
}
public function flush()
{
if ! empty($this->toSave) {
$con = Propel::getConnection(MyApp::DATABASE_NAME);
$con->beginTransaction();
try {
foreach ($this->toSave as $model) {
$model->save();
}
$con->commit();
$this->toSave = [];
} catch (Exception $e) {
$con->rollback();
throw $e;
}
}
}
}
class EloquentProductRepository implements ProductRepository {
protected $toSave = [];
public function addCategory(ProductInterface $product, CategoryInterface $category)
{
$this->toSave[] = function() use ($product, $catgory) {
$product->category()->save($category);
}
}
public function flush()
{
if( ! empty($this->toSave)) {
DB::transaction(function()
{
foreach ($this->toSave as $closure) {
$closure();
}
$this->toSave = [];
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment