当我已经拥有数据库时链接Doctrine实体

时间:2018-02-01 16:04:33

标签: database symfony doctrine-orm doctrine

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

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

之后我使用了一些insert命令来处理doctrine数据库和symfony项目中现有dataBase的所有数据。

目前一切运作良好!

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

在我找到的所有例子中,他们将模型彼此链接在一起,或者在dataBase中插入一些新数据

所以我在我的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我的文件在

之下

(EN)

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

(FR)

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定义为一对一)

1 个答案:

答案 0 :(得分: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请求