【问题标题】:Integrity constraint violation: 1048 Column 'user_id' cannot be null完整性约束违规:1048 列 'user_id' 不能为空
【发布时间】:2016-11-07 05:53:24
【问题描述】:

当我想使用 setUserId() 方法设置 user_id 时,我不知道为什么会出现此错误。

我在 User 类和 Document 类之间有一个 ManyToOne 关系。这样一个用户就可以拥有更多的文档。是因为关系吗?

用户.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;
}

文档.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;
}

我在这里保存我需要的东西:

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);
}

我收到了这个错误:

执行'INSERT INTO document (title, document, user_id) VALUES (?, ?, ?)' with params ["oim", null, null]:

SQLSTATE[23000]:违反完整性约束:1048 列“user_id” 不能为空

..所以我的文档可以是 null 但 user_id 不是。谁能帮帮我?

【问题讨论】:

  • 好吧,告诉数据库接受一个 Null 值到 user_id,或者设置一个默认值 0,例如
  • 如果要允许null需要在注解中设置nullable=true
  • 我不想 user_id 为空,我想要 $id = $this->getUser()->getId(); .但它不会将其持久化到数据库
  • 在使用关联时,您不会手动设置用户 ID,而只需将用户属性设置为用户对象。

标签: php doctrine-orm doctrine symfony


【解决方案1】:

简单尝试:

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

代替:

$document->setUserId($id);

Doctrine 知道如何管理关系以及如何填充正确的用户 ID,因此不需要 setUserId 方法。

希望有帮助

【讨论】:

  • 嗨@lorenos,如果这个或任何答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
【解决方案2】:

我认为您应该将$id 属性添加到用户实体并做某事。喜欢:

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);
}

再一次:您需要调试$this->getUser()->getId(),但不是数据库或实体,恕我直言

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2020-10-26
    • 2018-05-01
    • 2021-09-25
    • 2020-03-29
    • 2018-03-04
    • 2018-05-17
    相关资源
    最近更新 更多