Let's say I have a database and a table called User, which is filled up all with "name", "age". Name is like "Peter David Smith" or "Adam Pitt". Inside the User entity, I want to create a new column.
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="integer")
*/
private $age
// THIS IS THE NEW COLUMN
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $monogram;
How do I tell Doctrine that the new column called "monogram" should be filled up with the first letters of the $name's words? So in the case of "Peter Daniel Smith" it should be "PDS", at "Adam Pitt" -> "AD". The database has a lot or records with "name" and "age" so the database is not empty.
I've tried this in the constructor, doesn't work:
public function __construct()
{
// ...
$words = explode(" ", $this->name);
foreach ($words as $w){
$this->monogram .= $w[0];
}
}
After this I make the migration, run it, but the monogram column is null everywhere. Is there any way to fill the $monogram based on the existing database data on this level?