order creation

This commit is contained in:
Etienne Roudeix
2013-09-19 19:56:59 +02:00
parent 1d969e38ec
commit 6e9af59403
12 changed files with 4622 additions and 13 deletions

View File

@@ -23,12 +23,23 @@
namespace Thelia\Action;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\AddressQuery;
use Thelia\Exception\OrderException;
use Thelia\Model\AttributeAvI18n;
use Thelia\Model\AttributeAvI18nQuery;
use Thelia\Model\AttributeI18n;
use Thelia\Model\AttributeI18nQuery;
use Thelia\Model\AttributeQuery;
use Thelia\Model\AddressQuery;
use Thelia\Model\OrderAttributeCombination;
use Thelia\Model\ProductI18nQuery;
use Thelia\Model\Lang;
use Thelia\Model\ModuleQuery;
use Thelia\Model\OrderProduct;
use Thelia\Model\OrderStatus;
@@ -36,6 +47,7 @@ use Thelia\Model\Map\OrderTableMap;
use Thelia\Model\OrderAddress;
use Thelia\Model\OrderStatusQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Model\ProductI18n;
/**
*
@@ -167,15 +179,68 @@ class Order extends BaseAction implements EventSubscriberInterface
$placedOrder->save($con);
/* fulfill order_products and decrease stock // @todo + dispatch event */
/* fulfill order_products and decrease stock */
foreach($cartItems as $cartItem) {
$product = $cartItem->getProduct();
/* get customer translation */
$productI18n = $this->getI18n(ProductI18nQuery::create(), new ProductI18n(), $product->getId());
$pse = $cartItem->getProductSaleElements();
/* check still in stock */
if($cartItem->getQuantity() > $pse->getQuantity()) {
$e = new OrderException("Not enough stock", OrderException::NOT_ENOUGH_STOCK);
$e->cartItem = $cartItem;
throw $e;
}
/* decrease stock */
$pse->setQuantity(
$pse->getQuantity() - $cartItem->getQuantity()
);
$pse->save();
$orderProduct = new OrderProduct();
$orderProduct
->setOrderId($placedOrder->getId())
->setProductRef($product->getRef())
->setProductSaleElementsRef($pse->getRef())
->setTitle($productI18n->getTitle())
->setChapo($productI18n->getChapo())
->setDescription($productI18n->getDescription())
->setPostscriptum($productI18n->getPostscriptum())
->setQuantity($cartItem->getQuantity())
->setPrice($cartItem->getPrice())
->setPromoPrice($cartItem->getPromoPrice())
->setWasNew($pse->getNewness())
->setWasInPromo($cartItem->getPromo())
->setWeight($pse->getWeight())
;
$orderProduct->setDispatcher($this->getDispatcher());
$orderProduct->save();
$in = true;
/* fulfill order_attribute_combination and decrease stock */
foreach($pse->getAttributeCombinations() as $attributeCombination) {
$attribute = $this->getI18n(AttributeI18nQuery::create(), new AttributeI18n(), $attributeCombination->getAttributeId());
$attributeAv = $this->getI18n(AttributeAvI18nQuery::create(), new AttributeAvI18n(), $attributeCombination->getAttributeAvId());
$orderAttributeCombination = new OrderAttributeCombination();
$orderAttributeCombination
->setOrderProductId($orderProduct->getId())
->setAttributeTitle($attribute->getTitle())
->setAttributeChapo($attribute->getChapo())
->setAttributeDescription($attribute->getDescription())
->setAttributePostscriptumn($attribute->getPostscriptum())
->setAttributeAvTitle($attributeAv->getTitle())
->setAttributeAvChapo($attributeAv->getChapo())
->setAttributeAvDescription($attributeAv->getDescription())
->setAttributeAvPostscriptum($attributeAv->getPostscriptum())
;
$orderAttributeCombination->save();
}
}
/* discount @todo */
@@ -239,7 +304,7 @@ class Order extends BaseAction implements EventSubscriberInterface
TheliaEvents::ORDER_SET_INVOICE_ADDRESS => array("setInvoiceAddress", 128),
TheliaEvents::ORDER_SET_PAYMENT_MODULE => array("setPaymentModule", 128),
TheliaEvents::ORDER_PAY => array("create", 128),
TheliaEvents::ORDER_SET_REFERENCE => array("setReference", 128),
TheliaEvents::ORDER_BEFORE_CREATE => array("setReference", 128),
);
}
@@ -272,4 +337,42 @@ class Order extends BaseAction implements EventSubscriberInterface
return $request->getSession();
}
/**
* @param ModelCriteria $query
* @param ActiveRecordInterface $object
* @param $id
* @param array $needed = array('Title')
*
* @return ProductI18n
*/
protected function getI18n(ModelCriteria $query, ActiveRecordInterface $object, $id, $needed = array('Title'))
{
$i18n = $query
->filterById($id)
->filterByLocale(
$this->getSession()->getLang()->getLocale()
)->findOne();
/* or default translation */
if(null === $i18n) {
$i18n = $query
->filterById($id)
->filterByLocale(
Lang::getDefaultLanguage()->getLocale()
)->findOne();
}
if(null === $i18n) { // @todo something else ?
$i18n = $object;
foreach($needed as $need) {
$method = sprintf('get%s', $need);
if(method_exists($i18n, $method)) {
$i18n->$method('DEFAULT ' . strtoupper($need));
} else {
// @todo throw sg ?
}
}
}
return $i18n;
}
}

View File

@@ -24,6 +24,8 @@ namespace Thelia\Controller\Front;
use Symfony\Component\Routing\Router;
use Thelia\Controller\BaseController;
use Thelia\Model\AddressQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Tools\URL;
class BaseFrontController extends BaseController
@@ -69,7 +71,7 @@ class BaseFrontController extends BaseController
protected function checkValidDelivery()
{
$order = $this->getSession()->getOrder();
if(null === $order || null === $order->chosenDeliveryAddress || null === $order->getDeliveryModuleId()) {
if(null === $order || null === $order->chosenDeliveryAddress || null === $order->getDeliveryModuleId() || null === AddressQuery::create()->findPk($order->chosenDeliveryAddress) || null === ModuleQuery::create()->findPk($order->getDeliveryModuleId())) {
$this->redirectToRoute("order.delivery");
}
}
@@ -77,7 +79,7 @@ class BaseFrontController extends BaseController
protected function checkValidInvoice()
{
$order = $this->getSession()->getOrder();
if(null === $order || null === $order->chosenInvoiceAddress || null === $order->getPaymentModuleId()) {
if(null === $order || null === $order->chosenInvoiceAddress || null === $order->getPaymentModuleId() || null === AddressQuery::create()->findPk($order->chosenInvoiceAddress) || null === ModuleQuery::create()->findPk($order->getPaymentModuleId())) {
$this->redirectToRoute("order.invoice");
}
}

View File

@@ -78,11 +78,8 @@ class OrderController extends BaseFrontController
throw new \Exception("Delivery module cannot be use with selected delivery address");
}
/* try to get postage amount */
/* get postage amount */
$moduleReflection = new \ReflectionClass($deliveryModule->getFullNamespace());
if ($moduleReflection->isSubclassOf("Thelia\Module\DeliveryModuleInterface") === false) {
throw new \RuntimeException(sprintf("delivery module %s is not a Thelia\Module\DeliveryModuleInterface", $deliveryModule->getCode()));
}
$moduleInstance = $moduleReflection->newInstance();
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());

View File

@@ -220,7 +220,10 @@ final class TheliaEvents
const ORDER_SET_INVOICE_ADDRESS = "action.order.setInvoiceAddress";
const ORDER_SET_PAYMENT_MODULE = "action.order.setPaymentModule";
const ORDER_PAY = "action.order.pay";
const ORDER_SET_REFERENCE = "action.order.setReference";
const ORDER_BEFORE_CREATE = "action.order.beforeCreate";
const ORDER_AFTER_CREATE = "action.order.afterCreate";
const ORDER_PRODUCT_BEFORE_CREATE = "action.orderProduct.beforeCreate";
const ORDER_PRODUCT_AFTER_CREATE = "action.orderProduct.afterCreate";
/**
* Sent on image processing

View File

@@ -30,6 +30,7 @@ class OrderException extends \RuntimeException
*/
public $cartRoute = "cart.view";
public $orderDeliveryRoute = "order.delivery";
public $cartItem = null;
public $arguments = array();
@@ -39,6 +40,8 @@ class OrderException extends \RuntimeException
const UNDEFINED_DELIVERY = 200;
const NOT_ENOUGH_STOCK = 300;
public function __construct($message, $code = null, $arguments = array(), $previous = null)
{
if(is_array($arguments)) {

View File

@@ -80,7 +80,7 @@ class OrderDelivery extends BaseForm
->filterByType(BaseModule::DELIVERY_MODULE_TYPE)
->filterByActivate(1)
->filterById($value)
->find();
->findOne();
if(null === $module) {
$context->addViolation("Delivery module ID not found");

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,561 @@
<?php
namespace Thelia\Model\Map;
use Propel\Runtime\Propel;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\InstancePoolTrait;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Map\RelationMap;
use Propel\Runtime\Map\TableMap;
use Propel\Runtime\Map\TableMapTrait;
use Thelia\Model\OrderProduct;
use Thelia\Model\OrderProductQuery;
/**
* This class defines the structure of the 'order_product' table.
*
*
*
* This map class is used by Propel to do runtime db structure discovery.
* For example, the createSelectSql() method checks the type of a given column used in an
* ORDER BY clause to know whether it needs to apply SQL to make the ORDER BY case-insensitive
* (i.e. if it's a text column type).
*
*/
class OrderProductTableMap extends TableMap
{
use InstancePoolTrait;
use TableMapTrait;
/**
* The (dot-path) name of this class
*/
const CLASS_NAME = 'Thelia.Model.Map.OrderProductTableMap';
/**
* The default database name for this class
*/
const DATABASE_NAME = 'thelia';
/**
* The table name for this class
*/
const TABLE_NAME = 'order_product';
/**
* The related Propel class for this table
*/
const OM_CLASS = '\\Thelia\\Model\\OrderProduct';
/**
* A class that can be returned by this tableMap
*/
const CLASS_DEFAULT = 'Thelia.Model.OrderProduct';
/**
* The total number of columns
*/
const NUM_COLUMNS = 18;
/**
* The number of lazy-loaded columns
*/
const NUM_LAZY_LOAD_COLUMNS = 0;
/**
* The number of columns to hydrate (NUM_COLUMNS - NUM_LAZY_LOAD_COLUMNS)
*/
const NUM_HYDRATE_COLUMNS = 18;
/**
* the column name for the ID field
*/
const ID = 'order_product.ID';
/**
* the column name for the ORDER_ID field
*/
const ORDER_ID = 'order_product.ORDER_ID';
/**
* the column name for the PRODUCT_REF field
*/
const PRODUCT_REF = 'order_product.PRODUCT_REF';
/**
* the column name for the PRODUCT_SALE_ELEMENTS_REF field
*/
const PRODUCT_SALE_ELEMENTS_REF = 'order_product.PRODUCT_SALE_ELEMENTS_REF';
/**
* the column name for the TITLE field
*/
const TITLE = 'order_product.TITLE';
/**
* the column name for the CHAPO field
*/
const CHAPO = 'order_product.CHAPO';
/**
* the column name for the DESCRIPTION field
*/
const DESCRIPTION = 'order_product.DESCRIPTION';
/**
* the column name for the POSTSCRIPTUM field
*/
const POSTSCRIPTUM = 'order_product.POSTSCRIPTUM';
/**
* the column name for the QUANTITY field
*/
const QUANTITY = 'order_product.QUANTITY';
/**
* the column name for the PRICE field
*/
const PRICE = 'order_product.PRICE';
/**
* the column name for the PROMO_PRICE field
*/
const PROMO_PRICE = 'order_product.PROMO_PRICE';
/**
* the column name for the WAS_NEW field
*/
const WAS_NEW = 'order_product.WAS_NEW';
/**
* the column name for the WAS_IN_PROMO field
*/
const WAS_IN_PROMO = 'order_product.WAS_IN_PROMO';
/**
* the column name for the WEIGHT field
*/
const WEIGHT = 'order_product.WEIGHT';
/**
* the column name for the TAX field
*/
const TAX = 'order_product.TAX';
/**
* the column name for the PARENT field
*/
const PARENT = 'order_product.PARENT';
/**
* the column name for the CREATED_AT field
*/
const CREATED_AT = 'order_product.CREATED_AT';
/**
* the column name for the UPDATED_AT field
*/
const UPDATED_AT = 'order_product.UPDATED_AT';
/**
* The default string format for model objects of the related table
*/
const DEFAULT_STRING_FORMAT = 'YAML';
/**
* holds an array of fieldnames
*
* first dimension keys are the type constants
* e.g. self::$fieldNames[self::TYPE_PHPNAME][0] = 'Id'
*/
protected static $fieldNames = array (
self::TYPE_PHPNAME => array('Id', 'OrderId', 'ProductRef', 'ProductSaleElementsRef', 'Title', 'Chapo', 'Description', 'Postscriptum', 'Quantity', 'Price', 'PromoPrice', 'WasNew', 'WasInPromo', 'Weight', 'Tax', 'Parent', 'CreatedAt', 'UpdatedAt', ),
self::TYPE_STUDLYPHPNAME => array('id', 'orderId', 'productRef', 'productSaleElementsRef', 'title', 'chapo', 'description', 'postscriptum', 'quantity', 'price', 'promoPrice', 'wasNew', 'wasInPromo', 'weight', 'tax', 'parent', 'createdAt', 'updatedAt', ),
self::TYPE_COLNAME => array(OrderProductTableMap::ID, OrderProductTableMap::ORDER_ID, OrderProductTableMap::PRODUCT_REF, OrderProductTableMap::PRODUCT_SALE_ELEMENTS_REF, OrderProductTableMap::TITLE, OrderProductTableMap::CHAPO, OrderProductTableMap::DESCRIPTION, OrderProductTableMap::POSTSCRIPTUM, OrderProductTableMap::QUANTITY, OrderProductTableMap::PRICE, OrderProductTableMap::PROMO_PRICE, OrderProductTableMap::WAS_NEW, OrderProductTableMap::WAS_IN_PROMO, OrderProductTableMap::WEIGHT, OrderProductTableMap::TAX, OrderProductTableMap::PARENT, OrderProductTableMap::CREATED_AT, OrderProductTableMap::UPDATED_AT, ),
self::TYPE_RAW_COLNAME => array('ID', 'ORDER_ID', 'PRODUCT_REF', 'PRODUCT_SALE_ELEMENTS_REF', 'TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM', 'QUANTITY', 'PRICE', 'PROMO_PRICE', 'WAS_NEW', 'WAS_IN_PROMO', 'WEIGHT', 'TAX', 'PARENT', 'CREATED_AT', 'UPDATED_AT', ),
self::TYPE_FIELDNAME => array('id', 'order_id', 'product_ref', 'product_sale_elements_ref', 'title', 'chapo', 'description', 'postscriptum', 'quantity', 'price', 'promo_price', 'was_new', 'was_in_promo', 'weight', 'tax', 'parent', 'created_at', 'updated_at', ),
self::TYPE_NUM => array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, )
);
/**
* holds an array of keys for quick access to the fieldnames array
*
* first dimension keys are the type constants
* e.g. self::$fieldKeys[self::TYPE_PHPNAME]['Id'] = 0
*/
protected static $fieldKeys = array (
self::TYPE_PHPNAME => array('Id' => 0, 'OrderId' => 1, 'ProductRef' => 2, 'ProductSaleElementsRef' => 3, 'Title' => 4, 'Chapo' => 5, 'Description' => 6, 'Postscriptum' => 7, 'Quantity' => 8, 'Price' => 9, 'PromoPrice' => 10, 'WasNew' => 11, 'WasInPromo' => 12, 'Weight' => 13, 'Tax' => 14, 'Parent' => 15, 'CreatedAt' => 16, 'UpdatedAt' => 17, ),
self::TYPE_STUDLYPHPNAME => array('id' => 0, 'orderId' => 1, 'productRef' => 2, 'productSaleElementsRef' => 3, 'title' => 4, 'chapo' => 5, 'description' => 6, 'postscriptum' => 7, 'quantity' => 8, 'price' => 9, 'promoPrice' => 10, 'wasNew' => 11, 'wasInPromo' => 12, 'weight' => 13, 'tax' => 14, 'parent' => 15, 'createdAt' => 16, 'updatedAt' => 17, ),
self::TYPE_COLNAME => array(OrderProductTableMap::ID => 0, OrderProductTableMap::ORDER_ID => 1, OrderProductTableMap::PRODUCT_REF => 2, OrderProductTableMap::PRODUCT_SALE_ELEMENTS_REF => 3, OrderProductTableMap::TITLE => 4, OrderProductTableMap::CHAPO => 5, OrderProductTableMap::DESCRIPTION => 6, OrderProductTableMap::POSTSCRIPTUM => 7, OrderProductTableMap::QUANTITY => 8, OrderProductTableMap::PRICE => 9, OrderProductTableMap::PROMO_PRICE => 10, OrderProductTableMap::WAS_NEW => 11, OrderProductTableMap::WAS_IN_PROMO => 12, OrderProductTableMap::WEIGHT => 13, OrderProductTableMap::TAX => 14, OrderProductTableMap::PARENT => 15, OrderProductTableMap::CREATED_AT => 16, OrderProductTableMap::UPDATED_AT => 17, ),
self::TYPE_RAW_COLNAME => array('ID' => 0, 'ORDER_ID' => 1, 'PRODUCT_REF' => 2, 'PRODUCT_SALE_ELEMENTS_REF' => 3, 'TITLE' => 4, 'CHAPO' => 5, 'DESCRIPTION' => 6, 'POSTSCRIPTUM' => 7, 'QUANTITY' => 8, 'PRICE' => 9, 'PROMO_PRICE' => 10, 'WAS_NEW' => 11, 'WAS_IN_PROMO' => 12, 'WEIGHT' => 13, 'TAX' => 14, 'PARENT' => 15, 'CREATED_AT' => 16, 'UPDATED_AT' => 17, ),
self::TYPE_FIELDNAME => array('id' => 0, 'order_id' => 1, 'product_ref' => 2, 'product_sale_elements_ref' => 3, 'title' => 4, 'chapo' => 5, 'description' => 6, 'postscriptum' => 7, 'quantity' => 8, 'price' => 9, 'promo_price' => 10, 'was_new' => 11, 'was_in_promo' => 12, 'weight' => 13, 'tax' => 14, 'parent' => 15, 'created_at' => 16, 'updated_at' => 17, ),
self::TYPE_NUM => array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, )
);
/**
* Initialize the table attributes and columns
* Relations are not initialized by this method since they are lazy loaded
*
* @return void
* @throws PropelException
*/
public function initialize()
{
// attributes
$this->setName('order_product');
$this->setPhpName('OrderProduct');
$this->setClassName('\\Thelia\\Model\\OrderProduct');
$this->setPackage('Thelia.Model');
$this->setUseIdGenerator(true);
// columns
$this->addPrimaryKey('ID', 'Id', 'INTEGER', true, null, null);
$this->addForeignKey('ORDER_ID', 'OrderId', 'INTEGER', 'order', 'ID', true, null, null);
$this->addColumn('PRODUCT_REF', 'ProductRef', 'VARCHAR', true, 255, null);
$this->addColumn('PRODUCT_SALE_ELEMENTS_REF', 'ProductSaleElementsRef', 'VARCHAR', true, 255, null);
$this->addColumn('TITLE', 'Title', 'VARCHAR', false, 255, null);
$this->addColumn('CHAPO', 'Chapo', 'LONGVARCHAR', false, null, null);
$this->addColumn('DESCRIPTION', 'Description', 'CLOB', false, null, null);
$this->addColumn('POSTSCRIPTUM', 'Postscriptum', 'LONGVARCHAR', false, null, null);
$this->addColumn('QUANTITY', 'Quantity', 'FLOAT', true, null, null);
$this->addColumn('PRICE', 'Price', 'FLOAT', true, null, null);
$this->addColumn('PROMO_PRICE', 'PromoPrice', 'VARCHAR', false, 45, null);
$this->addColumn('WAS_NEW', 'WasNew', 'TINYINT', true, null, null);
$this->addColumn('WAS_IN_PROMO', 'WasInPromo', 'TINYINT', true, null, null);
$this->addColumn('WEIGHT', 'Weight', 'VARCHAR', false, 45, null);
$this->addColumn('TAX', 'Tax', 'FLOAT', false, null, null);
$this->addColumn('PARENT', 'Parent', 'INTEGER', false, null, null);
$this->addColumn('CREATED_AT', 'CreatedAt', 'TIMESTAMP', false, null, null);
$this->addColumn('UPDATED_AT', 'UpdatedAt', 'TIMESTAMP', false, null, null);
} // initialize()
/**
* Build the RelationMap objects for this table relationships
*/
public function buildRelations()
{
$this->addRelation('Order', '\\Thelia\\Model\\Order', RelationMap::MANY_TO_ONE, array('order_id' => 'id', ), 'CASCADE', 'RESTRICT');
$this->addRelation('OrderAttributeCombination', '\\Thelia\\Model\\OrderAttributeCombination', RelationMap::ONE_TO_MANY, array('id' => 'order_product_id', ), 'CASCADE', 'RESTRICT', 'OrderAttributeCombinations');
} // buildRelations()
/**
*
* Gets the list of behaviors registered for this table
*
* @return array Associative array (name => parameters) of behaviors
*/
public function getBehaviors()
{
return array(
'timestampable' => array('create_column' => 'created_at', 'update_column' => 'updated_at', ),
);
} // getBehaviors()
/**
* Method to invalidate the instance pool of all tables related to order_product * by a foreign key with ON DELETE CASCADE
*/
public static function clearRelatedInstancePool()
{
// Invalidate objects in ".$this->getClassNameFromBuilder($joinedTableTableMapBuilder)." instance pool,
// since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule.
OrderAttributeCombinationTableMap::clearInstancePool();
}
/**
* Retrieves a string version of the primary key from the DB resultset row that can be used to uniquely identify a row in this table.
*
* For tables with a single-column primary key, that simple pkey value will be returned. For tables with
* a multi-column primary key, a serialize()d version of the primary key will be returned.
*
* @param array $row resultset row.
* @param int $offset The 0-based offset for reading from the resultset row.
* @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM
*/
public static function getPrimaryKeyHashFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM)
{
// If the PK cannot be derived from the row, return NULL.
if ($row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)] === null) {
return null;
}
return (string) $row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)];
}
/**
* Retrieves the primary key from the DB resultset row
* For tables with a single-column primary key, that simple pkey value will be returned. For tables with
* a multi-column primary key, an array of the primary key columns will be returned.
*
* @param array $row resultset row.
* @param int $offset The 0-based offset for reading from the resultset row.
* @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM
*
* @return mixed The primary key of the row
*/
public static function getPrimaryKeyFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM)
{
return (int) $row[
$indexType == TableMap::TYPE_NUM
? 0 + $offset
: self::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)
];
}
/**
* The class that the tableMap will make instances of.
*
* If $withPrefix is true, the returned path
* uses a dot-path notation which is translated into a path
* relative to a location on the PHP include_path.
* (e.g. path.to.MyClass -> 'path/to/MyClass.php')
*
* @param boolean $withPrefix Whether or not to return the path with the class name
* @return string path.to.ClassName
*/
public static function getOMClass($withPrefix = true)
{
return $withPrefix ? OrderProductTableMap::CLASS_DEFAULT : OrderProductTableMap::OM_CLASS;
}
/**
* Populates an object of the default type or an object that inherit from the default.
*
* @param array $row row returned by DataFetcher->fetch().
* @param int $offset The 0-based offset for reading from the resultset row.
* @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType().
One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
*
* @throws PropelException Any exceptions caught during processing will be
* rethrown wrapped into a PropelException.
* @return array (OrderProduct object, last column rank)
*/
public static function populateObject($row, $offset = 0, $indexType = TableMap::TYPE_NUM)
{
$key = OrderProductTableMap::getPrimaryKeyHashFromRow($row, $offset, $indexType);
if (null !== ($obj = OrderProductTableMap::getInstanceFromPool($key))) {
// We no longer rehydrate the object, since this can cause data loss.
// See http://www.propelorm.org/ticket/509
// $obj->hydrate($row, $offset, true); // rehydrate
$col = $offset + OrderProductTableMap::NUM_HYDRATE_COLUMNS;
} else {
$cls = OrderProductTableMap::OM_CLASS;
$obj = new $cls();
$col = $obj->hydrate($row, $offset, false, $indexType);
OrderProductTableMap::addInstanceToPool($obj, $key);
}
return array($obj, $col);
}
/**
* The returned array will contain objects of the default type or
* objects that inherit from the default.
*
* @param DataFetcherInterface $dataFetcher
* @return array
* @throws PropelException Any exceptions caught during processing will be
* rethrown wrapped into a PropelException.
*/
public static function populateObjects(DataFetcherInterface $dataFetcher)
{
$results = array();
// set the class once to avoid overhead in the loop
$cls = static::getOMClass(false);
// populate the object(s)
while ($row = $dataFetcher->fetch()) {
$key = OrderProductTableMap::getPrimaryKeyHashFromRow($row, 0, $dataFetcher->getIndexType());
if (null !== ($obj = OrderProductTableMap::getInstanceFromPool($key))) {
// We no longer rehydrate the object, since this can cause data loss.
// See http://www.propelorm.org/ticket/509
// $obj->hydrate($row, 0, true); // rehydrate
$results[] = $obj;
} else {
$obj = new $cls();
$obj->hydrate($row);
$results[] = $obj;
OrderProductTableMap::addInstanceToPool($obj, $key);
} // if key exists
}
return $results;
}
/**
* Add all the columns needed to create a new object.
*
* Note: any columns that were marked with lazyLoad="true" in the
* XML schema will not be added to the select list and only loaded
* on demand.
*
* @param Criteria $criteria object containing the columns to add.
* @param string $alias optional table alias
* @throws PropelException Any exceptions caught during processing will be
* rethrown wrapped into a PropelException.
*/
public static function addSelectColumns(Criteria $criteria, $alias = null)
{
if (null === $alias) {
$criteria->addSelectColumn(OrderProductTableMap::ID);
$criteria->addSelectColumn(OrderProductTableMap::ORDER_ID);
$criteria->addSelectColumn(OrderProductTableMap::PRODUCT_REF);
$criteria->addSelectColumn(OrderProductTableMap::PRODUCT_SALE_ELEMENTS_REF);
$criteria->addSelectColumn(OrderProductTableMap::TITLE);
$criteria->addSelectColumn(OrderProductTableMap::CHAPO);
$criteria->addSelectColumn(OrderProductTableMap::DESCRIPTION);
$criteria->addSelectColumn(OrderProductTableMap::POSTSCRIPTUM);
$criteria->addSelectColumn(OrderProductTableMap::QUANTITY);
$criteria->addSelectColumn(OrderProductTableMap::PRICE);
$criteria->addSelectColumn(OrderProductTableMap::PROMO_PRICE);
$criteria->addSelectColumn(OrderProductTableMap::WAS_NEW);
$criteria->addSelectColumn(OrderProductTableMap::WAS_IN_PROMO);
$criteria->addSelectColumn(OrderProductTableMap::WEIGHT);
$criteria->addSelectColumn(OrderProductTableMap::TAX);
$criteria->addSelectColumn(OrderProductTableMap::PARENT);
$criteria->addSelectColumn(OrderProductTableMap::CREATED_AT);
$criteria->addSelectColumn(OrderProductTableMap::UPDATED_AT);
} else {
$criteria->addSelectColumn($alias . '.ID');
$criteria->addSelectColumn($alias . '.ORDER_ID');
$criteria->addSelectColumn($alias . '.PRODUCT_REF');
$criteria->addSelectColumn($alias . '.PRODUCT_SALE_ELEMENTS_REF');
$criteria->addSelectColumn($alias . '.TITLE');
$criteria->addSelectColumn($alias . '.CHAPO');
$criteria->addSelectColumn($alias . '.DESCRIPTION');
$criteria->addSelectColumn($alias . '.POSTSCRIPTUM');
$criteria->addSelectColumn($alias . '.QUANTITY');
$criteria->addSelectColumn($alias . '.PRICE');
$criteria->addSelectColumn($alias . '.PROMO_PRICE');
$criteria->addSelectColumn($alias . '.WAS_NEW');
$criteria->addSelectColumn($alias . '.WAS_IN_PROMO');
$criteria->addSelectColumn($alias . '.WEIGHT');
$criteria->addSelectColumn($alias . '.TAX');
$criteria->addSelectColumn($alias . '.PARENT');
$criteria->addSelectColumn($alias . '.CREATED_AT');
$criteria->addSelectColumn($alias . '.UPDATED_AT');
}
}
/**
* Returns the TableMap related to this object.
* This method is not needed for general use but a specific application could have a need.
* @return TableMap
* @throws PropelException Any exceptions caught during processing will be
* rethrown wrapped into a PropelException.
*/
public static function getTableMap()
{
return Propel::getServiceContainer()->getDatabaseMap(OrderProductTableMap::DATABASE_NAME)->getTable(OrderProductTableMap::TABLE_NAME);
}
/**
* Add a TableMap instance to the database for this tableMap class.
*/
public static function buildTableMap()
{
$dbMap = Propel::getServiceContainer()->getDatabaseMap(OrderProductTableMap::DATABASE_NAME);
if (!$dbMap->hasTable(OrderProductTableMap::TABLE_NAME)) {
$dbMap->addTableObject(new OrderProductTableMap());
}
}
/**
* Performs a DELETE on the database, given a OrderProduct or Criteria object OR a primary key value.
*
* @param mixed $values Criteria or OrderProduct object or primary key or array of primary keys
* which is used to create the DELETE statement
* @param ConnectionInterface $con the connection to use
* @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows
* if supported by native driver or if emulated using Propel.
* @throws PropelException Any exceptions caught during processing will be
* rethrown wrapped into a PropelException.
*/
public static function doDelete($values, ConnectionInterface $con = null)
{
if (null === $con) {
$con = Propel::getServiceContainer()->getWriteConnection(OrderProductTableMap::DATABASE_NAME);
}
if ($values instanceof Criteria) {
// rename for clarity
$criteria = $values;
} elseif ($values instanceof \Thelia\Model\OrderProduct) { // it's a model object
// create criteria based on pk values
$criteria = $values->buildPkeyCriteria();
} else { // it's a primary key, or an array of pks
$criteria = new Criteria(OrderProductTableMap::DATABASE_NAME);
$criteria->add(OrderProductTableMap::ID, (array) $values, Criteria::IN);
}
$query = OrderProductQuery::create()->mergeWith($criteria);
if ($values instanceof Criteria) { OrderProductTableMap::clearInstancePool();
} elseif (!is_object($values)) { // it's a primary key, or an array of pks
foreach ((array) $values as $singleval) { OrderProductTableMap::removeInstanceFromPool($singleval);
}
}
return $query->delete($con);
}
/**
* Deletes all rows from the order_product table.
*
* @param ConnectionInterface $con the connection to use
* @return int The number of affected rows (if supported by underlying database driver).
*/
public static function doDeleteAll(ConnectionInterface $con = null)
{
return OrderProductQuery::create()->doDeleteAll($con);
}
/**
* Performs an INSERT on the database, given a OrderProduct or Criteria object.
*
* @param mixed $criteria Criteria or OrderProduct object containing data that is used to create the INSERT statement.
* @param ConnectionInterface $con the ConnectionInterface connection to use
* @return mixed The new primary key.
* @throws PropelException Any exceptions caught during processing will be
* rethrown wrapped into a PropelException.
*/
public static function doInsert($criteria, ConnectionInterface $con = null)
{
if (null === $con) {
$con = Propel::getServiceContainer()->getWriteConnection(OrderProductTableMap::DATABASE_NAME);
}
if ($criteria instanceof Criteria) {
$criteria = clone $criteria; // rename for clarity
} else {
$criteria = $criteria->buildCriteria(); // build Criteria from OrderProduct object
}
if ($criteria->containsKey(OrderProductTableMap::ID) && $criteria->keyContainsValue(OrderProductTableMap::ID) ) {
throw new PropelException('Cannot insert a value for auto-increment primary key ('.OrderProductTableMap::ID.')');
}
// Set the correct dbName
$query = OrderProductQuery::create()->mergeWith($criteria);
try {
// use transaction because $criteria could contain info
// for more than one table (I guess, conceivably)
$con->beginTransaction();
$pk = $query->doInsert($con);
$con->commit();
} catch (PropelException $e) {
$con->rollBack();
throw $e;
}
return $pk;
}
} // OrderProductTableMap
// This is the static code needed to register the TableMap for this table with the main Propel class.
//
OrderProductTableMap::buildTableMap();

View File

@@ -3,9 +3,12 @@
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Propel;
use Thelia\Core\Event\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Order as BaseOrder;
use Thelia\Model\Map\OrderTableMap;
use \PDO;
class Order extends BaseOrder
{
@@ -19,11 +22,19 @@ class Order extends BaseOrder
*/
public function preInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::ORDER_SET_REFERENCE, new OrderEvent($this));
$this->dispatchEvent(TheliaEvents::ORDER_BEFORE_CREATE, new OrderEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::ORDER_AFTER_CREATE, new OrderEvent($this));
}
/**
* calculate the total amount
*
@@ -35,4 +46,171 @@ class Order extends BaseOrder
{
return 2;
}
/**
* PROPEL SHOULD FIX IT
*
* Insert the row in the database.
*
* @param ConnectionInterface $con
*
* @throws PropelException
* @see doSave()
*/
protected function doInsert(ConnectionInterface $con)
{
$modifiedColumns = array();
$index = 0;
$this->modifiedColumns[] = OrderTableMap::ID;
if (null !== $this->id) {
throw new PropelException('Cannot insert a value for auto-increment primary key (' . OrderTableMap::ID . ')');
}
// check the columns in natural order for more readable SQL queries
if ($this->isColumnModified(OrderTableMap::ID)) {
$modifiedColumns[':p' . $index++] = 'ID';
}
if ($this->isColumnModified(OrderTableMap::REF)) {
$modifiedColumns[':p' . $index++] = 'REF';
}
if ($this->isColumnModified(OrderTableMap::CUSTOMER_ID)) {
$modifiedColumns[':p' . $index++] = 'CUSTOMER_ID';
}
if ($this->isColumnModified(OrderTableMap::INVOICE_ORDER_ADDRESS_ID)) {
$modifiedColumns[':p' . $index++] = 'INVOICE_ORDER_ADDRESS_ID';
}
if ($this->isColumnModified(OrderTableMap::DELIVERY_ORDER_ADDRESS_ID)) {
$modifiedColumns[':p' . $index++] = 'DELIVERY_ORDER_ADDRESS_ID';
}
if ($this->isColumnModified(OrderTableMap::INVOICE_DATE)) {
$modifiedColumns[':p' . $index++] = 'INVOICE_DATE';
}
if ($this->isColumnModified(OrderTableMap::CURRENCY_ID)) {
$modifiedColumns[':p' . $index++] = 'CURRENCY_ID';
}
if ($this->isColumnModified(OrderTableMap::CURRENCY_RATE)) {
$modifiedColumns[':p' . $index++] = 'CURRENCY_RATE';
}
if ($this->isColumnModified(OrderTableMap::TRANSACTION_REF)) {
$modifiedColumns[':p' . $index++] = 'TRANSACTION_REF';
}
if ($this->isColumnModified(OrderTableMap::DELIVERY_REF)) {
$modifiedColumns[':p' . $index++] = 'DELIVERY_REF';
}
if ($this->isColumnModified(OrderTableMap::INVOICE_REF)) {
$modifiedColumns[':p' . $index++] = 'INVOICE_REF';
}
if ($this->isColumnModified(OrderTableMap::POSTAGE)) {
$modifiedColumns[':p' . $index++] = 'POSTAGE';
}
if ($this->isColumnModified(OrderTableMap::PAYMENT_MODULE_ID)) {
$modifiedColumns[':p' . $index++] = 'PAYMENT_MODULE_ID';
}
if ($this->isColumnModified(OrderTableMap::DELIVERY_MODULE_ID)) {
$modifiedColumns[':p' . $index++] = 'DELIVERY_MODULE_ID';
}
if ($this->isColumnModified(OrderTableMap::STATUS_ID)) {
$modifiedColumns[':p' . $index++] = 'STATUS_ID';
}
if ($this->isColumnModified(OrderTableMap::LANG_ID)) {
$modifiedColumns[':p' . $index++] = 'LANG_ID';
}
if ($this->isColumnModified(OrderTableMap::CREATED_AT)) {
$modifiedColumns[':p' . $index++] = 'CREATED_AT';
}
if ($this->isColumnModified(OrderTableMap::UPDATED_AT)) {
$modifiedColumns[':p' . $index++] = 'UPDATED_AT';
}
$db = Propel::getServiceContainer()->getAdapter(OrderTableMap::DATABASE_NAME);
if ($db->useQuoteIdentifier()) {
$tableName = $db->quoteIdentifierTable(OrderTableMap::TABLE_NAME);
} else {
$tableName = OrderTableMap::TABLE_NAME;
}
$sql = sprintf(
'INSERT INTO %s (%s) VALUES (%s)',
$tableName,
implode(', ', $modifiedColumns),
implode(', ', array_keys($modifiedColumns))
);
try {
$stmt = $con->prepare($sql);
foreach ($modifiedColumns as $identifier => $columnName) {
switch ($columnName) {
case 'ID':
$stmt->bindValue($identifier, $this->id, PDO::PARAM_INT);
break;
case 'REF':
$stmt->bindValue($identifier, $this->ref, PDO::PARAM_STR);
break;
case 'CUSTOMER_ID':
$stmt->bindValue($identifier, $this->customer_id, PDO::PARAM_INT);
break;
case 'INVOICE_ORDER_ADDRESS_ID':
$stmt->bindValue($identifier, $this->invoice_order_address_id, PDO::PARAM_INT);
break;
case 'DELIVERY_ORDER_ADDRESS_ID':
$stmt->bindValue($identifier, $this->delivery_order_address_id, PDO::PARAM_INT);
break;
case 'INVOICE_DATE':
$stmt->bindValue($identifier, $this->invoice_date ? $this->invoice_date->format("Y-m-d H:i:s") : null, PDO::PARAM_STR);
break;
case 'CURRENCY_ID':
$stmt->bindValue($identifier, $this->currency_id, PDO::PARAM_INT);
break;
case 'CURRENCY_RATE':
$stmt->bindValue($identifier, $this->currency_rate, PDO::PARAM_STR);
break;
case 'TRANSACTION_REF':
$stmt->bindValue($identifier, $this->transaction_ref, PDO::PARAM_STR);
break;
case 'DELIVERY_REF':
$stmt->bindValue($identifier, $this->delivery_ref, PDO::PARAM_STR);
break;
case 'INVOICE_REF':
$stmt->bindValue($identifier, $this->invoice_ref, PDO::PARAM_STR);
break;
case 'POSTAGE':
$stmt->bindValue($identifier, $this->postage, PDO::PARAM_STR);
break;
case 'PAYMENT_MODULE_ID':
$stmt->bindValue($identifier, $this->payment_module_id, PDO::PARAM_INT);
break;
case 'DELIVERY_MODULE_ID':
$stmt->bindValue($identifier, $this->delivery_module_id, PDO::PARAM_INT);
break;
case 'STATUS_ID':
$stmt->bindValue($identifier, $this->status_id, PDO::PARAM_INT);
break;
case 'LANG_ID':
$stmt->bindValue($identifier, $this->lang_id, PDO::PARAM_INT);
break;
case 'CREATED_AT':
$stmt->bindValue($identifier, $this->created_at ? $this->created_at->format("Y-m-d H:i:s") : null, PDO::PARAM_STR);
break;
case 'UPDATED_AT':
$stmt->bindValue($identifier, $this->updated_at ? $this->updated_at->format("Y-m-d H:i:s") : null, PDO::PARAM_STR);
break;
}
}
$stmt->execute();
} catch (Exception $e) {
Propel::log($e->getMessage(), Propel::LOG_ERR);
throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e);
}
try {
$pk = $con->lastInsertId();
} catch (Exception $e) {
throw new PropelException('Unable to get autoincrement id.', 0, $e);
}
$this->setId($pk);
$this->setNew(false);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Event\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\OrderProduct as BaseOrderProduct;
class OrderProduct extends BaseOrderProduct
{
use \Thelia\Model\Tools\ModelEventDispatcherTrait;
/**
* {@inheritDoc}
*/
public function preInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::ORDER_PRODUCT_BEFORE_CREATE, new OrderEvent($this->getOrder()));
return true;
}
/**
* {@inheritDoc}
*/
public function postInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::ORDER_PRODUCT_AFTER_CREATE, new OrderEvent($this->getOrder()));
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Thelia\Model;
use Thelia\Model\Base\OrderProductQuery as BaseOrderProductQuery;
/**
* Skeleton subclass for performing query and update operations on the 'order_product' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
*/
class OrderProductQuery extends BaseOrderProductQuery
{
} // OrderProductQuery