简体   繁体   中英

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

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

Entity: 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:

Entity: LTText

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

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

Entity: 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. 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:

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.

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."%'")
        .')'
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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