symfony-cmf / routing-bundle

Symfony bundle to provide the CMF chain router to handle multiple routers, and the dynamic router to load routes from a database or other sources.
159 stars 78 forks source link

symfony-cmf implementation using doctrine orm #483

Closed chris-k-k closed 2 years ago

chris-k-k commented 2 years ago

Environment

PC - Windows 10 Xampp Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.6 10.4.24-MariaDB database

Symfony packages

symfony/apache-pack                v1.0.1  v1.0.1  A pack for Apache support in Symfony
symfony/asset                      v6.1.0  v6.1.0  Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files
symfony/browser-kit                v6.1.0  v6.1.0  Simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically
symfony/cache                      v6.1.1  v6.1.1  Provides an extended PSR-6, PSR-16 (and tags) implementation
symfony/cache-contracts            v3.1.0  v3.1.0  Generic abstractions related to caching
symfony/config                     v6.1.0  v6.1.0  Helps you find, load, combine, autofill and validate configuration values of any kind
symfony/console                    v6.1.1  v6.1.1  Eases the creation of beautiful and testable command line interfaces
symfony/css-selector               v6.1.0  v6.1.0  Converts CSS selectors to XPath expressions
symfony/debug-bundle               v6.1.0  v6.1.0  Provides a tight integration of the Symfony VarDumper component and the ServerLogCommand from MonologBridge into the Symfony full-stack framework
symfony/dependency-injection       v6.1.0  v6.1.0  Allows you to standardize and centralize the way objects are constructed in your application
symfony/deprecation-contracts      v3.1.0  v3.1.0  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            v6.1.0  v6.1.0  Provides integration for Doctrine with various Symfony components
symfony/doctrine-messenger         v6.1.1  v6.1.1  Symfony Doctrine Messenger Bridge
symfony/dom-crawler                v6.1.0  v6.1.0  Eases DOM navigation for HTML and XML documents
symfony/dotenv                     v6.1.0  v6.1.0  Registers environment variables from a .env file
symfony/error-handler              v6.1.0  v6.1.0  Provides tools to manage errors and ease debugging PHP code
symfony/event-dispatcher           v6.1.0  v6.1.0  Provides tools that allow your application components to communicate with each other by dispatching events and listening to them
symfony/event-dispatcher-contracts v3.1.0  v3.1.0  Generic abstractions related to dispatching event
symfony/expression-language        v6.1.0  v6.1.0  Provides an engine that can compile and evaluate expressions
symfony/filesystem                 v6.1.0  v6.1.0  Provides basic utilities for the filesystem
symfony/finder                     v6.1.0  v6.1.0  Finds files and directories via an intuitive fluent interface
symfony/flex                       v2.2.2  v2.2.2  Composer plugin for Symfony
symfony/form                       v6.1.1  v6.1.1  Allows to easily create, process and reuse HTML forms
symfony/framework-bundle           v6.1.1  v6.1.1  Provides a tight integration between Symfony components and the Symfony full-stack framework
symfony/http-client                v6.1.1  v6.1.1  Provides powerful methods to fetch HTTP resources synchronously or asynchronously
symfony/http-client-contracts      v3.1.0  v3.1.0  Generic abstractions related to HTTP clients
symfony/http-foundation            v6.1.1  v6.1.1  Defines an object-oriented layer for the HTTP specification
symfony/http-kernel                v6.1.1  v6.1.1  Provides a structured process for converting a Request into a Response
symfony/intl                       v6.1.0  v6.1.0  Provides a PHP replacement layer for the C intl extension that includes additional data from the ICU library
symfony/mailer                     v6.1.1  v6.1.1  Helps sending emails
symfony/maker-bundle               v1.43.0 v1.43.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/messenger                  v6.1.0  v6.1.0  Helps applications send and receive messages to/from other applications or via message queues
symfony/mime                       v6.1.1  v6.1.1  Allows manipulating MIME messages
symfony/monolog-bridge             v6.1.1  v6.1.1  Provides integration for Monolog with various Symfony components
symfony/monolog-bundle             v3.8.0  v3.8.0  Symfony MonologBundle
symfony/notifier                   v6.1.0  v6.1.0  Sends notifications via one or more channels (email, SMS, ...)
symfony/options-resolver           v6.1.0  v6.1.0  Provides an improved replacement for the array_replace PHP function
symfony/password-hasher            v6.1.0  v6.1.0  Provides password hashing utilities
symfony/phpunit-bridge             v6.1.0  v6.1.0  Provides utilities for PHPUnit, especially user deprecation notices management
symfony/polyfill-intl-grapheme     v1.26.0 v1.26.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.26.0 v1.26.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.26.0 v1.26.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.26.0 v1.26.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.26.0 v1.26.0 Symfony polyfill for the Mbstring extension
symfony/process                    v6.1.0  v6.1.0  Executes commands in sub-processes
symfony/property-access            v6.1.0  v6.1.0  Provides functions to read and write from/to an object or array using a simple string notation
symfony/property-info              v6.1.1  v6.1.1  Extracts information about PHP class' properties using metadata of popular sources
symfony/proxy-manager-bridge       v6.1.0  v6.1.0  Provides integration for ProxyManager with various Symfony components
symfony/routing                    v6.1.1  v6.1.1  Maps an HTTP request to a set of configuration variables
symfony/runtime                    v6.1.1  v6.1.1  Enables decoupling PHP applications from global state
symfony/security-bundle            v6.1.0  v6.1.0  Provides a tight integration of the Security component into the Symfony full-stack framework
symfony/security-core              v6.1.0  v6.1.0  Symfony Security Component - Core Library
symfony/security-csrf              v6.1.0  v6.1.0  Symfony Security Component - CSRF Library
symfony/security-http              v6.1.1  v6.1.1  Symfony Security Component - HTTP Integration
symfony/serializer                 v6.1.1  v6.1.1  Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
symfony/service-contracts          v3.1.0  v3.1.0  Generic abstractions related to writing services
symfony/stopwatch                  v6.1.0  v6.1.0  Provides a way to profile code
symfony/string                     v6.1.0  v6.1.0  Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way
symfony/translation                v6.1.0  v6.1.0  Provides tools to internationalize your application
symfony/translation-contracts      v3.1.0  v3.1.0  Generic abstractions related to translation
symfony/twig-bridge                v6.1.0  v6.1.0  Provides integration for Twig with various Symfony components
symfony/twig-bundle                v6.1.1  v6.1.1  Provides a tight integration of Twig into the Symfony full-stack framework
symfony/validator                  v6.1.1  v6.1.1  Provides tools to validate values
symfony/var-dumper                 v6.1.0  v6.1.0  Provides mechanisms for walking through any arbitrary PHP variable
symfony/var-exporter               v6.1.1  v6.1.1  Allows exporting any serializable PHP data structure to plain PHP code
symfony/web-link                   v6.1.0  v6.1.0  Manages links between resources
symfony/web-profiler-bundle        v6.1.1  v6.1.1  Provides a development tool that gives detailed information about the execution of any request
symfony/yaml                       v6.1.0  v6.1.0  Loads and dumps YAML files

Symfony CMF packages

symfony-cmf/routing        3.0.0 3.0.0 Extends the Symfony routing component for dynamic routes and chaining several routers
symfony-cmf/routing-bundle 3.0.0 3.0.0 Symfony RoutingBundle

Subject

I'm trying to implement the ORM variation of the Routing Bundle to make use of the Dynamic Router. Unfortunately, I ran into problems when trying to persist new entries into the 'orm_routes' table. It looks like one of the table columns is using a reserved keyword (stemming from the variable 'condition' => Symfony\Component\Routing). What would be the best solution/workaround to fix this? Any pointers are highly appreciated!

Steps to reproduce

Using a (modified) snippet from the documentation: https://symfony.com/bundles/CMFRoutingBundle/current/routing-bundle/dynamic.html#the-orm-route-entity

services:
    Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\ContentRepository: '@cmf_routing.content_repository'
namespace App\Controller;

use App\Entity\TestEntity;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\ContentRepository;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route as CmfRoute;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TestController extends AbstractController
{
    #[Route('/test', name: 'app_test')]
    public function index(EntityManagerInterface $manager, ContentRepository $contentRepository): Response
    {
        $post = new TestEntity();
        $post->setName('My Content');
        $manager->persist($post);
        $manager->flush(); // flush to be able to use the generated id

        //$contentRepository = $this->container->get('cmf_routing.content_repository');

        $route = new CmfRoute();
        $route->setName('my-content');
        $route->setStaticPrefix('/my-content');
        $route->setDefault(RouteObjectInterface::CONTENT_ID, $contentRepository->getContentId($post));
        $route->setContent($post);

        $post->addRoute($route); // Create the backlink from content to route

        $manager->persist($post);
        $manager->flush();

        return $this->render('test/index.html.twig', [
            'controller_name' => 'TestController',
        ]);
    }
}

Expected results

The table 'orm_routes' ought to be populated with values referencing the new test entity via the ManyToMany-relation.

Actual results

Doctrine\DBAL\Exception\SyntaxErrorException:
An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'condition, variable_pattern, staticPrefix, name, position) VALUES ('', 'a:0:{...' at line 1

  at C:\xampp\htdocs\test-project\vendor\doctrine\dbal\src\Driver\API\MySQL\ExceptionConverter.php:88
  at Doctrine\DBAL\Driver\API\MySQL\ExceptionConverter->convert(object(Exception), object(Query))
     (C:\xampp\htdocs\test-project\vendor\doctrine\dbal\src\Connection.php:1814)
  at Doctrine\DBAL\Connection->handleDriverException(object(Exception), object(Query))
     (C:\xampp\htdocs\test-project\vendor\doctrine\dbal\src\Connection.php:1749)
  at Doctrine\DBAL\Connection->convertExceptionDuringQuery(object(Exception), 'INSERT INTO orm_routes (host, schemes, methods, defaults, requirements, options, condition, variable_pattern, staticPrefix, name, position) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', array('', array(), array(), array('_content_id' => 'App\\Entity\\TestEntity:13'), array(), array(), '', '', '/my-content', 'my-content', 0), array('string', 'array', 'array', 'array', 'array', 'array', 'string', 'string', 'string', 'string', 'integer'))
     (C:\xampp\htdocs\test-project\vendor\doctrine\dbal\src\Statement.php:184)
  at Doctrine\DBAL\Statement->execute(null)
     (C:\xampp\htdocs\test-project\vendor\doctrine\dbal\src\Statement.php:221)
  at Doctrine\DBAL\Statement->executeStatement()
     (C:\xampp\htdocs\test-project\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php:278)
  at Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts()
     (C:\xampp\htdocs\test-project\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php:1129)
  at Doctrine\ORM\UnitOfWork->executeInserts(object(ClassMetadata))
     (C:\xampp\htdocs\test-project\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php:426)
  at Doctrine\ORM\UnitOfWork->commit(null)
     (C:\xampp\htdocs\test-project\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php:398)
  at Doctrine\ORM\EntityManager->flush(null)
     (C:\xampp\htdocs\test-project\var\cache\dev\ContainerKe526fj\EntityManager_9a5be93.php:136)
  at ContainerKe526fj\EntityManager_9a5be93->flush()
     (C:\xampp\htdocs\test-project\src\Controller\TestController.php:39)
  at App\Controller\TestController->index(object(EntityManager_9a5be93), object(ContentRepository))
     (C:\xampp\htdocs\test-project\vendor\symfony\http-kernel\HttpKernel.php:152)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (C:\xampp\htdocs\test-project\vendor\symfony\http-kernel\HttpKernel.php:74)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (C:\xampp\htdocs\test-project\vendor\symfony\http-kernel\Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (C:\xampp\htdocs\test-project\vendor\symfony\runtime\Runner\Symfony\HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (C:\xampp\htdocs\test-project\vendor\autoload_runtime.php:29)
  at require_once('C:\\xampp\\htdocs\\test-project\\vendor\\autoload_runtime.php')
     (C:\xampp\htdocs\test-project\public\index.php:5)  
dbu commented 2 years ago

this has been fixed with #481, but we did not do a release with the fix. i now did 3.0.1, if you update the bundle, the error should be gone and the table created. does it work for you? (if you use doctrine migrations, you will need to recreate the migration after the upgrade)

chris-k-k commented 2 years ago

sorry for not spotting #481 beforehand. and yes, after updating to 3.0.1 it now works as intended. the added backticks do the trick. tyvm for the quick help!