I use Symfony with Doctrine and I try to insert data into DB. In my table messages there's one column user_id which is set as foreign key to users.id.
In my Message entitity there's this setter for user_id
/**
* @ORM\Entity
* @ORM\Table(name="Messages")
*/
class Message {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="user_id", type="integer")
*/
protected $user_id;
/**
* @ORM\Column(name="text", type="text")
*/
protected $text;
/**
* @ORM\ManyToOne(targetEntity="User")
* */
protected $user;
public function setText($text) {
$this->text = $text;
}
public function getText() {
return $this->text;
}
public function setUserId($user_id) {
$this->user_id = $user_id;
}
public function getUser_id() {
return $this->user_id;
}
public function getUser() {
return $this->user;
}
}
However it doesn't insert variable passed to my setter but NULL. Every other colums are filled with correct data while INSERTing just this one is not. I suppose it has something to do with the foreign key (there are proper data present in table users).
Part of controller code:
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
if ($isLogged) {
$user = $this->get('security.context')->getToken()->getUser();
$message->setUserId($user->getId());
}
$em->persist($message);
$em->flush();
When I change the setter to this it's inserted properly :
public function setUserId($user_id) {
$this->random_column_which_is_not_foreignkey = $user_id;
}
What's wrong? Thanks