public function testLoadSpecification() { ActiveRecord::removeFromPool($this->product); $this->product = Product::getInstanceByID($this->product->getID(), true); $this->product->loadSpecification(); // save as soon as the specification is loaded to make sure all associated objects are marked as existing. // and won't be re-inserted in database try { $this->product->save(); $this->pass(); } catch (Exception $e) { $this->fail(); } // set prices foreach ($this->product->getApplication()->getCurrencyArray() as $currency) { $this->product->setPrice($currency, 111); } $this->product->save(); $arr = $this->product->toArray(); foreach ($this->product->getApplication()->getCurrencyArray() as $currency) { $this->assertEqual($arr['price_' . $currency], 111); } // re-run all the previous tests on the restored object $this->testSimpleValues(); $this->testSingleSelectValues(); $this->testMultipleSelectValues(); $arr = $this->product->toArray(); }
/** * Exports the object as an array. * * You can specify the key type of the array by passing one of the class * type constants. * * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME, * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. * Defaults to TableMap::TYPE_PHPNAME. * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE. * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. * * @return array an associative array containing the field names (as keys) and field values */ public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) { if (isset($alreadyDumpedObjects['LegacyProductAttributeValuePrice'][serialize($this->getPrimaryKey())])) { return '*RECURSION*'; } $alreadyDumpedObjects['LegacyProductAttributeValuePrice'][serialize($this->getPrimaryKey())] = true; $keys = LegacyProductAttributeValuePriceTableMap::getFieldNames($keyType); $result = array($keys[0] => $this->getProductId(), $keys[1] => $this->getAttributeAvId(), $keys[2] => $this->getCurrencyId(), $keys[3] => $this->getDelta()); $virtualColumns = $this->virtualColumns; foreach ($virtualColumns as $key => $virtualColumn) { $result[$key] = $virtualColumn; } if ($includeForeignObjects) { if (null !== $this->aProduct) { $result['Product'] = $this->aProduct->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } if (null !== $this->aAttributeAv) { $result['AttributeAv'] = $this->aAttributeAv->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } if (null !== $this->aCurrency) { $result['Currency'] = $this->aCurrency->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } } return $result; }
public function __construct() { $products = array(); for ($i = 0; $i < self::TOTAL_PRODUCTS; $i++) { $randomProduct = new Product(); $products[] = $randomProduct->toArray(); } echo json_encode($products); }
public function testProductToArrayMethod() { $product = new Product(); $product->title = 'title'; $product->size = 1024; $product->description = 'description'; $product->unit_price = 1.5; $expected = ['title' => 'title', 'size' => 1024, 'description' => 'description', 'unit_price' => 1.5]; $this->assertEquals($expected, $product->toArray()); }
/** * Exports the object as an array. * * You can specify the key type of the array by passing one of the class * type constants. * * @param string $keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM. * Defaults to BasePeer::TYPE_PHPNAME. * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to true. * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. * * @return array an associative array containing the field names (as keys) and field values */ public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) { if (isset($alreadyDumpedObjects['UserProduct'][serialize($this->getPrimaryKey())])) { return '*RECURSION*'; } $alreadyDumpedObjects['UserProduct'][serialize($this->getPrimaryKey())] = true; $keys = UserProductPeer::getFieldNames($keyType); $result = array($keys[0] => $this->getUserId(), $keys[1] => $this->getProductId(), $keys[2] => $this->getExpires()); if ($includeForeignObjects) { if (null !== $this->aUser) { $result['User'] = $this->aUser->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } if (null !== $this->aProduct) { $result['Product'] = $this->aProduct->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } } return $result; }
/** * Exports the object as an array. * * You can specify the key type of the array by passing one of the class * type constants. * * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME, * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. * Defaults to TableMap::TYPE_PHPNAME. * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE. * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. * * @return array an associative array containing the field names (as keys) and field values */ public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) { if (isset($alreadyDumpedObjects['DigressivePrice'][$this->getPrimaryKey()])) { return '*RECURSION*'; } $alreadyDumpedObjects['DigressivePrice'][$this->getPrimaryKey()] = true; $keys = DigressivePriceTableMap::getFieldNames($keyType); $result = array($keys[0] => $this->getId(), $keys[1] => $this->getProductId(), $keys[2] => $this->getPrice(), $keys[3] => $this->getPromoPrice(), $keys[4] => $this->getQuantityFrom(), $keys[5] => $this->getQuantityTo()); $virtualColumns = $this->virtualColumns; foreach ($virtualColumns as $key => $virtualColumn) { $result[$key] = $virtualColumn; } if ($includeForeignObjects) { if (null !== $this->aProduct) { $result['Product'] = $this->aProduct->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } } return $result; }
/** * Exports the object as an array. * * You can specify the key type of the array by passing one of the class * type constants. * * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME, * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. * Defaults to TableMap::TYPE_PHPNAME. * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE. * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. * * @return array an associative array containing the field names (as keys) and field values */ public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) { if (isset($alreadyDumpedObjects['GoogleshoppingProductSynchronisation'][$this->getPrimaryKey()])) { return '*RECURSION*'; } $alreadyDumpedObjects['GoogleshoppingProductSynchronisation'][$this->getPrimaryKey()] = true; $keys = GoogleshoppingProductSynchronisationTableMap::getFieldNames($keyType); $result = array($keys[0] => $this->getId(), $keys[1] => $this->getProductId(), $keys[2] => $this->getTargetCountry(), $keys[3] => $this->getLang(), $keys[4] => $this->getSyncEnable(), $keys[5] => $this->getGoogleshoppingAccountId()); $virtualColumns = $this->virtualColumns; foreach ($virtualColumns as $key => $virtualColumn) { $result[$key] = $virtualColumn; } if ($includeForeignObjects) { if (null !== $this->aProduct) { $result['Product'] = $this->aProduct->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } if (null !== $this->aGoogleshoppingAccount) { $result['GoogleshoppingAccount'] = $this->aGoogleshoppingAccount->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } } return $result; }
private function productForm(Product $product) { $productFormData = $product->toArray(); if ($product->isLoaded()) { $product->loadSpecification(); $productFormData = array_merge($productFormData, $product->getSpecification()->getFormData()); if (isset($productFormData['Manufacturer']['name'])) { $productFormData['manufacturer'] = $productFormData['Manufacturer']['name']; } } else { $product->load(ActiveRecord::LOAD_REFERENCES); } $product->loadPricing(); $form = $this->buildForm($product); $pricing = $product->getPricingHandler(); $pricesData = $product->toArray(); $listPrices = $pricing->toArray(ProductPricing::DEFINED, ProductPricing::LIST_PRICE); $pricesData['shippingHiUnit'] = (int) $pricesData['shippingWeight']; $pricesData['shippingLoUnit'] = ($pricesData['shippingWeight'] - $pricesData['shippingHiUnit']) * 1000; if (array_key_exists('defined', $pricesData)) { foreach ($pricesData['calculated'] as $currency => $price) { $pricesData['price_' . $currency] = isset($pricesData['defined'][$currency]) ? $pricesData['defined'][$currency] : ''; $productFormData['price_' . $currency] = $pricesData['price_' . $currency]; } } foreach ($listPrices as $currency => $price) { $pricesData['listPrice_' . $currency] = $price; } $form->setData($productFormData); // status values $status = array(0 => $this->translate('_disabled'), 1 => $this->translate('_enabled')); // product types $types = array(Product::TYPE_TANGIBLE => $this->translate('_tangible'), Product::TYPE_DOWNLOADABLE => $this->translate('_intangible'), Product::TYPE_BUNDLE => $this->translate('_bundle')); // default product type if (!$product->isLoaded()) { $product->type->set(substr($this->config->get('DEFAULT_PRODUCT_TYPE'), -1)); $form->set('type', $product->type->get()); } $response = new ActionResponse(); $product->getSpecification()->setFormResponse($response, $form); $response->set("cat", $product->getCategory()->getID()); $response->set("hideFeedbackMessage", $this->request->get("afterAdding") == 'on'); $response->set("productForm", $form); $response->set("path", $product->getCategory()->getPathNodeArray(true)); $response->set("productTypes", $types); $response->set("productStatuses", $status); $response->set("baseCurrency", $this->application->getDefaultCurrency()->getID()); $response->set("otherCurrencies", $this->application->getCurrencyArray(LiveCart::EXCLUDE_DEFAULT_CURRENCY)); $response->set("shippingClasses", $this->getSelectOptionsFromSet(ShippingClass::getAllClasses())); $response->set("taxClasses", $this->getSelectOptionsFromSet(TaxClass::getAllClasses())); $productData = $product->toArray(); if (empty($productData['ID'])) { $productData['ID'] = 0; } $response->set("product", $productData); return $response; }
* create new product */ $app->post('/products', function () use($app) { disable_cache($app); // only admins can create products if_is_admin(function () use($app) { try { $params = json_decode($app->request()->getBody(), true); $product = new Product(); $product->setName($params['name']); $product->setCreatedAt(time()); if (isset($params['data'])) { $product->setData(json_encode($params['data'])); } $product->save(); ok($product->toArray()); } catch (Exception $e) { error('io-error', $e->getMessage()); } }); }); /* * change product */ $app->put('/products/:id', function ($id) use($app) { if_is_admin(function () use($app, $id) { $product = ProductQuery::create()->findPk($id); if ($product) { $params = json_decode($app->request()->getBody(), true); $product->setName($params['name']); $product->setData(json_encode($params['data']));
/** * Exports the object as an array. * * You can specify the key type of the array by passing one of the class * type constants. * * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME, * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. * Defaults to TableMap::TYPE_PHPNAME. * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE. * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. * * @return array an associative array containing the field names (as keys) and field values */ public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) { if (isset($alreadyDumpedObjects['ProductDelay'][$this->getPrimaryKey()])) { return '*RECURSION*'; } $alreadyDumpedObjects['ProductDelay'][$this->getPrimaryKey()] = true; $keys = ProductDelayTableMap::getFieldNames($keyType); $result = array($keys[0] => $this->getId(), $keys[1] => $this->getProductId(), $keys[2] => $this->getDeliveryDelayMin(), $keys[3] => $this->getDeliveryDelayMax(), $keys[4] => $this->getRestockDelayMin(), $keys[5] => $this->getRestockDelayMax(), $keys[6] => $this->getDeliveryDateStart(), $keys[7] => $this->getDeliveryType()); $virtualColumns = $this->virtualColumns; foreach ($virtualColumns as $key => $virtualColumn) { $result[$key] = $virtualColumn; } if ($includeForeignObjects) { if (null !== $this->aProduct) { $result['Product'] = $this->aProduct->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } } return $result; }
public function edit_dish($idDish = null) { $flash_string = ''; if (Session::exists('account')) { $flash_string = Session::flash('account'); } $user = new User(); if ($user->isLoggedIn()) { if (isset($idDish)) { $dish = Dish::toArray($idRecipe); $units = Unit::toArray(); $product = Product::toArray(); $this->view('account/dish_edit', ['register' => true, 'loggedIn' => 1, 'flash' => $flash_string, 'name' => $user->data()->name, 'page_name' => "Edit dish", 'products' => json_encode($product), 'user_id' => $user->data()->id, 'dish' => json_encode($dish), 'units' => json_encode($units)]); } else { Redirect::to('account/dishes'); } } else { Session::flash('home', 'You have been logged out, please log back in'); Redirect::to('home'); } }