0

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

2
  • Please update with the parts of the code that persist the entities Commented Oct 24, 2013 at 23:53
  • I have added some more code Commented Oct 25, 2013 at 0:49

1 Answer 1

1

You need to use JoinColumn.

Try this:

/**
 * @ORM\Entity
 * @ORM\Table(name="Messages")
 */
class Message {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="text", type="text")
     */
    protected $text;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id")
     */
    protected $user;

    public function setText($text) {
        $this->text = $text;
    }

    public function getText() {
        return $this->text;
    }

    public function setUser($user) {
        $this->user = $user;
    }

    public function getUser() {
        return $this->user;
    }
}

And in your controller:

if ($request->getMethod() == 'POST') {
    $form->bind($request);
    if ($form->isValid()) {
        if ($isLogged) {
            $user = $this->get('security.context')->getToken()->getUser();
            $message->setUser($user);
        }
        $em->persist($message);
        $em->flush();
        // ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.