This is part of my Post entity:
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="post")
* @ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $user;
and from the User entity:
/**
* @ORM\OneToMany(targetEntity="Post", mappedBy="user")
*/
private $post;
my controller looks like this:
$post = new Post();
$form = $this->createForm('FreeCycler\UserBundle\Form\PostType', $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$post->setUser($this->getUser());
// also tried this:
//$post->setUser($this->getUser()->getId());
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush($post);
return $this->redirectToRoute('my_post_show', array('id' => $post->getId()));
}
The post gets saved to the orm but the user is empty or null. The $this->getUser(), however isn't null and it's shows the user object if I dump it.
$post->setUser($this->getUser());after the object creation$post = new Post();Post::setUser()method? and try to renamename="user"toname="user_id"and update your DB schema.Userentity as the security user in yourSecurityUserProvider?