/**
  * @param array $category
  * @return bool|int
  */
 public function import(array $category)
 {
     $category = $this->prepareCategoryData($category);
     // Try to find an existing category by name and parent
     $model = null;
     if (isset($category['parent']) && isset($category['name'])) {
         $model = $this->repository->findOneBy(['parent' => $category['parent'], 'name' => $category['name']]);
     }
     if (!$model instanceof Category) {
         $model = new Category();
     }
     $parentModel = null;
     if (isset($category['parent'])) {
         $parentModel = $this->repository->find((int) $category['parent']);
         if (!$parentModel instanceof Category) {
             $this->logger->error("Parent category {$category['parent']} not found!");
             return false;
         }
     }
     $model->fromArray($category);
     $model->setParent($parentModel);
     $this->em->persist($model);
     $this->em->flush();
     // Set category attributes
     $attributes = $this->prepareCategoryAttributesData($category);
     unset($category);
     $categoryId = $model->getId();
     if (!empty($attributes)) {
         $attributeID = $this->db->fetchOne("SELECT id FROM s_categories_attributes WHERE categoryID = ?", [$categoryId]);
         if ($attributeID === false) {
             $attributes['categoryID'] = $categoryId;
             $this->db->insert('s_categories_attributes', $attributes);
         } else {
             $this->db->update('s_categories_attributes', $attributes, ['categoryID = ?' => $categoryId]);
         }
     }
     return $categoryId;
 }
Example #2
0
    /**
     * @param array $params
     * @return \Shopware\Models\Category\Category
     * @throws \Shopware\Components\Api\Exception\ValidationException
     * @throws \Exception
     */
    public function create(array $params)
    {
        $this->checkPrivilege('create');

        $params = $this->prepareCategoryData($params);

        $category = new \Shopware\Models\Category\Category();
        $category->fromArray($params);

        $violations = $this->getManager()->validate($category);
        if ($violations->count() > 0) {
            throw new ApiException\ValidationException($violations);
        }

        $this->getManager()->persist($category);
        $this->flush();

        return $category;
    }
Example #3
0
 /**
  * @param array $params
  * @return \Shopware\Models\Category\Category
  * @throws \Shopware\Components\Api\Exception\ValidationException
  * @throws \Exception
  */
 public function create(array $params)
 {
     $this->checkPrivilege('create');
     $params = $this->prepareCategoryData($params);
     $category = new \Shopware\Models\Category\Category();
     $category->fromArray($params);
     if (isset($params['id'])) {
         $metaData = $this->getManager()->getMetadataFactory()->getMetadataFor('Shopware\\Models\\Category\\Category');
         $metaData->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
         $category->setPrimaryIdentifier($params['id']);
     }
     $violations = $this->getManager()->validate($category);
     if ($violations->count() > 0) {
         throw new ApiException\ValidationException($violations);
     }
     $this->getManager()->persist($category);
     $this->flush();
     return $category;
 }
 /**
  * {@inheritDoc}
  */
 public function fromArray(array $array = array())
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'fromArray', array($array));
     return parent::fromArray($array);
 }