Code snippets for symfony 1.x


Refine Tags

Snippets tagged "propel subclasses"

Listing sub-classes

When you use class inheritance in Propel, it can be quite useful to get a sorted array of all the descendant classes of any base class in your model.

First we need a simple method to determine whether a particular class inherits from a base class. We do this by ascending the class inheritance tree until we either a) find the base class we're looking for or b) reach the top of the tree

  function inheritsFrom($AClass,$ABaseClass)
    while (($strClass!='') and !$bIsSubClass)
    return $bIsSubClass;     

Then we can extend the sfCore class, as shown below, to generate an array of sub-classes, that exist in the "model". I've added a couple of extra options, to allow class names to be converted to class key constants, and abstract classes to be ignored, if desired.

class EnhancedCore extends sfCore
  public static function subClassesOf($AClass,$AKeyField='',$AIgnoreAbstractClasses=false)
    foreach(self::$classes as $strClass => $strPath)
      //ignore classes outside the "model" library folder   
      if (substr($strPath,0,$intPathLength)==$modelDir)
        if (inheritsFrom($strClass,$AClass))
          if ($AIgnoreAbstractClasses)
            $reflector=new ReflectionClass($strClass);
          if (!$bIgnoreClass)
            if ($AKeyField!='')
              $peerReflector=new ReflectionClass($AClass.'Peer');                      
              if (array_key_exists($strConstant,$peerReflector->getConstants()))
    return $arrClasses;

So how about a simple example, in order to demonstrate its use. Let's say we have a schema.xml file like so:

<table name="person" 
    <column name="id" type="INTEGER" required="true" autoIncrement="true" primaryKey="true" />
    <column name="name" type="VARCHAR" size="50" required="true"/>            
    <column name="classkey" type="INTEGER" required="true" inheritance="single">    
      <inheritance key="1" class="Employee" extends="person"/>
      <inheritance key="2" class="Customer" extends="person"/>
      <inheritance key="3" class="Manager" extends="Employee"/>                        

Symfony produces one base class (Person) and one peer class (PersonPeer) from this schema, as well as three sub-classes (Employee,Customer,Manager). Then the following simple code


outputs >>>

array(3) {
    string(8) "Customer"
    string(8) "Employee"
    string(7) "Manager"
by Todd Martin on 2008-01-21, tagged inheritance  propel  subclasses