【问题标题】:Link Doctrine Entity when I already have a dataBase当我已经有一个数据库时链接 Doctrine Entity
【发布时间】:2018-02-01 16:04:33
【问题描述】:

我正在使用一个现有的数据库(来自我的公司),他们想用 symfony 4 构建一个内部网。

我创建了我的学说文件和学说数据库。

之后,我使用了一些插入命令来处理教义数据库和我的 symfony 项目中现有数据库中的所有数据。

目前一切正常!

但我现在想在我的学说模型中添加一个关系映射(一对多、多对一、多对多)。

在我发现的所有示例中,它们将模型彼此链接到夹具内或与一些新数据插入数据库中

所以我在 Bodyshops.orm.yml 链接中添加了一个一对一的单向关系到 BodyshopsEmail.orm.yml,然后使用了

doctrine:schema:update --force

成功后,我想在控制器中使用我的 Bodyshops 模型,我得到了一个

App\Entity\BodyshopsEmail 上的主键 senderEmail 缺少值

我知道问题被提出了

Missing value for primary key id Doctrine Symfony2

Doctrine - "Missing value for primary key"

但我猜错误在这里是因为我的 bodyshopsEmail id 中有两个 ID(与 bodyshops.id 相同)和 senderMail(在 bodyshopsEmail 中设置 foreach 行)

我想知道我是否必须制作一个脚本来绑定我的实际数据关系映射以避免错误,或者错误不是来自链接而是来自 orm.yml 示例强>

希望我足够清楚,感谢阅读:p 我的文件在下面

(英文)

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

(法国)

https://www.jdecool.fr/blog/2017/09/20/tutorial-jobeet-symfony-4-partie-3a-le-modele-de-donnees.html

https://openclassrooms.com/courses/developpez-votre-site-web-avec-le-framework-symfony2/les-relations-entre-entites-avec-doctrine2#=

Bodyshops.orm.yml

App\Entity\Bodyshops:
    type: entity

    id:
        id: #B
            type: string
            length: 2

    fields:

        accessFrom:
            type: string
            length: 5
            nullable: false

        region:
            type: string
            length: 8
            nullable: false

        name:
            type: string
            length: 32
            nullable: false
        nameAlternative:
            type: string
            length: 32
            nullable: false
        nameCode:
            type: string
            length: 32
            nullable: false

        phone:
            type: string
            length: 16
            nullable: true
        fax:
            type: string
            length: 16
            nullable: true

        website:
            type: string
            length: 64
            nullable: true

        address1:
            type: string
            length: 64
            nullable: false
        address2:
            type: string
            length: 64
            nullable: true
        address3:
            type: string
            length: 64
            nullable: true
        zip:
            type: string
            length: 6
            nullable: false
        city:
            type: string
            length: 32
            nullable: false
        country:
            type: string
            length: 32
            nullable: false
        countryCode:
            type: string
            length: 4
            nullable: false

        colorBackground:
            type: string
            length: 8
            nullable: false
        colorText:
            type: string
            length: 8
            nullable: true
        workshopSlots:
            type: integer
            length: 4
            nullable: false

        departements:
            type: string
            length: 32
            nullable: false

        ipList:
            type: json_array
            nullable: true

        identRepa:
            type: string
            length: 32
            nullable: true
        statutJurid:
            type: string
            length: 2
            nullable: true
        capital:
            type: string
            length: 16
            nullable: true
        natInscript:
            type: string
            length: 64
            nullable: true
        rcsRdm:
            type: string
            length: 10
            nullable: true
        gerant:
            type: string
            length: 1
            nullable: true
        codeApe:
            type: string
            length: 4
            nullable: true
        idIntracomm:
            type: string
            length: 13
            nullable: true
    oneToOne:
        email:
            targetEntity: BodyshopsEmail
            joinColumn:
                name: id
                referencedColumnName: id

BodyshopsEmail.orm.yml

App\Entity\BodyshopsEmail:
    type: entity

    id:
        id:
            type: string
            length: 2
            nullable: false

        senderEmail:
            type: string
            length: 64
            nullable: false

    fields:
        senderName:
            type: string
            length: 64
            nullable: false

        replyTo:
            type: string
            length: 64
            nullable: true


        smtpHost:
            type: string
            length: 32
            nullable: false

        smtpPort:
            type: string
            length: 6
            nullable: false

        smtpLogin:
            type: string
            length: 64
            nullable: false

        smtpAuth:
            type: string
            length: 32 
            nullable: false

        smtpSecurity:
            type: string
            length: 32
            nullable: true


        popHost:
            type: string
            length: 32
            nullable: true

        popPort:
            type: string
            length: 6
            nullable: true

        imapHost:
            type: string
            length: 32
            nullable: false

        imapPort:
            type: string
            length: 6
            nullable: false


        receiveLogin:
            type: string
            length: 64
            nullable: false

        receiveAuth:
            type: string
            length: 32
            nullable: false

        receiveSecurity:
            type: string
            length: 32
            nullable: false


        globalPassword:
            type: string
            length: 64
            nullable: false


        signatureFile:
            type: string
            length: 256
            nullable: true

我认为向您展示模型没有用,但请确保每个值都作为 getter 和 setter(甚至 $email 由一对一定义)

【问题讨论】:

  • a script that will bind my actual data relationship mapping 是什么意思?
  • 在每个模型中设置新关系的脚本

标签: database symfony doctrine-orm doctrine


【解决方案1】:

如果您使用数据库和模型中已经存在的列,则不会更改架构。

例如,如果您的现有数据库中有一个表 cart 和一个列 customer_id

你可以的

/**
 * One Cart has One Customer.
 * @OneToOne(targetEntity="Customer", inversedBy="cart")
 * @JoinColumn(name="customer_id", referencedColumnName="id")
 */
private $customer;

您可以使用php bin/console doctrine:schema:update --dump-sql 来检查是否有任何 sql 请求需要进行更改

【讨论】:

  • 我和他们遇到了同样的问题,但是所有的解决方案都不起作用(我没有像第一个解决方案那样删除数据库,原因正是我不想要的)@987654321 @
  • 我使用了“set foreign_key_checks=0;”它没有用,所以我使用了“set GLOBAL foreign_key_checks=0;”我能够更新我的 db::schema。但是,当我尝试使用实体链接到我的一对一单向关系 ATable->BTable 时,它​​说我“BModel 上的主键 BMember 缺少值”(我的 A.id 与 B.id 一对一连接其中 id 真的是写 'id' 这可能是问题所在?)我的 B 表有两个 idKey 一个是 id 另一个是错误消息中的 bMember
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 2014-11-21
  • 2016-07-08
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
相关资源
最近更新 更多