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条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作