PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to change default doctrine entity scheme?

 May 12, 2022     doctrine-orm, mysql, php, symfony     No comments   

Issue

The default one to one relation scheme is

CREATE TABLE Product (
    id INT AUTO_INCREMENT NOT NULL,
    shipping_id INT DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE Shipping (
    id INT AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE Product ADD FOREIGN KEY (shipping_id) REFERENCES Shipping(id);

But I need to use this scheme but with it some modification ON DELETE SET NULL ON UPDATE SET NULL;:

CREATE TABLE Product (
    id INT AUTO_INCREMENT NOT NULL,
    shipping_id INT DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE Shipping (
    id INT AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE Product ADD FOREIGN KEY (shipping_id) REFERENCES Shipping(id) ON DELETE SET NULL ON UPDATE SET NULL;

Can I create this scheme with doctrine entity configuration?

P.S. I has two entities: Vendor and Image

BW\ShopBundle\Entity\Vendor:
    type: entity
    table: vendors
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    oneToOne:
        image:
            targetEntity: BW\FileBundle\Entity\Image
            cascade: [persist, remove]

and

BW\FileBundle\Entity\Image:
    type: entity
    table: images
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        filename:
            type: string
            length: 255
            nullable: true

And I use a oneToOne Unidirectional relation.

So when I remove Image object, I has an error, becouse I had Unidirectional relation (And it's important to know that I can't to change relation to bidirectional)


Solution

Have you tried the Orphan Removal ?

From the offcial documentation :

There is another concept of cascading that is relevant only when removing entities from collections. If an Entity of type A contains references to privately owned Entities B then if the reference from A to B is removed the entity B should also be removed, because it is not used anymore.

OrphanRemoval works with one-to-one, one-to-many and many-to-many associations

You chould use the orphanRemoval=true option like this :

@OneToOne(targetEntity="Image", orphanRemoval=true)


Answered By - blackbishop
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing