Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

How to use the admin generator along with propel inheritance

The problem

Say that you have a model object Son that inherits from the model object Father, via propel inheritance. You want to create an admin generator interface for the object Son. The problem is that propel does not generate a SonPeer class, so you'll have to call:

symfony propel-init-admin Father

but the list function will list all the Father objects instead of the Son objects only. Same problem with the create function that will create a Father object, not a Son object.

The solution

Here is a solution. You will have to overload getFatherOrCreate and addFiltersCriteria:

// add this in the actions.class.php of your admin module
  protected function addFiltersCriteria(&$c)
  {
    $c->add(FatherPeer::CLASS_KEY, FatherPeer::CLASSKEY_SON);
    parent::addFiltersCriteria($c);
  }  
 
  protected function getFatherOrCreate ($id = 'id')
  {
    $son = parent::getFatherOrCreate($id);
 
    if ($son->isNew()) // if it is a new one then we create a Son object
      $son = new Son();
   else
     $this->redirect404Unless($son->isSon()); // we check that we really got a son object
 
   return $son;
  }

Now everything will work as if you were using the Son object.

by Olivier Verdier on 2006-05-25, tagged admin  generator  inheritance  propel 

Comments on this snippet

gravatar icon
#1 Tom Cowin on 2006-06-15 at 11:19

Olivier - thanks for posting this - I could not get it working against release 1369, (don't know if I missed something?) but I did the following to get mine working -

symfony propel-init-admin son Father ^^^

and inserted the following:

protected function addFiltersCriteria(&$c) { $c->add(FatherPeer::CLASS_KEY, FatherPeer::CLASSKEY_SON); ^^^^^^^^^^^^ parent::addFiltersCriteria(&$c); }

protected function getFatherOrCreate ($id = 'id') { $son = parent::getFatherOrCreate($id);

//CLASS_KEY of new Father object would be? null? I've multiple children if that makes a difference...
//so check to see if object is new first
if ($son->isNew()) 
  return new Son();
//and _then_ insure existing object is of the correct type
elseif($son->getClassKey() != FatherPeer::CLASSKEY_SON)
  $this->redirect404();
 
return $son;

}

gravatar icon
#2 Olivier Verdier on 2006-08-01 at 05:36

Oh! Thank you Tom, you are totally right. I updated the snippet with a corrected version.

You need to create an account or log in to post a comment or rate this snippet.