Answer the question
In order to leave comments, you need to log in
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;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question