Created
October 22, 2013 13:30
-
-
Save dadamssg/7100829 to your computer and use it in GitHub Desktop.
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 | |
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