We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Improved model layout

I initially posted this as an issue on github, but the forum might get better exposure/discussion.

I think we can all agree that model building with dev tools has some problems. Mainly losing comments and changes to setters and getters.

I would like to propose a better structure, which has actually worked for me very well for the last few months. This is based on what symfony 1.4 does.

Every model consists of two classes (using User model as example):

  • "RawUser"
  • "User" which extends "RawUser"

Both are generated by the dev tools, but only RawUser is overwritten when the --force parameter is used. This way you are free to make any changes to setters / getters etc in the "Model" class without having to worry about your code being lost. It also helps de-clutter the Model class, since it doesn't contain any variable declarations or setters.

You can test it here: https://github.com/nazwa/phalcon-devtools

I would love to hear what others think about this approach.

Edit Two thing to consider:

  • Name of the 'base' class. Is 'RawXX' a good choice? If i remember correctly I had a conflict with 'BaseXX', which is why I didn't use it.
  • Should the 'base' class be in the same folder or in a sub folder of the models dir. How can the autoload be improved to remove the extra variable. Maybe assume that the sub-directory is always called 'Raw' or 'Base' ?

Hi,

I totally agree with this solution, nice job and I hope it will be integrated into futur versions of devtools !

In my opinion 'base' class should be in a sub folder, because in this case we should never edit them, so we dont need to pollute our models list with it.

I'll try to use your repo till it's not possible to do it with official devtools, and i'll really need it to maintain and upgrade my model application easily :)

Best regards.



15.1k

I'm rally glad you like the idea. Let me know how things go and if you have any ideas on how to improve it.

Hey,

Sorry for answering so late but i was overwhelmed this time !

Anyway, i tried your fork and send you a PR because i had a bug : https://github.com/nazwa/phalcon-devtools/pull/1

It's working great now and it will totally save me a lot of time !!! It should definitely be implemented into phalcon-devtools !



15.1k

I'm really glad you like it! Thanks for the fix, seems like a recent merge from official gone mad.