Symfony 无法为双向 onetoOne 关系插入记录

Symfony Not able to insert record for Bidirectional onetoOne relationship

我有两个表 UserSurvey 一个用户只能填写一个调查,所以我在两者之间设置的关系是 Bidirectional onetoOne

应用程序的流程是用户必须先注册然后填写调查,注册就可以了。

我遇到的问题是,当用户尝试保存调查时,他收到以下错误

[1/2] PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mv2.mv_survey, CONSTRAINT FK_E81A494BBF396750 FOREIGN KEY (id) REFERENCES mv_users (survey_id))

我正在使用 YML 而不是 annotations 所以这是我的 orm.yml 用户和调查文件

用户

UserBundle\Entity\User:
    type: entity
    table: mv_users
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        username:
            type: string
            length: 255
        name:
            type: string
            length: 255
        email:
            type: string
            length: 255
            unique: true
        role:
            type: string
            length: 255
        password:
            type: string
            length: 255
        salt:
            type: string
            length: 255
            nullable: true
        isActive:
            type: boolean
        survey_id:
            type: integer
            nullable: true
    oneToOne:
        survey_allias:
            targetEntity: XYZBundle\Entity\survey
            joinColumn:
                name: survey_id
                referencedColumnName: id

    lifecycleCallbacks: {  }

调查

XYZBundle\Entity\survey:
    type: entity
    table: mv_survey
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        gender:
            type: string
            length: 255
            nullable: true
        dob:
            type: date
            nullable: true
        postcode:
            type: integer
            nullable: true            
        userID:
            type: integer
            column: user_id
    oneToOne:
        user_allias:
            targetEntity: UserBundle\Entity\User
            inversedBy: survey_allias
            joinColumn:
                name: id
                referencedColumnName: survey_id

    lifecycleCallbacks: {  }

我知道我可能应该使用 Fixtures 但只是为了测试控制器内部的一切是否正常工作我这样做是为了插入数据

$survey = new Survey();
$survey->setGender('Male');
$survey->setDob(new \DateTime());
$survey->setUserID('1');
$manager->persist($survey);
$manager->flush();

这给出了

的错误

[1/2] PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mv2.mv_survey, CONSTRAINT FK_E81A494BBF396750 FOREIGN KEY (id) REFERENCES mv_users (survey_id))

我将非常感谢对此提供的任何帮助,我什至删除了整个数据库模式并按照其他帖子的建议重新创建了它,但这也没有帮助,我在这里缺少什么?

$survey->setUserID('1');

这是不好的做法。你应该直接设置实体。

你应该这样做:

用户orm.yml

您的 orm.yml 或实体中都不需要 surveyID 属性。

oneToOne:
    survey:
        targetEntity: XYZBundle\Entity\survey
        mappedBy: user

用户实体

protected $survey;

public function setSurvey(Survey $survey)
{
    $this->survey = $survey;
}

public function getSurvey()
{
    return $this->survey;
}

调查orm.yml

您的 orm.yml 或实体中都不需要用户 ID 属性。

oneToOne:
    user:
        targetEntity: UserBundle\Entity\User
        inversedBy: survey
        joinColumn:
            name: survey_id
            referencedColumnName: id

调查实体

protected $user;

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

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

养成让 Doctrine 处理外键之类的习惯。您必须为关系实现的所有实体都是 setters/getters 直接用于实体(不是 id):

$entity1->setEntity2($entity2);