dousi4900 2014-05-31 20:18
浏览 41
已采纳

使用Doctrine中的类表继承从子表中删除行

I have made a really basic example with two models.

"Singer" extends from "Person"

I am using class table Inheritance with these two models:

<?php
namespace models;

/**
 * @Table(name="persons")
 * @entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="type", type="string")
 * @DiscriminatorMap({"singer" = "\models\Singer"})
 */
abstract class Person {

    /**
     * @Id
     * @Column(type="integer", nullable=false, name="id_person")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @column(type="string", nullable=false) */
    protected $name;

The singer model looks like:

namespace models;

/**
 * @Table(name="singers")
 * @entity
 */
class Singer extends Person{

    /** @column(type="string", nullable=false) */
    protected $gender;
}

Workflow

Consider this scenario.

  1. In the db I have these rows:

    persons table:

    id_person | name | type
    -----------------------
    1           john   singer
    

    singers table:

    id_person | gender
    ------------------
    1           pop  
    
  2. I proceed to remove this singer:

    $singer = $em->find('models\Singer', 1);
    $em->remove($singer);
    $em->flush();
    
  3. After execute the code above, I check again the database and I found this:

    persons table:

    id_person | name | type
    -----------------------
    (empty)
    

    singers table:

    id_person | gender
    ------------------
    1           pop  
    

    As you note, the row from child table was not removed as expected.

  4. So, after searching in doctrine's documentation, it states:

    When you do not use the SchemaTool to generate the required SQL you should know that deleting a class table inheritance makes use of the foreign key property ON DELETE CASCADE in all database implementations. A failure to implement this yourself will lead to dead rows in the database.

    So, I proceed to alter persons table as below:

    ALTER TABLE persons 
    ADD CONSTRAINT fk_persons_1
    FOREIGN KEY (id_person)
    REFERENCES singers (id_person)
    ON DELETE CASCADE
    ON UPDATE NO ACTION;
    

Now, the problems get complicated:

  • when I remove a singer, the information still there, even the persons table was altered in order to delete from singers table too.
  • When I try to insert a new singer like

    $singer = new \models\Singer('Zara', 'rock');
    $em->persist($singer);
    $em->flush();
    

    it throws an exception:

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`practica`.`persons`, CONSTRAINT `fk_persons_1` FOREIGN KEY (`id_person`) REFERENCES `singers` (`id_person`) ON DELETE CASCADE ON UPDATE NO ACTION)' in /var/www/html/doctrine/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:138 Stack trace: #0 /var/www/html/doctrine/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php(138): PDOStatement->execute(NULL) #1 /var/www/html/doctrine/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php(165): Doctrine\DBAL\Statement->execute() #2 /var/www/html/doctrine/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(929): Doctrine\ORM\Persisters\JoinedSubclassPersister->executeInserts() #3 /var/www/html/doctrine/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(318): Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata)) #4 /var/www/html/doctrine/vendor/doct in /var/www/html/doctrine/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php on line 47
    

So, basically, all I need is to remove the information in child table too in MySQL db. But I do not get it.

  • 写回答

2条回答 默认 最新

  • drp935159 2014-06-04 16:39
    关注

    Try reversing the FOREIGN KEY i.e. remove it from your persons table and add it to your singers table

    ALTER TABLE singers 
    ADD CONSTRAINT fk_singers_1
    FOREIGN KEY (id_person)
    REFERENCES persons (id_person)
    ON DELETE CASCADE
    ON UPDATE NO ACTION;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗