S
S
Svyatoslav2020-01-04 18:45:57
symfony
Svyatoslav, 2020-01-04 18:45:57

How to set the field name in a database table for an embedded field in Doctrine?

Happy New Year everyone! :)
There is a Client entity that contains various information about the client. My question is how to use Doctrine in the database to assign names to the fields "surname", "first name" and "patronymic" with the approach that I use.
The Client entity has a name field, it is represented by a value object named FullName. To communicate with this object, the doctrine uses an embedded connection, specified in the annotations.
FullName, in turn, contains three fields: last name, first name, and patronymic. In this case, each field is represented by its own value object, which is simply called Name. What for? Name can shorten the name, and for this it was allocated to a separate value object. Let's say "Ivan" abbreviates to "I.". Further, already in FullName, you can easily make a method that returns the abbreviated user name "Ivanov I.I."
To make it clear what is at stake, I have something like this code:

/** @ORM\Entity() */
class Client
{
    /** @ORM\Id() */
    private int $id;
    /** @ORM\Embedded(class="FullName") */
    private FullName $name;
    
    public function __construct(FullName $name)
    {
        $this->name = $name;
    }
}

/** @ORM\Embeddable() */
class FullName 
{
    /** @ORM\Embedded(class="Name") */
    private Name $surname;
    /** @ORM\Embedded(class="Name") */
    private Name $name;
    /** @ORM\Embedded(class="Name") */
    private Name $patronymic;
    
    public function __construct(Name $surname, Name $name, Name $patronymic)
    {
        $this->surname = $surname;
        $this->name = $name;
        $this->patronymic = $patronymic;
    }
    
    public function getFullName()
    {
        return implode(' ', [
            $this->surname->getFull(),
            $this->name->getFull(),
            $this->patronymic->getFull(),
        ]);
    }
    
    public function getShortName()
    {
        return implode(' ', [
            $this->surname->getFull(),
            $this->name->getShort(),
            $this->patronymic->getShort(),
        ]);
    }
}

/** @ORM\Embeddable() */
class Name
{
    /** @ORM\Column(type="string", length=100) */
    private string $value;

    public function __construct(string $value)
    {
        $this->value = $value;
    }

    public function getFull(): string
    {
        return $this->value;
    }

    public function getShort(): string
    {
        $value = $this->value;
        if ($value) {
            $value = mb_substr($value, 0, 1) . '.';
        }
        
        return $value;
    }
}

Since the last name, first name and patronymic are stored in their objects of the Name class in the field with the same name value, you have to rely on the automatically added columnPrefix, for example "surname_", so the fields "surname_value", "name_value", "patroymic_value" are obtained in the database table . I don't like this name purely aesthetically. I would like to get rid of the "_value" suffix in their naming. But how can I explain this to the Doctrine? Is this even possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Decadal, 2020-01-04
@ShameQNS

not. Name rewriting for Embedded is not supported
https://stackoverflow.com/questions/53497770/doctr...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question