Reputation: 426
I'm developing a web application with Symfony 3 from an existing project for over 8 years. Therefore I inherit an existing model.
Some entities have this field
/**
* @var integer
*
* @ORM\Column(name="FourID_Site", type="bigint", nullable=true)
*/
private $fouridSite;
And some other have
/**
* @var \FourListe
*
* @ORM\ManyToOne(targetEntity="FourListe")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="FourID_Site", referencedColumnName="FourID")
* })
*/
private $fouridSite;
I have a method which is called before all persist and flush doctrine method for update automatically some fields.
if (property_exists(get_class($entity), 'fouridSite')) {
$entity->setFouridSite($this->current_user->getFouridSiteutilisateur());
}
if (property_exists(get_class($entity), 'majlogin')) {
$entity->setMajlogin($this->current_user->getLogin());
}
if (property_exists(get_class($entity), 'majdate')) {
$entity->setMajdate(new \DateTime());
}
Issue : This code throw an error when $fouridSite
is an integer because I can't assign an entity ($this->current_user->getFouridSiteutilisateur()
return a FourListe
entity) when doctrine want an integer. And it is normal.
The native php method get type() can't be used in this case because it return NULL.
So, there is a way maybe, through annotation, to know if $fourisSite
will be an entity or an integer ?
Thanks for ideas or potentials any suggestions?
Upvotes: 1
Views: 1269
Reputation: 13167
Depending on where your update hook is located, you can use this to know the type of your column:
$em = $this->get('doctrine')->getManager();
$metadata = $em->getClassMetadata(get_class($entity));
$fieldMetadata = $metadata->fieldMappings['fouridSite'];
// Catch the type
$fieldType = $fieldMetadata['type'];
Then add some checks on this type to register the good value.
Hope you can access the doctrine service from your method.
Upvotes: 1