Doctrine2 - Inheritance mapping, querying sub-classes
Asked Answered
S

1

8

I have joined table inheritance (lets call it Action) and I need to get all records of Action that satisfy the condition, but the fields are in sub-classes ? Is there way to access sub-class fields without writing Native Query, but use DQL ? (or queryBuilder)?

Sestertium answered 21/3, 2012 at 18:22 Comment(5)
Does the fields belong to only one class, or for several sub-classes for a query?Judie
all sub-classes have a relationship to a different class, but the property is always same.Sestertium
I don't understand what you are trying to do exactly. Can you post a small exapmle of the entities and what you want to query?Judie
I have 10 entities that extends Joined Inheritance Mapping Entity, each sub-class entity have relation ship to a different object, which object have a different condition to show. For example I have CommentAction Extends Actions which have a relationship to Comment, which have condition to show when isApproved = true but another class, RateAction extends Actions have relationship to Ratings, which have a property to show isActive and I want to get all Actions that satisfy sub-class's relationship's condition to show.Sestertium
or another case I have 10 entities that extend class Object, I want to order them by datePublished,but only 2 of them has that property, I can make AND WHERE object INSTANCE OF That 2 classes, but when I ORDER BY datePublished, there is no such property, because it is in sub-classes.Sestertium
B
0

If you have a subclass you can directly query for that subclass. The parent class's properties will be automatically inherited(duh). I believe this is what you are looking for (based on how I have implemented class table inheritance).

class User{
  private $id;
  private $username;
}

class Merchant extends User{
  private $bizname;
  private $isActive;
}

Now according to you if I have to get the active merchants, I would do the following, and it works for me:

$qb->select('m.bizname')
   ->from('Merchant','m')
   ->where('m.isActive = :flag')
   ->setParameter('flag',TRUE);
Bousquet answered 9/4, 2012 at 17:7 Comment(5)
Yes, that's works, but I want ->from('User','u')->andWhere('userSubClass.isActive :flag'), that's the problem.Sestertium
The query above does give all the users where merchant is active.. Think of this.. when you want to select users where subclass active.. that means it will only return the user of type sub class.. so selecting merchants which are active returns you all the properties of user as well as merchantBousquet
Yes but I have 5 different sub-classes, and every sub-class have different condition to verify if it's active. For instance:Sestertium
For instance: Comment, rating, favorite all extends Action. Action have dateTime column (which I orderBy). I want to get ALL actions where (CASE WHEN Action INSTANCE OF Comment THEN comment.isApproved WHEN Action INSTANCE OF Rating THEN rating.isActive WHEN Action INSTANCE OF Favorite THEN favorite.isRemoved END) = 1, or something. What I do now is to query every sub-class with specific WHERE Stmt, and array_merge() them in application layer.Sestertium
@Sestertium when technology does not support what you want to do, then you need to think about your application around the available technology. Also I can confirm that CASE statement work with querybuilder.Bousquet

© 2022 - 2024 — McMap. All rights reserved.