Added product creation

This commit is contained in:
franck
2013-09-17 20:18:53 +02:00
parent 9e927c40be
commit 1e15142283

View File

@@ -10,6 +10,8 @@ use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Event\ProductEvent; use Thelia\Core\Event\ProductEvent;
use Propel\Runtime\ActiveQuery\Criteria; use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Propel;
use Thelia\Model\Map\ProductTableMap;
class Product extends BaseProduct class Product extends BaseProduct
{ {
@@ -19,6 +21,8 @@ class Product extends BaseProduct
use \Thelia\Model\Tools\UrlRewritingTrait; use \Thelia\Model\Tools\UrlRewritingTrait;
protected $defaultCategory = null;
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@@ -45,7 +49,7 @@ class Product extends BaseProduct
} }
/** /**
* @return the current default category for this product * @return the current default category ID for this product
*/ */
public function getDefaultCategoryId() public function getDefaultCategoryId()
{ {
@@ -84,6 +88,42 @@ class Product extends BaseProduct
return $this; return $this;
} }
/**
* Create a new product, along with the default category ID
*
* @param int $defaultCategoryId the default category ID of this product
*/
public function create($defaultCategoryId) {
$con = Propel::getWriteConnection(ProductTableMap::DATABASE_NAME);
$con->beginTransaction();
try {
// Create the product
$this->save($con);
// Add the default category
$pc = new ProductCategory();
$pc
->setProduct($this)
->setCategoryId($defaultCategoryId)
->setDefaultCategory(true)
->save($con)
;
// Store all the stuff !
$con->commit();
}
catch(PropelException $ex) {
$con->rollback();
throw $ex;
}
}
/** /**
* Calculate next position relative to our default category * Calculate next position relative to our default category
*/ */
@@ -105,8 +145,6 @@ class Product extends BaseProduct
*/ */
public function preInsert(ConnectionInterface $con = null) public function preInsert(ConnectionInterface $con = null)
{ {
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale()); $this->generateRewritenUrl($this->getLocale());
$this->dispatchEvent(TheliaEvents::BEFORE_CREATEPRODUCT, new ProductEvent($this)); $this->dispatchEvent(TheliaEvents::BEFORE_CREATEPRODUCT, new ProductEvent($this));
@@ -119,6 +157,10 @@ class Product extends BaseProduct
*/ */
public function postInsert(ConnectionInterface $con = null) public function postInsert(ConnectionInterface $con = null)
{ {
$this->setPosition($this->getNextPosition());
$this->save();
$this->dispatchEvent(TheliaEvents::AFTER_CREATEPRODUCT, new ProductEvent($this)); $this->dispatchEvent(TheliaEvents::AFTER_CREATEPRODUCT, new ProductEvent($this));
} }