2

i don't know why I'm getting this error when I wanna set user_id with setUserId() method.

I have a ManyToOne relationship between User class and Document class. So that one user can have more documents. Is it because the relationship?

User.php

/**
 * @ORM\OneToMany(targetEntity="Document", mappedBy="users")
 */
private $documents;


/**
 * Add document
 *
 * @param \AppBundle\Entity\Document $document
 *
 * @return User
 */
public function addDocument(\AppBundle\Entity\Document $document)
{
    $this->documents[] = $document;

    return $this;
}

Document.php

/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="documents")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $users;

/**
 * Set users
 *
 * @param \AppBundle\Entity\User $users
 *
 * @return Document
 */
public function setUsers(\AppBundle\Entity\User $users)
{
    $this->users = $users;

    return $this;
}

/**
 * Set userId
 *
 * @param integer $userId
 *
 * @return Document
 */
public function setUserId($userId)
{
    $this->user_id = $userId;

    return $this;
}

Here im saving what i need:

public function createAction(Request $request)
{
    $content = $request->getContent();
    $json = json_decode($content);

    $id = $this->getUser()->getId();
    $repository = $this->getDoctrine()->getRepository('AppBundle:Document');
    $document = new Document();
    //$user_id = $id;

    $document->setTitle($json->title);
    $document->setUserId($id);

    $em = $this->getDoctrine()->getManager();
    $em->persist($document);
    $em->flush();

    return new JsonResponse($json);
}

And I'm getting this error:

An exception occurred while executing 'INSERT INTO document (title, document, user_id) VALUES (?, ?, ?)' with params ["oim", null, null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null

..So my document can be null but user_id not. Can anyone help me?

Matteo
  • 37,680
  • 11
  • 100
  • 115
lorenos
  • 115
  • 2
  • 12
  • Well, tell the DB to accept a Null value to user_id, or set a default 0 for example – Damien Pirsy Jul 05 '16 at 11:51
  • If you want to allow `null` you need to set `nullable=true` in the annotation – mickadoo Jul 05 '16 at 11:51
  • Possible duplicate of [Symfony2 and Doctrine, Column cannot be null with OneToOne relationships](http://stackoverflow.com/questions/14465956/symfony2-and-doctrine-column-cannot-be-null-with-onetoone-relationships) – mickadoo Jul 05 '16 at 11:54
  • I don't wanna user_id to be null, i wanna the id of the logged in user with $id = $this->getUser()->getId(); . But it won't to persist it to database – lorenos Jul 05 '16 at 11:55
  • When working with associations, you would not set the user id manually, but only set the user property to a User object. – Gerry Jul 05 '16 at 12:12
  • How to set user property to User object? Can you give me an example maybe? – lorenos Jul 05 '16 at 12:14

2 Answers2

4

Try simply:

$document->setUser($this->getUser());

instead of:

$document->setUserId($id);

Doctrine know how to manage the relation and how to populate the correct user id, so the method setUserId is not necessary.

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
  • Hi @lorenos if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Matteo Jul 05 '16 at 12:27
0

I think you should add $id property to User entity and to do smth. like:

public function createAction(Request $request)
{
$content = $request->getContent();
$json = json_decode($content);

//some checking for existing user id
$user = new User();
$user->setId($this->getUser()->getId());
$repository = $this->getDoctrine()->getRepository('AppBundle:Document');
$document = new Document();


$document->setTitle($json->title);
$document->setUsers($user);

$em = $this->getDoctrine()->getManager();
$em->persist($document);
$em->flush();

return new JsonResponse($json);
}

And once more: you need to debug $this->getUser()->getId() but not the database or entities, imho

D.Samchuk
  • 1,219
  • 9
  • 9