Author Topic: How to make a PHP/MySQL CRUD system  (Read 795 times)

0 Members and 1 Guest are viewing this topic.

Offline m0l0ko

  • Peasant
  • *
  • Posts: 129
  • Cookies: -4
    • View Profile
How to make a PHP/MySQL CRUD system
« on: July 04, 2014, 10:59:28 am »
I've been building them for a while now, and fell into many a pitfall, so learned from my mistakes. Firstly, I saw the need to store information about every page, along with information about all the form fields and how they should be dealt with. In a system like CakePHP, a lot of that is taken care of for you, but from experience, its still best to make your own page data system.

The first major pitfall I fell into was storing everything in arrays. Its alright for simple web page systems but it can get complicated fast, and you can end up with a monstrous multidimensional array which is a bastard to debug. XML files are probably a better approach, but since I'm most comfortable with dbs, I'm going that route. First I make a crud database, this will have tables with info on all the pages, subpages. For non static pages (i.e. ones where you display DB data and let users edit it), there'll be tables that controls all that (i.e. how many form fields need to be displayed, how they should be display and validated [for frameworks obviously, I'll just extract that from the models]. This kind of system is actually more useful for frameworks, because you can add rules for how differently routed pages should be processed and avoid all the ass pains I've had from working with CakePHP in an unconvetional way (I hack and tweat the crap out of everything and do a lot of unusual routiing). Another benefit is it will give you much easier control over complicated database relations. Rather than being limited by the models built in rules, you can make a table of relations which will provide the code necessary for the  connection to be made and processed properly. I'm kind of rambling on here, I have the idea in my head, I'll post more about it when I set up the system.

If anyone wants to help me out with this i'd greatly appreciate it. Any constructive criticism and suggestions are more than welcome.

Offline Schalla

  • VIP
  • Peasant
  • *
  • Posts: 81
  • Cookies: 29
    • View Profile
Re: How to make a PHP/MySQL CRUD system
« Reply #1 on: July 04, 2014, 12:12:42 pm »
Could you provide some sample data and what must be stored how to set which relations?

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: How to make a PHP/MySQL CRUD system
« Reply #2 on: July 04, 2014, 02:56:05 pm »
I actually use crud systems all the time nowadays.  That next to a good ORM system.

You can look at Eloquent http://laravel.com/docs/eloquent for inspiration or to use eloquent as a the ORM. And a crud system is basically some base crud class which works on specific routes thus /post/create and PUT/POST/DELETE/GET to /post/1. Laravel has this built in for you to extend: http://laravel.com/docs/controllers#resource-controllers (also good for inspiration).
~Factionwars

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline m0l0ko

  • Peasant
  • *
  • Posts: 129
  • Cookies: -4
    • View Profile
Re: How to make a PHP/MySQL CRUD system
« Reply #4 on: July 06, 2014, 03:09:41 pm »
Could you provide some sample data and what must be stored how to set which relations?

Plants, compounds, products, preparations and ailments is the data, they are all interrelated (i.e. a plant contains a compound which cures an ailment and happens to be one of the active ingredients of a product), they are all related to a big nested hierarchical category database, and some of the items (the receptors in the brain that the compounds target) are nested hierarchies themselves so its not so easy to do with CakePHP. I managed to get most of it working, but CakePHP is too bulky and slow for this kind of project.


I actually use crud systems all the time nowadays.  That next to a good ORM system.

You can look at Eloquent http://laravel.com/docs/eloquent for inspiration or to use eloquent as a the ORM. And a crud system is basically some base crud class which works on specific routes thus /post/create and PUT/POST/DELETE/GET to /post/1. Laravel has this built in for you to extend: http://laravel.com/docs/controllers#resource-controllers (also good for inspiration).
Cheers, I'll learn laravel. Cake is the first PHP framework I learned and while it has its benefits (the Hash class makes working with arrays a whole lot easier), I'd prefer to use a more light weight framework. However I need to get the job done as soon as I can but I've hit a dead end with cake, I'm getting this error:
Code: [Select]
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 168772 bytes) in /var/www/html/site/lib/Cake/Log/Engine/FileLog.php on line 134so I've obviously been doing something wrong. I started packing everything into a plugin so that I can easily migrate from server to server or site to site and I think I must have done something thats causing some kind of redundant process thats eating up all the memory.
« Last Edit: July 06, 2014, 03:15:22 pm by m0l0ko »