# Using bake without losing custom methods?

**URL:** <https://discourse.cakephp.org/t/using-bake-without-losing-custom-methods/4392>\
**Category:** Need Help\
**Created:** [June 10, 2018, 5:36pm UTC](https://discourse.cakephp.org/t/using-bake-without-losing-custom-methods/4392 "2018-06-10T17:36:58Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [June 11, 2018, 5:10pm UTC](https://discourse.cakephp.org/t/using-bake-without-losing-custom-methods/4392/5 "2018-06-11T17:10:36Z")

</div>

> How could software like “bake” detect, which parts should be updated and which shouldn’t?

I don’t really know, well, maybe detect some stuff based on conventions?  
I don’t know if `afterSave()` can be generated automagically anywhere in bake?  
If not, I think CakePHP could be able to load/overwrite methods in some other file.  
like for example `Model/Extension/UsersTableExtension.php` (please don’t hate if I made mistakes, it’s just a quick mockup):

```
<?php
  namespace App\Model\Table;

  // Just an example, the Devs should come up with the convention n such
  class UsersTableExtension extends UsersTable {
    public function afterSave(Event $event, EntityInterface $entity, \ArrayObject $options) {
      // do stuff
    }
  }

```

Then bake doesn’t have to do anything in the `UsersTableExtension.php`, so it doesn’t need to be overwritten.  
`afterSave()` is obviously just one example this could be used for.  
Basically, we take all the extending stuff (like `afterSave()`, `beforeDelete()`, `beforeFind()` etc.) and put it somewhere else.  
CakePHP will then load up the `UsersTable.php` and see if there’s a `UsersTableExtenstion.php`.  
If so, it will load and overwrite this stuff in `UsersTable.php`.  
This way, `bake` can overwrite the model itself (like the `initialize()`, `validationDefault()` etc.), but won’t overwrite the other stuff like `afterSave()`.  
And if somebody wants to add something to let’s say the `initialize()` function, he/she could just do it the same way as normal, using the `parent::initialize()` method.

I don’t know how feasible this is as I haven’t dived that deep into CakePHP yet, but I think this could be done.

EDIT: I’ll actually take this idea to the git repo to start a discussion about this over there

---

_[View the full topic](https://discourse.cakephp.org/t/using-bake-without-losing-custom-methods/4392)._
