Martin
Martin

Reputation: 1134

Prevent class methods overwritten in Symfony

I have added a few personalized methods to the BaseContent class in Symfony however there are other developers working on the Symfony app and probably they are having code generated by some sort of CRUD generator (I am not really familiar with Symfony so I am not sure what is being used). The problem is that my bespoke code gets overwritten when the other developers re-generate the PHP classes.

Is there anything that can be done to keep the bespoke methods in the same BaseContent class file?

Upvotes: 1

Views: 145

Answers (1)

Pendrokar
Pendrokar

Reputation: 142

You add or override BaseContent parameters and methods in your Content class, which is generated by symfony commands propel:build-model or doctrine:build-model(depending on which ORM you use), and located parent directory of the Base class. This so that when changing your schema you don't have to rewrite your personalized methods accordingly.

Propel example:

// lib/model/om/BaseContent.php
// Don't touch as it may be overwritten
abstract class BaseContent extends BaseObject  implements Persistent {
...
    public function getParameter()
    {
        return $this->parameter;
    }
...
}

// lib/model/Content.php
// add or override here
class Content extends BaseContent {
    public function getParameter()
    {
        //Do something more
        return parent::getParameter();
    }
}

Anywhere within your symfony project you always use the non-Base class, which inherits the base class.

Base classes are generated by PHP ORMs Propel (under folders 'om' and 'map') or Doctrine (under folder 'base') basing them on what is defined in your schema.

Upvotes: 2

Related Questions