prepareAssociatedData example


        $this->checkPrivilege('create');

        $product = new ProductModel();

        $translations = [];
        if (!empty($params['translations'])) {
            $translations = $params['translations'];
            unset($params['translations']);
        }

        $params = $this->prepareAssociatedData($params$product);

        $product->fromArray($params);

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

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

        
return $variant;
    }

    /** * Internal helper function to save the product data. * * @param array $data * @param Product $article */
    protected function saveArticle($data$article)
    {
        $data = $this->prepareAssociatedData($data$article);

        $article->fromArray($data);

        $this->get('models')->persist($article);
        $this->get('models')->flush();
        if (empty($data['id']) && !empty($data['autoNumber'])) {
            $this->increaseAutoNumber($data['autoNumber']$article->getMainDetail()->getNumber());
        }

        $savedProduct = $this->getArticle($article->getId());
        $this->View()->assign([
            
        if (isset($params['defaultBillingAddress'])) {
            $params['billing'] = $params['defaultBillingAddress'];
            unset($params['defaultBillingAddress']);
        }

        if (isset($params['defaultShippingAddress'])) {
            $params['shipping'] = $params['defaultShippingAddress'];
            unset($params['defaultShippingAddress']);
        }

        $params = $this->prepareCustomerData($params$customer);
        $params = $this->prepareAssociatedData($params$customer);
        $customer->fromArray($params);

        $billing = $this->createAddress($params['billing']) ?? new AddressModel();
        $shipping = $this->createAddress($params['shipping'] ?? null);

        $registerService = $this->getContainer()->get(RegisterServiceInterface::class);
        $context = $this->getContainer()->get(ContextServiceInterface::class)->getShopContext()->getShop();

        $context->addAttribute('sendOptinMail', new Attribute([
            'sendOptinMail' => ($params['sendOptinMail'] ?? false) === true,
        ]));

        


    /** * @return UserModel */
    public function create(array $params)
    {
        $this->checkPrivilege('create', 'usermanager');

        // Create models         $user = new UserModel();
        $params = $this->prepareAssociatedData($params$user);
        $user->fromArray($params);

        /** @var UserValidator $userValidator */
        $userValidator = $this->getContainer()->get('shopware.auth.validator.user_validator');
        $userValidator->validate($user);

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

        return $user;
    }

    
Home | Imprint | This part of the site doesn't use cookies.