教义一对多关系不会保存 - 完整性约束违规 [英] Doctrine One-To-Many Relationship Won't Save - Integrity Constraint Violation

查看:138
本文介绍了教义一对多关系不会保存 - 完整性约束违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Doctrine ORM协会。我已经阅读了几个教程和在线文档,但它不起作用,我老实说不清楚我在这里做错了什么。好像我对教义的实验很有意思或者是错过。任何帮助将不胜感激。



我想拥有一个用户实体和UserHistory实体,它具有单个用户的多行。对我来说听起来像一对多。我不一定需要它是双向的。



我正在向用户添加一个历史记录项,当用户被保存时会导致错误,因为user_history表中没有设置user_id列。



实体:

 #Entity\User.php 
<?php
命名空间应用程序\Entity;

使用Doctrine\ORM\Mapping作为ORM;
使用Doctrine\Common\Collections\ArrayCollection;

/ **
* @ ORM\Entity
* /
类用户
{
/ **
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =AUTO)
* @ ORM\Column(type =integer)
* /
protected $ ID;

/ **
* @ ORM\Column(type =string,length = 255)
* /
protected $ username;

/ **
* @ ORM\OneToMany(targetEntity =UserHistory,mappedBy =user,cascade = {persist,remove})
* @ ORM\JoinColumn(name =id,referencedColumnName =user_id)
* /
protected $ history;

public function __construct()
{
$ this-> setHistory(new ArrayCollection());
}

public function getHistory()
{
return $ this-> history;
}
}

#Entity\UserHistory.php
<?php
命名空间应用程序\Entity;

使用Doctrine\ORM\Mapping作为ORM;

/ **
* @ ORM\Entity
* @ ORM\Table(name =user_history)
* /
class UserHistory
{
/ **
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =AUTO)
* @ ORM\Column type =integer)
* /
protected $ id;

/ **
* @ ORM\ManyToOne(targetEntity =User)
* @ ORM\JoinColumn(name =user_id,referencedColumnName =id ,nullable = false)
* /
protected $ user;

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

在我的控制器中,我在尝试: / p>

  $ em = $ this-> getUserService() - > getEntityManager(); 
$ user = $ em-> find('Picpara\Entity\User',1);
$ history = new UserHistory();
$ user-> getHistory() - > add($ history);
$ em-> persist($ user);
$ em-> flush();

所有这些都导致完整性约束违规。



Doctrine\DBAL\DBALException



文件:
/myproject/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php :91



消息:



执行INSERT INTO user_history(user_id)VALUES(?) 'with params [null]:



SQLSTATE [23000]:完整性约束违规:1048列'user_id'不能为空


$ b $像我说的那样,我已经在这个地方挖了一下SO,google,试着在注释里移动一下。这是我得到的。不知道我在做错什么任何人都可以帮忙吗?

解决方案

问题是您没有在历史类中设置用户实体。协会的内容不会自动执行。

  class UserHistory 
{
public function setUser($ user){$ this-> user = $用户;

class User
{
public function addHistory($ history)
{
$ this-> history-> add($ history) ;
$ history-> addUser($ this); // ***这是你缺少的
}

//在你的控制器类
$ user-> addHistory($ history);


I'm attempting to use Doctrine ORM Associations. I've read several tutorials and the online docs, but it's not working, and I'm honestly not sure what I'm doing wrong here. Seems like my experiments with Doctrine are pretty hit or miss. Any assistance would be appreciated.

I'd like to have a User entity and UserHistory entity, which has multiple rows for a single User. That to me sounds like One-To-Many. I don't necessarily need it to be bidirectional.

The problem I'm having is adding a history item to a user results in an error when the user is saved, because the user_id column is not set in the user_history table.

Entities:

# Entity\User.php
<?php
namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

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

    /**
     * @ORM\Column(type="string",length=255)
     */
    protected $username;

    /**
     * @ORM\OneToMany(targetEntity="UserHistory", mappedBy="user", cascade={"persist","remove"})
     * @ORM\JoinColumn(name="id", referencedColumnName="user_id")
     */
    protected $history;

    public function __construct()
    {
        $this->setHistory(new ArrayCollection());
    }

    public function getHistory()
    {
        return $this->history;
    }
}

# Entity\UserHistory.php
<?php
namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user_history")
 */
class UserHistory
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
     */
    protected $user;

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

In my controller, I'm trying this:

    $em = $this->getUserService()->getEntityManager();
    $user = $em->find('Picpara\Entity\User', 1);
    $history = new UserHistory();
    $user->getHistory()->add($history);
    $em->persist($user);
    $em->flush();

All of which results in an integrity constraint violation.

Doctrine\DBAL\DBALException

File: /myproject/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:91

Message:

An exception occurred while executing 'INSERT INTO user_history (user_id) VALUES (?)' with params [null]:

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

Like I said, I've dug around here on SO, googled, tried moving things around in the annotations. This is as far as I get. Not sure what I'm doing wrong. Can anyone help?

解决方案

The problem is that you are not setting the user entity in your history class. The association stuff does not do this automatically.

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

class User
{
    public function addHistory($history)
    {
        $this->history->add($history);
        $history->addUser($this);  // *** This is what you are missing
    }

// In your controller class
$user->addHistory($history);

这篇关于教义一对多关系不会保存 - 完整性约束违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆