本文整理匯總了PHP中Pim\Bundle\CatalogBundle\Model\ProductInterface::addCategory方法的典型用法代碼示例。如果您正苦於以下問題:PHP ProductInterface::addCategory方法的具體用法?PHP ProductInterface::addCategory怎麽用?PHP ProductInterface::addCategory使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Pim\Bundle\CatalogBundle\Model\ProductInterface
的用法示例。
在下文中一共展示了ProductInterface::addCategory方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1:
function it_adds_category_field($categoryRepository, ProductInterface $product, CategoryInterface $mug, CategoryInterface $shirt, CategoryInterface $men)
{
$categoryRepository->findOneByIdentifier('mug')->willReturn($mug);
$categoryRepository->findOneByIdentifier('shirt')->willReturn($shirt);
$product->getCategories()->willReturn([$men]);
$product->addCategory($mug)->shouldBeCalled();
$product->addCategory($shirt)->shouldBeCalled();
$this->addFieldData($product, 'categories', ['mug', 'shirt']);
}
示例2: addFieldData
/**
* {@inheritdoc}
*
* Expected data input format : ["category_code"]
*/
public function addFieldData(ProductInterface $product, $field, $data, array $options = [])
{
$this->checkData($field, $data);
$categories = [];
foreach ($data as $categoryCode) {
$category = $this->categoryRepository->findOneByIdentifier($categoryCode);
if (null !== $category) {
$categories[] = $category;
} else {
throw InvalidArgumentException::expected($field, 'existing category code', 'adder', 'category', $categoryCode);
}
}
foreach ($categories as $category) {
$product->addCategory($category);
}
}
示例3: setFieldData
/**
* {@inheritdoc}
*
* Expected data input format : ["category_code"]
*/
public function setFieldData(ProductInterface $product, $field, $data, array $options = [])
{
$this->checkData($field, $data);
$categories = [];
foreach ($data as $categoryCode) {
$category = $this->getCategory($categoryCode);
if (null === $category) {
throw InvalidArgumentException::expected($field, 'existing category code', 'setter', 'category', $categoryCode);
} else {
$categories[] = $category;
}
}
$oldCategories = $product->getCategories();
foreach ($oldCategories as $category) {
$product->removeCategory($category);
}
foreach ($categories as $category) {
$product->addCategory($category);
}
}
示例4: denormalizeCategories
/**
* Denormalize product categories
*
* @param string $data
* @param string $format
* @param array $context
* @param ProductInterface $product
*/
protected function denormalizeCategories($data, $format, array $context, ProductInterface $product)
{
foreach ($product->getCategories() as $category) {
$product->removeCategory($category);
}
$categoryCodes = strlen($data) > 0 ? explode(",", $data) : array();
foreach ($categoryCodes as $categoryCode) {
$product->addCategory($this->serializer->denormalize($categoryCode, $this->categoryClass, $format, $context));
}
}
示例5: doPerform
/**
* {@inheritdoc}
*/
protected function doPerform(ProductInterface $product)
{
foreach ($this->categories as $category) {
$product->addCategory($category);
}
}