Doctrine 2 association without foreign key constraints

后端 未结 6 1554
醉酒成梦
醉酒成梦 2020-12-16 03:40

I\'m in the process of converting a legacy PHP application to Symfony 2. The application data is not very consistent at the moment, so I would like to avoid creating foreign

相关标签:
6条回答
  • 2020-12-16 04:18

    Basically you can't prevent the sql commands from being generated. At least not without diving into the Doctrine code.

    However, you don't need to actually apply the constraints to your database. D2 will work just fine without them.

    0 讨论(0)
  • 2020-12-16 04:23

    You should leave out the ManyToMany and the JoinColumn definitions and handle the Manufacturer property in your custom Product repository with a public getManufacturer method that extends the auto generated Product repository.

    0 讨论(0)
  • 2020-12-16 04:35

    I had a problem with the same command. I got the exception:

    SQLSTATE[HY000]: General error: 1005 Can't create table 'xxx.#sql-66c_3e' (errno: 150)

    For me it helped to declare the column as unique (in your case id).

    Now app/console doctrine:schema:update runs fine again.

    0 讨论(0)
  • 2020-12-16 04:35

    You have to set lost foreign keys to null, then you can set your contstraint. Following query gives you the ids from datasets to change:

    select p.id from product p
    left join manufacturer m on m.id=p.manufakturer_id
    where m.id is null
    
    0 讨论(0)
  • 2020-12-16 04:39

    Try to add onDelete="CASCADE" like

    * @ORM\JoinColumn(name="manufacturer_id", referencedColumnName="id", onDelete="CASCADE")
    
    0 讨论(0)
  • 2020-12-16 04:40

    I had to go through the same process recently and fortunately there is an easy solution, just add nullable=true to the column's annotation.

    This will work as long as the existing data is valid, in my case I had to change 0's to NULL's and change keys that didn't exist anymore to NULL.

    0 讨论(0)
提交回复
热议问题