function testShouldSetAttributesThroughConstructor() { $product = new Product(array('attributes' => array('color' => 'red', 'size' => 'small'))); $expected = array('color' => 'red', 'size' => 'small'); $this->assertEquals($expected, $product->attributes(), 'should set attributes through constructor'); }
function editAction() { $form = new Form($this->categoryMapper()); if ($this->params('id')) { $product = $this->productMapper()->load($this->params('id')); $form->populate(array('name' => $product->getName(), 'description' => $product->getDescription(), 'sku' => $product->getSku(), 'basePrice' => $product->getBasePrice(), 'categories' => $product->getCategories())); } else { $product = new Product(); } if ($this->getRequest()->isPost() && $form->isValid($this->params()->fromPost())) { //print_r($_POST);exit; /** * Save the uploaded image ... [if there is one] */ $image_hash_to_add = false; $file = $_FILES['image_to_add']['tmp_name']; if ($file) { $saver = new Saver(file_get_contents($file)); $saver->save(); $image_hash_to_add = $saver->getHash(); } /** * Set scalar properties & default image */ $product->setSku($form->getValue('sku')); $product->setName($form->getValue('name')); $product->setDescription($form->getValue('description')); $product->setBasePrice($form->getValue('basePrice')); $product->setCategories($form->getValue('categories')); $product->setDefaultImageHash($this->params()->fromPost('default_image')); /** * Add new image ... [if one was uploaded] */ if ($image_hash_to_add) { $product->addImageHash($image_hash_to_add); } /** * Update existing attributes */ foreach ($product->attributes() as $attribute => $existingValue) { $newValue = $this->params()->fromPost("attribute_{$attribute}"); $product->setAttributeValue($attribute, $newValue); } if ($this->params()->fromPost('new_attribute_label') && $this->params()->fromPost('new_attribute_value')) { $product->setAttributeValue($this->params()->fromPost('new_attribute_label'), $this->params()->fromPost('new_attribute_value')); } /** * Save & redirect */ $this->productMapper()->save($product); $this->flashMessenger()->setNamespace('success')->addMessage('Saved Product'); if (!$this->params()->fromPost('save_and_continue')) { return $this->redirect()->toRoute('product_manage'); } } else { if ($this->getRequest()->isPost()) { $this->flashMessenger()->setNamespace('error')->addMessage('One of the fields is not valid. Product NOT saved.'); } } return array('form' => $form, 'product' => $product); }