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

CappY
  • 1,510
  • 1
  • 17
  • 32
  • Does the fields belong to only one class, or for several sub-classes for a query? – Hakan Deryal Mar 21 '12 at 18:44
  • all sub-classes have a relationship to a different class, but the property is always same. – CappY Mar 21 '12 at 18:45
  • 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? – Hakan Deryal Mar 21 '12 at 18:48
  • 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. – CappY Mar 21 '12 at 22:19
  • 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. – CappY Mar 21 '12 at 22:22

1 Answers1

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);
Broncha
  • 3,794
  • 1
  • 24
  • 34
  • 2
    Yes, that's works, but I want ->from('User','u')->andWhere('userSubClass.isActive :flag'), that's the problem. – CappY Apr 10 '12 at 13:45
  • 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 merchant – Broncha Apr 10 '12 at 13:58
  • Yes but I have 5 different sub-classes, and every sub-class have different condition to verify if it's active. For instance: – CappY Apr 11 '12 at 21:02
  • 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. – CappY Apr 11 '12 at 21:15
  • @CappY 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. – Broncha Oct 19 '13 at 13:00