DoctrineExtensions icon indicating copy to clipboard operation
DoctrineExtensions copied to clipboard

SF6 + PHP 8.1 + DoctrineExtension 2.5 = Slug not generated

Open ecwio opened this issue 3 years ago • 9 comments

I recently upgraded to Symfony 6

I run php 8.1 with doctrine/doctrine-bundle 2.5

here is my entity:

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @Gedmo\Slug(fields={"uid"})
 * @ORM\Column(length=120, unique=true)
 * @Groups({"post"})
 */
private $slug;

When I try saving to my database MariaDB 10.6.4 it says that Slug cannot be null. It worked before upgrading

my composer.json

"require": {
    "php": ">=8.0.2",
    "ext-ctype": "*",
    "ext-fileinfo": "*",
    "ext-gd": "*",
    "ext-iconv": "*",
    "ext-imagick": "^3.6",
    "ext-json": "*",
    "ext-openssl": "*",
    "beberlei/doctrineextensions": "^1.3",
    "doctrine/doctrine-bundle": "^2.5",
    "doctrine/orm": "^2.10",
    "gedmo/doctrine-extensions": "^3.3",
    "jms/serializer-bundle": "^4.0",
    "knplabs/knp-paginator-bundle": "^5.8",
    "symfony/asset": "6.0.*",
    "symfony/cache": "6.0.*",
    "symfony/config": "6.0.*",
    "symfony/console": "6.0.*",
    "symfony/dependency-injection": "6.0.*",
    "symfony/dotenv": "6.0.*",
    "symfony/flex": "^2",
    "symfony/framework-bundle": "6.0.*",
    "symfony/mailer": "6.0.*",
    "symfony/mime": "6.0.*",
    "symfony/runtime": "6.0.*",
    "symfony/twig-bundle": "6.0.*",
    "symfony/validator": "6.0.*",
    "symfony/web-profiler-bundle": "6.0.*",
    "symfony/yaml": "6.0.*"
},

doctrine.yaml

doctrine:
 dbal:
    url: '%env(DATABASE_URL)%'
    server_version: 'mariadb-10.6.4'
 orm:
    auto_generate_proxy_classes: true
    naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
    auto_mapping: true
    mappings:
        App:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Entity'
            prefix: 'App\Entity'
            alias: App
    dql:
        numeric_functions:
            rand: DoctrineExtensions\Query\Mysql\Rand

ecwio avatar Dec 03 '21 07:12 ecwio

Thank you for reporting the issue @signmeuptwice. Could you please check if the same version of this package is working fine in your project previous to the upgrade?

phansys avatar Dec 03 '21 11:12 phansys

@phansys would do but I removed the extension and made my own to reduce dependencies

ecwio avatar Dec 03 '21 12:12 ecwio

Hi, I don't know if it could help, but I may have the same issue on a brand new project (PHP 8.0, SF 6.0.4, Mysql 8, doctrine-bundle 2.5.5). That's the first time I'm using Gedmo/Slug so, cannot confirm it work before, but when I'm trying to save a new entity, have the exact same issue.

My entity:

<?php
declare(strict_types=1);

namespace App\Entity;

use App\Repository\ProductRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity(repositoryClass=ProductRepository::class)
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private string $name;


    /**
     * @Gedmo\Slug(fields={"name"})
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private string $slug;


    public function getSlug(): string
    {
        return $this->slug;
    }
}

my composer.json

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "require": {
        "php": ">=8.0.2",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "ext-pcre": "*",
        "ext-session": "*",
        "ext-simplexml": "*",
        "ext-tokenizer": "*",
        "ext-zlib": "*",
        "composer/package-versions-deprecated": "1.11.99.4",
        "doctrine/annotations": "^1.13",
        "doctrine/doctrine-bundle": "^2.5",
        "doctrine/doctrine-migrations-bundle": "^3.2",
        "doctrine/orm": "^2.10",
        "gedmo/doctrine-extensions": "^3.4",
        "phpdocumentor/reflection-docblock": "^5.3",
        "phpstan/phpdoc-parser": "^1.2",
        "sensio/framework-extra-bundle": "^6.1",
        "symfony/apache-pack": "^1.0",
        "symfony/asset": "6.0.*",
        "symfony/console": "6.0.*",
        "symfony/dotenv": "6.0.*",
        "symfony/expression-language": "6.0.*",
        "symfony/flex": "^2",
        "symfony/form": "6.0.*",
        "symfony/framework-bundle": "6.0.*",
        "symfony/http-client": "6.0.*",
        "symfony/intl": "6.0.*",
        "symfony/lock": "6.0.*",
        "symfony/mailer": "6.0.*",
        "symfony/messenger": "6.0.*",
        "symfony/mime": "6.0.*",
        "symfony/monolog-bundle": "^3.1",
        "symfony/notifier": "6.0.*",
        "symfony/process": "6.0.*",
        "symfony/property-access": "6.0.*",
        "symfony/property-info": "6.0.*",
        "symfony/proxy-manager-bridge": "6.0.*",
        "symfony/runtime": "6.0.*",
        "symfony/security-bundle": "6.0.*",
        "symfony/serializer": "6.0.*",
        "symfony/string": "6.0.*",
        "symfony/translation": "6.0.*",
        "symfony/twig-bundle": "6.0.*",
        "symfony/validator": "6.0.*",
        "symfony/web-link": "6.0.*",
        "symfony/yaml": "6.0.*",
        "twig/extra-bundle": "^2.12|^3.0",
        "twig/twig": "^2.12|^3.0"

    },

doctrine.yaml:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        schema_filter: ~^(?!import_)~
        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '13'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
Exception trace:
  at D:\wamp\www\configurateurpc\vendor\doctrine\dbal\src\Driver\PDO\Statement.php:101
 PDOStatement->execute() at D:\wamp\www\configurateurpc\vendor\doctrine\dbal\src\Driver\PDO\Statement.php:101
 Doctrine\DBAL\Driver\PDO\Statement->execute() at D:\wamp\www\configurateurpc\vendor\doctrine\dbal\src\Statement.php:180 Doctrine\DBAL\Statement->execute() at D:\wamp\www\configurateurpc\vendor\doctrine\dbal\src\Statement.php:221
 Doctrine\DBAL\Statement->executeStatement() at D:\wamp\www\configurateurpc\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php:276
 Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts() at D:\wamp\www\configurateurpc\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php:1128
 Doctrine\ORM\UnitOfWork->executeInserts() at D:\wamp\www\configurateurpc\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php:425
 Doctrine\ORM\UnitOfWork->commit() at D:\wamp\www\configurateurpc\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php:392
 Doctrine\ORM\EntityManager->flush()

And the code throwing this error:

$product = (new Product())
    ->setName($item['product_name']);
$this->em->persist($product);
$this->em->flush();

jmbianca avatar Jan 31 '22 16:01 jmbianca

It looks like we're having a similar type of issue, this entity also uses the Tree and Loggable extensions.

The app I'm seeing the issue on is running Symfony 4.4 with PHP 7.4 and uses MariaDB 10.1. DBAL (2.13.7), ORM (2.11.0), and this package (3.5.0) all essentially up-to-date. I checked one of our Symfony 5.4 apps (running PHP 8.0 with DBAL 3.3 and up-to-date ORM and extensions package) and it doesn't seem like that one has any issues with the slugs. Below is the stripped-down entity we're seeing this pop up on:

<?php

namespace App\Asset\Entity;

use App\Doctrine\ORM\Entity\IdPrimaryKey;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @Gedmo\Loggable()
 * @Gedmo\Tree(type="nested")
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="App\Asset\Repository\AssetCategoryRepository")
 */
class AssetCategory
{
    /**
     * @var int|null
     *
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @Gedmo\Versioned()
     * @ORM\Column(type="string")
     */
    private $title = '';

    /**
     * @var string|null
     *
     * @Gedmo\Versioned()
     * @Gedmo\Slug(fields={"title"}, unique=true, unique_base="parent")
     * @ORM\Column(type="string")
     */
    private $slug;

    /**
     * @var int|null
     *
     * @Gedmo\Versioned()
     * @Gedmo\TreeLeft
     * @ORM\Column(type="integer")
     */
    private $lft;

    /**
     * @var int|null
     *
     * @Gedmo\Versioned()
     * @Gedmo\TreeRight
     * @ORM\Column(type="integer")
     */
    private $rgt;

    /**
     * @var int|null
     *
     * @Gedmo\TreeLevel
     * @ORM\Column(type="integer")
     */
    private $lvl;

    /**
     * @var AssetCategory
     *
     * @Gedmo\TreeRoot
     * @ORM\ManyToOne(targetEntity="App\Asset\Entity\AssetCategory")
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
     */
    private $root;

    /**
     * @var AssetCategory|null
     *
     * @Gedmo\Versioned()
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="App\Asset\Entity\AssetCategory", inversedBy="children", cascade={"persist"})
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
     */
    private $parent;

    /**
     * @var Collection<AssetCategory>
     *
     * @ORM\OneToMany(targetEntity="App\Asset\Entity\AssetCategory", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    private $children;

    public function __construct()
    {
        $this->children = new ArrayCollection();
    }

    // Getters and setters omitted for simplicity
}

mbabker avatar Jan 31 '22 18:01 mbabker

I'll try later today if I can reproduce the issue, @mbabker can you please try with 3.4? maybe is something related with making Slug annotations compatible with attributes.

franmomu avatar Feb 02 '22 11:02 franmomu

I'm not able to reproduce the issue.

@jmbianca did you register the sluggable listener service as in https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/symfony4.md#doctrine-extension-listener-services? that's the only thing I can think of:

gedmo.listener.sluggable:
        class: Gedmo\Sluggable\SluggableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ "@annotation_reader" ] ]

franmomu avatar Feb 02 '22 23:02 franmomu

In my case, the problem ended up being legacy form code and the form value defaulting to an empty string, which conflicts with the sluggable listener only acting when $slug === null.

mbabker avatar Feb 24 '22 16:02 mbabker

I have the same issue with PHP 8.1 and Symfony 6, my slug is null in database

image

"require": { "php": ">=8.0.2", "ext-ctype": "", "ext-iconv": "", "api-platform/core": "^2.6", "doctrine/annotations": "^1.0", "doctrine/doctrine-bundle": "^2.6", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.12", "easycorp/easyadmin-bundle": "^4.1", "gesdinet/jwt-refresh-token-bundle": "^1.1", "intervention/image": "^2.7", "knplabs/doctrine-behaviors": "^2.6", "knplabs/knp-snappy": "^1.4", "league/flysystem": "^3.0", "lexik/jwt-authentication-bundle": "^2.14", "nelmio/cors-bundle": "^2.2", "php-amqplib/rabbitmq-bundle": "^2.11", "phpdocumentor/reflection-docblock": "^5.3", "phpstan/phpdoc-parser": "^1.2", "sensio/framework-extra-bundle": "^6.2", "symfony/amqp-messenger": "6.1.", "symfony/asset": "6.1.", "symfony/console": "6.1.", "symfony/debug-bundle": "6.1.", "symfony/doctrine-messenger": "6.1.", "symfony/dotenv": "6.1.", "symfony/expression-language": "6.1.", "symfony/flex": "^2", "symfony/framework-bundle": "6.1.", "symfony/lock": "6.1.", "symfony/mailer": "6.1.", "symfony/messenger": "6.1.", "symfony/monolog-bundle": "^3.7", "symfony/property-access": "6.1.", "symfony/property-info": "6.1.", "symfony/proxy-manager-bridge": "6.1.", "symfony/rate-limiter": "6.1.", "symfony/runtime": "6.1.", "symfony/security-bundle": "6.1.", "symfony/serializer": "6.1.", "symfony/twig-bundle": "6.1.", "symfony/validator": "6.1.", "symfony/yaml": "6.1.*", "vich/uploader-bundle": "^1.19" } `

romainpltr avatar Jul 26 '22 10:07 romainpltr

I may be wrong, but you don't have the stof/doctrine-extensions-bundle package installed

Also add to yaml config:

stof_doctrine_extensions:
    translation_fallback: true
    orm:
      default:
         sluggable: true

RitchieWhitemore avatar Sep 02 '22 08:09 RitchieWhitemore

stof_doctrine_extensions configuration is no longer available in SF6

newQuery avatar Oct 05 '22 08:10 newQuery

stof_doctrine_extensions configuration is no longer available in SF6

Where do you put the content of the file "stof_doctrine_extensions.yaml"?

DimitriPSN avatar Dec 13 '22 13:12 DimitriPSN

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Aug 01 '23 18:08 github-actions[bot]