/**
  * Populate Element
  *
  * @param BaseElementModel $element Element
  * @param Request          $request Request
  *
  * @return BaseElementModel Element
  */
 public function populateElement(BaseElementModel $element, Request $request)
 {
     $fields_key = craft()->config->get('contentModelFieldsLocation', 'restfulApi');
     $attributes = $request->getParsedBody();
     $element->setAttributes($attributes);
     if (isset($attributes[$fields_key])) {
         if (isset($attributes[$fields_key]['title'])) {
             $element->getContent()->title = $attributes[$fields_key]['title'];
         }
         $element->setContent($attributes[$fields_key]);
     }
     return $element;
 }
 /**
  * Save a model into DB
  *
  * @param BaseElementModel $model
  *
  * @return bool
  * @throws \CDbException
  * @throws \Exception
  */
 public function save(BaseElementModel $model)
 {
     $productTypeId = craft()->db->createCommand()->select('typeId')->from('market_products')->where('id=:id', [':id' => $model->productId])->queryScalar();
     $productType = craft()->market_productType->getById($productTypeId);
     if ($model->id) {
         $record = Market_VariantRecord::model()->findById($model->id);
         if (!$record) {
             throw new HttpException(404);
         }
     } else {
         $record = new Market_VariantRecord();
     }
     /* @var Market_VariantModel $model */
     $record->isImplicit = $model->isImplicit;
     $record->productId = $model->productId;
     // We dont ask for a sku when dealing with a product with variants
     if ($model->isImplicit && $productType->hasVariants) {
         $model->sku = 'implicitSkuOfProductId' . $model->productId;
         $record->sku = $model->sku;
     } else {
         $record->sku = $model->sku;
     }
     if (!$productType->titleFormat) {
         $productType->titleFormat = "{sku}";
     }
     // implicit variant has no custom field data so play it safe and default it to sku.
     if ($model->isImplicit) {
         $productType->titleFormat = "{sku}";
     }
     $model->getContent()->title = craft()->templates->renderObjectTemplate($productType->titleFormat, $model);
     $record->price = $model->price;
     $record->width = $model->width;
     $record->height = $model->height;
     $record->length = $model->length;
     $record->weight = $model->weight;
     $record->minQty = $model->minQty;
     $record->maxQty = $model->maxQty;
     $record->stock = $model->stock;
     $record->unlimitedStock = $model->unlimitedStock;
     if ($model->unlimitedStock && $record->stock == "") {
         $model->stock = 0;
         $record->stock = 0;
     }
     $record->validate();
     $model->addErrors($record->getErrors());
     MarketDbHelper::beginStackedTransaction();
     try {
         if (!$model->hasErrors()) {
             if (craft()->market_purchasable->saveElement($model)) {
                 $record->id = $model->id;
                 $record->save(false);
                 MarketDbHelper::commitStackedTransaction();
                 return true;
             }
         }
     } catch (\Exception $e) {
         MarketDbHelper::rollbackStackedTransaction();
         throw $e;
     }
     MarketDbHelper::rollbackStackedTransaction();
     return false;
 }