Manuel Andres Diaz
Manuel Andres Diaz

Reputation: 21

Symfony 4 table relationships do not work out of naming strategy?

I have created two entities of existing database tables, these tables use the doctrine conventions for table relationships, I need to relate the tables to be able to work, the entities work by consulting data, but not between them.

Table name "Articulos"

class Articulos
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     */
    private $ID_Articulo;

     /**
     * @ORM\Column(name="ID_Clasificacion_Articulo", type="integer")
     * @ORM\ManyToOne(targetEntity="ClasificacionesArticulos")
     */
    private $ID_Clasificacion_Articulo;
.......

Table name "ClasificacionesArticulos"
class ClasificacionesArticulos
{
     /**
     * @ORM\Column(name="ID_Clasificacion_Articulo", type="integer")
     * @ORM\Id()
     * @ORM\OneToMany(targetEntity="Articulos", mappedBy="ID_Clasificacion_Articulo")
     */
    private $ID_Clasificacion_Articulo;

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

    /**
     * @ORM\Column(type="string", length=150)
     */
    private $Nombre;
.........

When I consult any of the entities, returns result without children of relationships. I suppose it's because of the names of the fields id does not use the name strategies, but I can not change them in the database, I have to adapt to it by requirements.

If someone has an idea, I thank you very much

Upvotes: 1

Views: 223

Answers (1)

Majesty
Majesty

Reputation: 1909

This can be accomplished by implementing custom Doctrine naming strategy. In Symfony entity, use camelCase to avoid any problems with naming. But, if you need specific table names follow the guide

You have to implement NamingStrategy class:

class CustomNamingStrategy implements NamingStrategy
{
}

register it as a service by adding following to the end of the the config/services.yaml :

app.naming_strategy.custom:
    class: App\Service\CustomNamingStrategy
    autowire: true

Then specify naming strategy by editing config/packages/doctrine.yaml as follows:

naming_strategy: app.naming_strategy.custom

I believe you are looking for propertyToColumnName method, as Doctrine says

If you have database naming standards, like all table names should be prefixed by the application prefix, all column names should be lower case, you can easily achieve such standards by implementing a naming strategy.

Upvotes: 1

Related Questions