Snippets

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

Navigation

propel behavior to extend classes with separate tables

I needed to have many subclasses inherited from a main class.

To do that with propel, you need to have all your classes in one big table. I wanted to have separate tables.

Then I discovered the propel behaviors and it does that very well !

Let's see an example with a master class "element" and a subclass "subElement" :

by Vincent Texier on 2007-08-13, tagged behavior  inheritance  model  propel 

Comments on this snippet

gravatar icon
#1 Luciano Andrade on 2007-08-15 at 12:28

Great snipe.

some minur corrections in project/config/config.php shuld say

    array('ElementBehavior', 'setCreated'),
    array('ElementBehavior', 'getCreatedAt'),

and in project/lib/model/Element.php

public function setContent(BaseObject $object, $value) {
            if (!$object->getElement()) $object->setElement(new Element());
            $object->getElement()->setContent($value);
    }
gravatar icon
#2 Vincent Texier on 2007-08-15 at 05:43

Ooops! thanks! Corrected...

gravatar icon
#3 Benoit RENAUD on 2007-09-10 at 06:26

I'm trying to use it but I have a problem: The behavior seems to work, I don't have any error but when I use a getter like "$sub_element->getName();" inheritance doesn't work ! Symfony doesn't find this function. How can I find where I made a mistake ? I did a print_r($sub_element) but no Element in there... is this normal ? When I change something in behaviors symfony react so I think that the problem is near from the display of $sub_element. Any help ? Thx

gravatar icon
#4 Benoit RENAUD on 2007-09-12 at 04:23

So after two days looking for a solution I found the answer : a "symfony propel-build-model" ! After this tutorial you've to rebuild the model ! aaaaahh Only that ! I'm sick :s ;) thx for the tutorial

gravatar icon
#5 Benoit RENAUD on 2007-09-17 at 11:17

It works ! It's a good way to go for inheritance !

I already have a little problem: It does not work with "propel-load-data", I can't correctly populate my database this way :s

When I make a propel-load-data I have this: The class "BaseSubElement" has already a mixin for method "setName". It's like if propel was loading two times my project/lib/model/SubElement.php !

Any idea ?

gravatar icon
#6 Marcin Nowak on 2008-01-27 at 06:20

Benoit: I had same problem. sometimes config/config.php is included more than once by symfony or pake tasks, so sfPropelBehavior::registerMethods() is called again. Try to move behavior code and config to separate plugin (plugin configs are included only once).

gravatar icon
#7 Thomas Parisot on 2008-03-05 at 10:59

It seems to work fine within the code. Did you try with an admin-generator for the SubElement ? Fields for SubElement are well displayed but fields for Element are grayed out (disabled=true). If I add disabled=false in the generator.yml, i can edit these fields, they are saved but not displayed back on screen.

It's a bit odd.

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