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

суббота, 26 ноября 2022 г.

Composer upgrade not working fix

When you want do single package upgrade like `composer update willdurand/hateoas --with-all-dependencies` and you have errors like this:

Your requirements could not be resolved to an installable set of packages:


  Problem 1

    - Root composer.json requires willdurand/hateoas ^3.8, found willdurand/hateoas[3.8.0] but these were not loaded, likely because it conflicts with another require.

  Problem 2

    - willdurand/hateoas-bundle 1.4.0 requires willdurand/hateoas ^2.10.0 -> found willdurand/hateoas[2.10.0, 2.11.0, 2.12.0] but it conflicts with your root composer.json require (^3.8).

    - sylius/resource-bundle v1.6.4 requires willdurand/hateoas-bundle ^1.2 -> satisfiable by willdurand/hateoas-bundle[1.4.0].

    - sylius/resource-bundle is locked to version v1.6.4 and an update of this package was not requested.


It means you need to go up by dependency tree and do `upgrade` manually. In my case I need to set "sylius/resource-bundle" to "^1.8", then run "composer update sylius/resource-bundle --with-all-dependencies" and finally run first command 

четверг, 20 октября 2022 г.

[NelmioApiDocBundle] how to register models without annotations or attributes?

config/packages/nelmio_api_doc.yaml:

```

nelmio_api_doc:

    models:

        use_jms: true

        names:

            - { alias: ExamType, type: StudentBundle\Form\ExamType }

            - { alias: User, type: App\Entity\User }

            - { alias: Pager, type: ApiBundle\Model\Pager }

``` 

среда, 2 марта 2022 г.

[php] code changes does not applied, how to fix?

If you make changes in php code and don't see that they work, like code remains the same, do this

 be sure your php.ini configuration `opcache.validate_timestamps=0` ,  not 1. It can be in dev mode when you try to optimize Symfony project workflow.

суббота, 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.

понедельник, 10 января 2022 г.

вторник, 9 ноября 2021 г.

[Symfony] Cannot read index 'email' from object of type App\Entity\User because it doesn't implement \ArrayAccess

 Its because PropertyAccessor thinks form is an array. To change it, set form data_class. For example in your form Type:

public function configureOptions(OptionsResolver $resolver)

    {

        $resolver

            ->setDefaults([

                'data_class' => User::class,

...


понедельник, 25 октября 2021 г.

[behat] chromedriver cannot call send on null fix

 When you have error like this:

Fatal error: Call to a member function waitForLoad() on null (Behat\Testwork\Call\Exception\FatalThrowableError

Add this into your context:

if ($this->getSession()->isStarted() === false) {

            $minkSession->start();

}

четверг, 26 августа 2021 г.

[Symfony] Cannot login at Test environment - fix (in Behat)

When you trying to login in Test env, and it does not show any error (you was returned on login form again), and you does not have anything in logs, except Guard authenticator does not support the request. {"firewall_key":"main","authenticator":"App\\Security\\AppAuthenticator"}, you can try this.

 I removed lines:

 session:

        storage_id: session.storage.mock_file

in config/packages/test/framework.yaml and it worked

вторник, 10 августа 2021 г.

How to create CollectionType with empty one element

 Add 'data' => [''] (not 'empty_data') to the Collection type form configuration.

воскресенье, 4 июля 2021 г.

[Symfony] how to modify data_class object setting logic

 When you need extra manipulations to object data population in form (for example, you need to resolve whether is entered user email is already registered and put existing user object or it needs to create a new user), you might want to set data_class with closure, but there isn't such option.

The right way is to use https://symfony.com/doc/4.4/form/data_mappers.html Data Mappers

среда, 12 мая 2021 г.

[Numberformatter] how to show plus sign after decimal number?

 If you need to show number for example "10.000+", where plus is in suffix, write:

$fmt = new NumberFormatter('es_ES', NumberFormatter::DECIMAL);
$fmt->setTextAttribute(NumberFormatter::POSITIVE_SUFFIX, '+');
$result = $fmt->format(10000);

Also if you use `NumberFormatter::POSITIVE_PREFIX` instead of `NumberFormatter::POSITIVE_SUFFIX`, you will get "+10.000".

понедельник, 19 апреля 2021 г.

[JMSSerializer] default serialization group are ignored fix

 If your default serialization group not working (does not ignore fields which should be shown only with specific group, not Default), check that \JMS\Serializer\Exclusion\GroupsExclusionStrategy is added into \JMS\Serializer\Context at  \JMS\Serializer\GraphNavigator::accept() method.

If you are using Sylius (or standalone bundles), it occurs when \FOS\RestBundle\Serializer\JMSSerializerAdapter does not add GroupsExclusionStrategy. You can fix it with adding serialization_groups: [Default] like this:

api_building_show:

    path: /exam/{id}

    methods: [GET]

    defaults:

        _controller: app.controller.building:showAction

        _sylius:

            serialization_groups: [Default]

четверг, 15 апреля 2021 г.

[FosRestBundle] [JmsSerializerBundle] how to show datetime in correct timezone

If your date fields looks like "2021-04-08T00:00:00+00:00", instead of your preferred timezone, set correct timezone in your cli/fpm php.ini:

[Date]

date.timezone = "Europe/Riga"

понедельник, 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

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

Symfony server - all routes fail with 404 fix

If you know that your project has working routes, but at requesting them with browser occasionally Symfony new server responds with 404 with any route, it can be your started Symfony local Web server (symfony command from CLI) from wrong directory, for example, in the parent folder after git cloning. In other words, check the current directory before running symfony serve command.

понедельник, 16 сентября 2019 г.

Symfony doesnt update minor version fix

If you cannot update your Symfony project to new minor version (for example, from 4.2 to 4.3), and your packages only checks packages only previous versions like this:

The requested package symfony/security-bundle 4.3.4 exists as symfony/security-bundle[4.2.x-dev, v4.2.0, v4.2.0-BETA1, v4.2.0-BETA2, v4.2.0-RC1, v4.2.1, v4.2.10, v4.2.11, v4.2.2, v4.2.3, v4.2.4, v4.2.5, v4.2.6, v4.2.7, v4.2.8, v4.2.9] but these are rejected by your constraint.
  Problem 2
    - The requested package symfony/translation ^4.3 exists as symfony/translation[4.2.x-dev, v4.2.0, v4.2.0-BETA1, v4.2.0-BETA2, v4.2.0-RC1, v4.2.1, v4.2.10, v4.2.11, v4.2.2, v4.2.3, v4.2.4, v4.2.5, v4.2.6, v4.2.7, v4.2.8, v4.2.9] but these are rejected by your constraint.
  Problem 3
    - The requested package symfony/twig-bundle ^4.3 exists as symfony/twig-bundle[4.2.x-dev, v4.2.0, v4.2.0-BETA1, v4.2.0-BETA2, v4.2.0-RC1, v4.2.1, v4.2.10, v4.2.11, v4.2.2, v4.2.3, v4.2.4, v4.2.5, v4.2.6, v4.2.7, v4.2.8, v4.2.9] but these are rejected by your constraint.

it means you should to change extra node in your composer.json file:
"extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "4.3.*"
        }
    }

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

Ways to optimize a Sylius project in dev mode

If you feel that your Sylius dev configuration is slow, try this:

1) use nginx (or docker with nginx) instead of php bin/console server:run
2) be sure XDebug is disabled
3) add these lines to your php.ini:

opcache.memory_consumption=256
opcache.max_accelerated_files=20000
realpath_cache_size=4096K

How to fix "504 gateway timeout" [php] [xdebug]

At the long PHP debug browser responds with "504 gateway timeout". If you want to increase this debugging period,

1) set in /etc/nginx/nginx.conf:
http {
    ...
    fastcgi_read_timeout 3600;
    ...
}
2) set this line in php.ini:
request_terminate_timeout = 3600s

3) restart nginx or restart containers.

It also possible to do in your Docker configuration.

четверг, 22 августа 2019 г.

Doctrine persists extra entities at update/merge fix

If you have batch create and update action simultaneously (persisting entity if it doesn't exists in DB or update it otherwise), made like in this documentation, and you noticed that it creates redundant entities, it means Doctrine think they are new. Enable "detach" cascade (transitional) operation in Doctrine like this:

/**
* @OneToMany(targetEntity="Customer", mappedBy="product", cascade={"detach"})
*/
protected $customers;

If you are not in dev environment, do not forget to run orm:clear-cache:metadata command.

суббота, 13 июля 2019 г.

my minimum steps to enable xdebug in ubuntu & phpstorm (locally)

1. install xdebug helper in Chrome-based browser
2. sudo apt-get install php7.2-xdebug
3. add these lines in <>:
xdebug.idekey=PHPSTORM
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
4. add in phpstorm settings Languages & Frameworks > PHP > Servers:

Name & Host: 127.0.0.1
Port 8000
4. set a breakpoint at reachable code line and start debugging