Working with PHP 7, Symfony 3.1.3 and Doctrine 2.5.4.
I am trying to save updates to an entity. The entity is a child of another class (simplified versions of the classes):
class ProjectRequest
{
// No Doctrine or Symfony annotations on the parent class
protected $title;
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
}
class ProjectRequestNewProject extends ProjectRequest
{
/**
* @var string
* @ORM\Column(name="title", type="string")
* @Assert\NotBlank()
*/
protected $title;
/**
* @var string
* @ORM\Column(name="new_project_description", type="string")
* @Assert\NotBlank()
*/
protected $description;
/**
* @return string|null
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
* @return ProjectRequestNew
*/
public function setDescription(string $description): ProjectRequestNew
{
$this->description = $description;
return $this;
}
}
When I save a new record it works fine (simplified):
$entity = new ProjectRequestNewProject();
//... Symfony form management
$form->handleRequest();
$entityManager->persist($entity);
$entityManager->flush();
When I try to update the entity however, things get weird. My save logic is very normal. I have used the same logic on multiple other projects (and even other entities in the same project).
$projectRequest = $this->getDoctrine->getRepository('ProjectRequestNewProject')->find($id)
$form = $this->createForm(
ProjectRequestNewProjectType::class,
$projectRequest,
[
'entityManager' => $entityManager,
'action' => $this->generateUrl('project_request_new_project_edit', ['id' => $id])
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// save request
$entityManager->flush();
// redirect to show project request
return $this->redirect(
$this->generateUrl(
'project_request_show',
['id' => $id]
)
);
}
With this logic, if I update the $title
field on the entity then Doctrine updates the record as expected. But if I only change the $description
field Doctrine doesn't update the database.
Looking at the Symfony profile, I can see that the data is sent to the server and transformed normally. It really appears that Doctrine is ignoring changes to fields that were declared on the child entity when determining if the entity was changed.
I can't find anything about this behavior searching Google or StackOverflow (I may not be using the correct search terms) and I can't figure out why Doctrine is ignoring changes to fields on the child entity for determining if it needs to update the database.
Again, if I change both the title and the description, then changes to both fields will be saved to the database, but if I only change the description the change will not be saved to the database.
Is there something I've missed in the documentation, or is there an issue with entities that extend a base class?