![]() |
|
Snippets |
|
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.
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.
Comments on this snippet
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);
}
Oh! Thank you Tom, you are totally right. I updated the snippet with a corrected version.