douyinzha5820 2011-10-25 08:27
浏览 28
已采纳

PHP Doctrine使用鉴别器映射查询继承的类

In this example:

http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#single-table

we see that

SELECT e FROM Entities\Employee e WHERE e.name = 'test'

makes this query:

 SELECT p0_.id AS id0, p0_.name AS name1, p0_.department AS department2,
        p0_.discr AS discr3 FROM Person p0_ WHERE (p0_.name = ?) AND p0_.discr IN ('employee')

Is there a way to query records only with the discriminator "Person"?

This:

SELECT e FROM Entities\Person e WHERE e.name = 'test'

returns all the records.

Any ideas?

  • 写回答

1条回答 默认 最新

  • doujing8435 2012-05-03 17:12
    关注

    By default the parent entity in a Inheritance Mapped Entity does not seem to use the Discriminator rules.

    I got round this by creating a base class/entity.

    /**
     * @Entity
     * @InheritanceType("SINGLE_TABLE")
     * @DiscriminatorColumn(name="discr", type="string")
     * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
     */
    class PersonBase
    {
        /**
         * @Id @Column(type="integer")
         * @GeneratedValue
         */
        protected $id;
    
        /**
         * @Column(type="string", length=50)
         */
        protected $name;
    
        // ...
    
    }
    
    /**
     * @Entity
     */
    class Person extends PersonBase
    {
    }
    
    /**
     * @Entity
     */
    class Employee extends PersonBase
    {
        /**
         * @Column(type="string", length=50)
         */
        private $department;
    
        // ...
    }
    

    Now Person will use discriminator rules, you need never directly reference PersonBase.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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