Показаны сообщения с ярлыком sylius standalone. Показать все сообщения
Показаны сообщения с ярлыком sylius standalone. Показать все сообщения

суббота, 12 февраля 2022 г.

[SyliusResourceBundle] how to pass model into controller?

 Sometimes you don't need to use model instead of entity, and then save some data into entity and persist it. It means you cannot use it in Sylius ResourceControllers, because your model must implement ResourceInterface, create dedicated ResourceController, Repository, Manager, and model cannot do it.

If you want it, this means you doing it wrong, because it violated CRUD principles. The best solution is to use Symfony Data Transformers https://symfony.com/doc/current/form/data_transformers.html , add it to your form, implement transform() method, where it will create your model/DTO object from entity, and reverseTransform(), where DTO will be converted into Entity object.

четверг, 27 января 2022 г.

[hateoas] [jms] how to set custom serialization group

 The issue is using Hateoas + JmsSerializerBundle with custom serialization groups don't respond with embedded with needed fields, for me they respond only with {}. The workaround is make \JMS\Serializer\Exclusion\DisjunctExclusionStrategy::shouldSkipProperty() to return with false.

You can make it with custom class, which implements ExclusionStrategyInterface before all standard Strategies in Context like this:

$view->getContext()->addExclusionStrategy(new CustomDisjunctStrategy([]));

That's it, after it it should work (but without links). If you use Sylius bundles, you can use it in overriden ViewHandler like this:

<?php

namespace App\Serializer;


use FOS\RestBundle\View\View;

use FOS\RestBundle\View\ViewHandler as RestViewHandler;

use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;

use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;

use Symfony\Component\HttpFoundation\Response;


final class ViewHandler implements ViewHandlerInterface

{

    /** @var RestViewHandler */

    private $restViewHandler;


    public function __construct(RestViewHandler $restViewHandler)

    {

        $this->restViewHandler = $restViewHandler;

    }


    /**

     * {@inheritdoc}

     */

    public function handle(RequestConfiguration $requestConfiguration, View $view): Response

    {

        if (!$requestConfiguration->isHtmlRequest()) {

            if ($requestConfiguration->getSerializationGroups()) {

                $groups = $requestConfiguration->getSerializationGroups();

                $view->getContext()->addExclusionStrategy(new CustomDisjunctStrategy([]));

            } else {

                $groups = [];

            }

            $this->restViewHandler->setExclusionStrategyGroups($groups);


            if ($version = $requestConfiguration->getSerializationVersion()) {

                $this->restViewHandler->setExclusionStrategyVersion($version);

            }


            $view->getContext()->enableMaxDepth();

        }


        return $this->restViewHandler->handle($view);

    }

}



# services.yaml:

sylius.resource_controller.view_handler:

        class: App\Serializer\ViewHandler

        arguments:

            - "@fos_rest.view_handler"

четверг, 8 июля 2021 г.

[Sylius] GridHelper::renderGrid() must be an instance of Sylius\Component\Grid\View\GridView, instance of Pagerfanta\Pagerfanta - how to fix

 It means you forgot to put `grid: gridname` into your route configuration. For example:

admin_user_index:

    path: /users

    defaults:

        _controller: app.controller.user::indexAction

        _sylius:

            template: "@AdminBundle/grid/index.html.twig"

            grid: admin_user

понедельник, 23 ноября 2020 г.

[Sylius] Argument 1 passed to Sylius\Component\Grid\Definition\Filter::setTemplate() must be of the type string, null given fix

 put `grid: <gridname>` into your route like this:

tests:
path: /tests
defaults:
_controller: app.controller.test:indexAction
_sylius:
template: tests/index.html.twig
grid: app_test

воскресенье, 22 ноября 2020 г.

[Monofony] how to create/get admin user?

 run `php bin/console app:install:database`, after then you will have user "admin@example.com" with password "admin". You can use it in 127.0.0.1:8000/admin/login

воскресенье, 1 декабря 2019 г.

sylius.resource_controller.flash_helper has a dependency on a non-existent parameter "locale" - fix

if you have the such error:

The service "sylius.resource_controller.flash_helper" has a dependency on a non-existent parameter "locale". Did you mean one of these: "kernel.default_locale", "stof_doctrine_extensions.default_locale"?

It means you do not have an locale service parameter. To fix this, add this to the beginning of your project config/services.yaml file:

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: en