/** * This function prepares the posted extJs data of the article model. * @param $data * @return array */ protected function prepareArticleAssociatedData($data) { //check if a tax id is passed and load the tax model or set the tax parameter to null. if (!empty($data['taxId'])) { $data['tax'] = Shopware()->Models()->find('Shopware\\Models\\Tax\\Tax', $data['taxId']); } else { $data['tax'] = null; } //check if a supplier id is passed and load the supplier model or set the supplier parameter to null. if (!empty($data['supplierId'])) { $data['supplier'] = Shopware()->Models()->find('Shopware\\Models\\Article\\Supplier', $data['supplierId']); } elseif (!empty($data['supplierName'])) { $supplier = $this->getManager()->getRepository('Shopware\\Models\\Article\\Supplier')->findOneBy(array('name' => trim($data['supplierName']))); if (!$supplier) { $supplier = new \Shopware\Models\Article\Supplier(); $supplier->setName($data['supplierName']); } $data['supplier'] = $supplier; } else { $data['supplier'] = null; } //check if a supplier id is passed and load the supplier model or set the supplier parameter to null. if (!empty($data['priceGroupId'])) { $data['priceGroup'] = Shopware()->Models()->find('Shopware\\Models\\Price\\Group', $data['priceGroupId']); } else { $data['priceGroup'] = null; } if (!empty($data['filterGroupId'])) { $data['propertyGroup'] = Shopware()->Models()->find('Shopware\\Models\\Property\\Group', $data['filterGroupId']); } else { $data['propertyGroup'] = null; } $data['changed'] = new \DateTime(); return $data; }
/** * @param array $data * @throws \Shopware\Components\Api\Exception\CustomValidationException * @return array */ protected function prepareArticleAssociatedData($data, ArticleModel $article) { //check if a tax id is passed and load the tax model or set the tax parameter to null. if (!empty($data['taxId'])) { $data['tax'] = $this->getManager()->find('Shopware\Models\Tax\Tax', $data['taxId']); if (empty($data['tax'])) { throw new ApiException\CustomValidationException(sprintf("Tax by id %s not found", $data['taxId'])); } } elseif (!empty($data['tax'])) { $tax = $this->getManager()->getRepository('Shopware\Models\Tax\Tax')->findOneBy(array('tax' => $data['tax'])); if (!$tax) { throw new ApiException\CustomValidationException(sprintf("Tax by taxrate %s not found", $data['tax'])); } $data['tax'] = $tax; } else { unset($data['tax']); } //check if a supplier id is passed and load the supplier model or set the supplier parameter to null. if (!empty($data['supplierId'])) { $data['supplier'] = $this->getManager()->find('Shopware\Models\Article\Supplier', $data['supplierId']); if (empty($data['supplier'])) { throw new ApiException\CustomValidationException(sprintf("Supplier by id %s not found", $data['supplierId'])); } } elseif (!empty($data['supplier'])) { $supplier = $this->getManager()->getRepository('Shopware\Models\Article\Supplier')->findOneBy(array('name' => $data['supplier'])); if (!$supplier) { $supplier = new \Shopware\Models\Article\Supplier(); $supplier->setName($data['supplier']); } $data['supplier'] = $supplier; } else { unset($data['supplier']); } //check if a priceGroup id is passed and load the priceGroup model or set the priceGroup parameter to null. if (isset($data['priceGroupId'])) { if (empty($data['priceGroupId'])) { $data['priceGroupId'] = null; } else { $data['priceGroup'] = $this->getManager()->find('Shopware\Models\Price\Group', $data['priceGroupId']); if (empty($data['priceGroup'])) { throw new ApiException\CustomValidationException(sprintf("Pricegroup by id %s not found", $data['priceGroupId'])); } } } else { unset($data['priceGroup']); } //check if a propertyGroup is passed and load the propertyGroup model or set the propertyGroup parameter to null. if (isset($data['filterGroupId'])) { if (empty($data['filterGroupId'])) { $data['propertyGroup'] = null; } else { $data['propertyGroup'] = $this->getManager()->find('Shopware\Models\Property\Group', $data['filterGroupId']); if (empty($data['propertyGroup'])) { throw new ApiException\CustomValidationException(sprintf("PropertyGroup by id %s not found", $data['filterGroupId'])); } } } else { unset($data['propertyGroup']); } return $data; }
/** * @expectedException \InvalidArgumentException */ public function testCanReAssignWithAnotherIdThrowsExceptionManyToOne() { $article = new Article(); $supplier = new \Shopware\Models\Article\Supplier(); $supplier->setName("test"); $supplier->setDescription('description'); $this->setProperty($supplier, 'id', 1); $article->setSupplier($supplier); $this->assertSame($supplier, $article->getSupplier()); $data = array('supplier' => array('id' => '2', 'name' => 'foo')); $article->fromArray($data); }