/** * @params Product $product * * @depends testCreate */ public function testRead(Product $product) { $metaDatas = MetaDataQuery::create()->filterByElementKey(get_class($product))->filterByElementId($product->getId())->find(); $this->assertEquals($metaDatas->count(), 2); $metaData = MetaDataQuery::create()->filterByMetaKey('test')->filterByElementKey(get_class($product))->filterByElementId($product->getId())->findOne(); $this->assertNotNull($metaData); $this->assertEquals('test', $metaData->getMetaKey()); $this->assertEquals(get_class($product), $metaData->getElementKey()); $this->assertEquals($product->getId(), $metaData->getElementId()); $this->assertEquals('test', $metaData->getValue()); $this->assertEquals(false, $metaData->getIsSerialized()); $datas = MetaDataQuery::getAllVal(get_class($product), $product->getId()); $this->assertEquals(count($datas), 2); $this->assertEquals($datas['test'], 'test'); $this->assertEquals($datas['test2'], array("fr_FR" => "bonjour", "en_US" => "Hello")); return $product; }
/** * Retrieve meta data associated to an element * * params should contain at least key an id attributes. Thus it will return * an array of associated data. * * If meta argument is specified then it will return an unique value. * * @param array $params * @param \Smarty $smarty * * @throws \InvalidArgumentException * * @return string|array|null */ public function metaAccess($params, $smarty) { $meta = $this->getParam($params, 'meta', null); $key = $this->getParam($params, 'key', null); $id = $this->getParam($params, 'id', null); $cacheKey = sprintf('meta_%s_%s_%s', $meta, $key, $id); $out = null; if (array_key_exists($cacheKey, self::$dataAccessCache)) { return self::$dataAccessCache[$cacheKey]; } if ($key !== null && $id !== null) { if ($meta === null) { $out = MetaDataQuery::getAllVal($key, (int) $id); } else { $out = MetaDataQuery::getVal($meta, $key, (int) $id); } } else { throw new \InvalidArgumentException("key and id arguments are required in meta access function"); } self::$dataAccessCache[$cacheKey] = $out; if (!empty($params['out'])) { $smarty->assign($params['out'], $out); return $out !== null ? true : false; } else { if (is_array($out)) { throw new \InvalidArgumentException('The argument "out" is required if the meta value is an array'); } return $out; } }
/** * Retrieve meta data associated to an element * * params should contain at least key an id attributes. Thus it will return * an array of associated datas. * * If meta argument is specified then it will return an unique value. * * @param $params * @param $smarty * * @throws \InvalidArgumentException * * @return string|array|null */ public function metaAccess($params, $smarty) { $meta = $this->getParam($params, 'meta', null); $key = $this->getParam($params, 'key', null); $id = $this->getParam($params, 'id', null); $cacheKey = sprintf('meta_%s_%s_%s', $meta, $key, $id); $out = null; if (array_key_exists($cacheKey, self::$dataAccessCache)) { return self::$dataAccessCache[$cacheKey]; } if ($key !== null && $id !== null) { if ($meta === null) { $out = MetaDataQuery::getAllVal($key, (int) $id); } else { $out = MetaDataQuery::getVal($meta, $key, (int) $id); } } else { throw new \InvalidArgumentException("key and id attributes are required in meta access function"); } self::$dataAccessCache[$cacheKey] = $out; return $out; }