cakephp: Placing custom Model methods in separate places so Bake doesn't remove them

This is a (multiple allowed):

  • feature-discussion (RFC)

  • bug

  • enhancement

  • CakePHP Version: N/A

  • Platform and Target: N/A

What you did

Come up with a possible solution to this issue, which I now bring here for debate.

What happened

Please bear with me, this can be a bit of a read… While working on my project I kept coming by the same issue over and over while using the bake tool to automatically update models after I editted my MySQL schema in MySQL Workbench:

It keps overwriting my custom methods

Hoping CakePHP would have an easy solution for this, I went to the CakePHP forums and opened a question there to seek for help. This, however, led to the answer that what I wanted wasn’t easily doable. After thinking for a few moments, I came up with something in my head:

Why can’t we just put those custom methods in another file?

So, I replied on the thread with a possible new feature that could be implemented. Basically we put custom methods for events like afterSave(), beforeDelete(), beforeFind() etc. in a different location, like Model/Extension/UsersTableExtension.php. CakePHP would obviously load up the Model/Table/UsersTable.php and after doing that, it would try to see if UsersTableExtension.php exists (either throw an error if it doesn’t or leave it be) This file would then be loaded up and extend the UsersTable class like so (As far as I have read, chaining classes like this is possible, correct me if I’m wrong):

<?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
    }
  }

This way, bake won’t keep overwriting custom methods that we have cooked. Now, I understand that somebody here will throw in the “just build the models by hand” or “bake is not made for that purpose”, but do understand that this could potentially make bake a lot more useful (especially for newer CakePHP users) in the longer run. It would also potentially save time for developers like me (that use bake to generate the models) as we don’t have to keep re-adding the methods (or discarding the changes in the git client).

What you expect to happen

Discuss with me the feasability of this feature. Unfortunately, I haven’t dived deep enough in CakePHP and PHP programming in general (I do know my way around a bit, but am still learning a lot everyday) to build this feature into CakePHP myself and create a merge request. But I hope you guys give it a thought anyways.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

@jeremyharris using Git (or SVN if that’s your gig) will become a pain in the buttocks in bigger projects. manually using the checkout way with 1 table that gets re-baked? do-able. Now imagine the same… but with 10, 20 or even 50 tables? have fun with that .-.

@ADmad hm… I’ll tinker around with Traits tonight. I’ve read some more about PHP Traits, and if Bake can re-add those use statements, then it does about the same as my proposal.

I often just use git to restore my changes after doing another bake, would a workflow like that work?

Thats fine, baby steps 😃

For some starting points:

  • You can use the $tableObject here to add the used classes context.
  • Modify the twig template to inject those used traits. You can probably do it super nicely and split up the trait namespace and the trait name if you really want, so that the long-form goes at the top and you use the short name inside the class itself.