简体   繁体   English

原则联接和类表继承

[英]Doctrine Join and Class Table Inheritance

I'm running into this problem with doctrine and no-one in my surroundings can help me with this. 我在教义上遇到了这个问题,周围的任何人都无法帮助我解决这个问题。 So I was hoping someone here knows it: 所以我希望这里的人知道这一点:

Im working with the following structure: 我正在使用以下结构:

Entity: MLT 实体:MLT

/**
 * @Entity
 */
class MLT 
{
    /**
     * @OneToMany(targetEntity="\WAD\Common\LT", cascade={"remove"}, mappedBy="term")
     */
    private $languageTerms;  
}

Entity: LT 实体:LT

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"lttext" = "LTText", "ltstring" = "LTString"})
 */
abstract class LT {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    private $id;

    ...
}

Then I have two of the same kind of entities: LTString and LTText: 然后,我有两个相同类型的实体:LTString和LTText:

Entity: LTText 实体:LTText

/**
 * @Entity
 */
class LTText extends LT {

    /**
     * @Column(type="text")
     */
    private $value;
}

Entity: LTString 实体:LTString

/**
 * @Entity
 */
class LTText extends LT {

    /**
     * @Column(type="string")
     */
    private $value;
}

Now my problem: 现在我的问题是:

I'm creating the following join: 我正在创建以下联接:

$qb = $em->createQueryBuilder();
$qb->select('naam')
    ->from('\TBIT\Entities\Naam', 'naam') //this is the MLT
    ->leftjoin('naam.languageTerms','lts') //these are the LT's
    ->orwhere($qb->expr()->like('lts.value',"'%".$word."%'"));

Now that last line is causing me the troubles. 现在,最后一行给我带来了麻烦。 It gives the error that lts doesn't have a value field. 它给出的错误是lts没有值字段。 This is sort of true since the subclass has it. 这是正确的,因为子类具有它。 But how can I make the query auto include the subclass? 但是,如何使查询自动包含子类?

Have you tried to put the $value field at LT class as protected access: 您是否尝试将$value字段放在LT类中作为protected访问:

class LTText extends LT {

    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
     private $id;

    /**
     * @Column(type="string")
     */
     private $value;

}

So, if you need to set $value as different data type just rewrite it in the subclass. 因此,如果需要将$value设置为其他数据类型,只需在子类中重写它即可。

I found I could work around this problem using a subquery. 我发现我可以使用子查询解决此问题。 Something along the lines of: 类似于以下内容:

$qb->select('naam')
    ->from('\TBIT\Entities\Naam', 'naam') //this is the MLT
    ->where(
        'naam.languageTerms in (SELECT lts FROM [class] lts'
            .' WHERE ' . $qb->expr()->like('lts.value',"'%".$word."%'")
        .')'
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM