jimmeak/doctrine-bundle

Making life easier while developing Symfony project using Doctrine

3.0.1 2024-04-08 20:56 UTC

This package is auto-updated.

Last update: 2024-04-08 20:57:42 UTC


README

This bundle has been created as helping hand across many Symfony projects, when Doctrine is present and more complex database Entities are required. You can use many predefined PHP traits and also one mapped superclass containing createdAt or updatedAt with Gedmo Doctrine Extension.

Installation

You need to use PHP 8.2 to run this bundle, since it contains new readonly classes syntax.

If you are using Symfony Flex, all you need to do is to run composer require command

composer require jimmeak/doctrine-bundle

If you are not using flex within the Symfony application, make sure you have the bundle registered

// config/bundles.php

<?php

return [
    ...
    Jimmeak\DoctrineBundle\JimmeakDoctrineBundle::class => ['all' => true],
]

Usage

When creating database Entity object, you can use traits like FirstName or LastName

// src/Entity/SomeEntity.php
<?php

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Person {
    # Creating autogenerated UUID v7
    use \Jimmeak\DoctrineBundle\Trait\UuidV7    
    
    # Giving Person FirstName attribute, getter and setter.
    use \Jimmeak\DoctrineBundle\Trait\FirstName; 
    
    # Giving Person LastName attribute, getter and setter.
    use \Jimmeak\DoctrineBundle\Trait\LastName;  
}

#[ORM\Entity]
class User {
    # Creating autogenerated UUID v7
    use \Jimmeak\DoctrineBundle\Trait\UuidV7
    
    # Giving Entity User First name, Last name, getters and setters
    # Giving Entity User one additional getter for Full name
    # with optional reverse possibility 
    use \Jimmeak\DoctrineBundle\Trait\FullName;
}

This Doctrine Bundle also contains Table Name Listener. It can take a name resolver interface and created database table names according to rules of this class. Default NameResolver created table names from Namespace hierarchy. Everything can be set in config file

jimmeak_doctrine:
    
    # Values below are default values set in DependencyInjection/Configuration.php
    table_name_listener:
        
        # Listener will create new structure of table names based on class Namespaces
        allow: false
        
        # If "allow" is set as true, in this node must be specified the entity namespace
        # Default value is set to App, but it will vary from project to project according to ORM Mapping
        # set in config/packages/doctrine.yaml
        entity_namespace: App
        
        # Service has to be registered in container and be
        name_resolver: null  

    

Creating your own Name Resolver

The only important thing you need to have in mind is the implementation of correct interface. Create your own service

// src/Resolver/MyOwnDoctrineNameResolver.php

<?php

namespace Resolver;

use Jimmeak\DoctrineBundle\Resolver\NameResolverInterface;

class MyOwnDoctrineNameResolver implements NameResolverInterface
{
    public function name(string $namespace, string $prefix): string
    {
        // return your table name based on namespace and prefix
    }

    public function manyToManyName(string $sourceEntityNamespace, string $targetEntityNamespace, string $prefix): string
    {
        // return your table name for joining table (relation many to many)
        // having source and target entity namespaces and prefix.
    }
}

Then do not forget to turn the table name listener on and give him your service

jimmeak_doctrine:
    table_name_listener:
        allow: true
        name_resolver: 'Resolver\MyOwnDoctrineNameResolver'