r/PHPhelp • u/Alas93 • Oct 21 '25
Solved is this a good design?
edit: I wanted to thank everyone for their inputs. I see a few concepts I'm unfamiliar with so that's stuff I'm going to look into some more. And for those asking about seeing the data/code, I know that would be more helpful, but I'm refraining from sharing it all as a matter of there being some internal information in the datasets as well.
I know I didn't explain the whole thing super well, but even still, I'm grateful for the help you guys have given
tldr - post was a wall of text, with a bunch of unnecessary details that made it more confusing I think, so removed that wall. The short of it is, I made a program for my work for parsing part numbers for machines at my work, the part numbers are a pain, I ended up sorting things into a folder/file structure with JSON files at the bottom, and I can use snippets of the part number to create a directory to the json files to retrieve the correct data. Basically I created a kind of psuedo-database for this data. That said, I see some potential better solutions in this thread, so I'm going to look into those
4
u/TemporarySun314 Oct 21 '25 edited Oct 21 '25
Nested folders are nothing bad, as long as it stays reasonable (with modern project structure standards you probably get 3 levels deep minimum for your code). If you get 20 or more levels that's a bit excessive.
In general it's a good idea to use a database for storage, as this is just a highly optimized application to efficiently store and work with data. Better than everything you could write yourself, if you utilize the database correctly.
That being said, a few megabytes are not much data, and especially for something like static datasets, that don't change much and should just show a plain table or something, loading some json files is not necessarily wrong. You should think about then however how you can arrange the data in a way already, that PHP does not have to do much with it anymore. But as soon as you start to iterate through all data entries to find an entry which fulfill certain requirements or wanna sort it in PHP, a database is much better at this job.
In general it's also a good practice to split between your Data models, your views (that what getting shown to the end user), and the controller that handles the requests and returns data. There is the question how you implemented that in detail.
4
u/LostInCyberSpace-404 Oct 21 '25
There is no reason to not use a simple database for this. Given your description the db queries should be incredibly simple. Stop trying to overthink this and use the correct tech for the job.
1
u/supergnaw Oct 21 '25
I don't think this would be "bad practice" but rather a somewhat cumbersome codebase to maintain.
I'd be curious to see what this looked like, how you had any joins, etc.
This is where I would probably have approached skinning this problem differently. I couldn't tell you in what version they were added but MySQL has built-in JSON functions that allow you add, manipulate, and return the JSON data. If what you already have works, then it works, but I'm a big fan of databases for large datasets, which sounds like what you will have given you plan on having 10 or more megabytes of parts data.
Here's some questions for you to see if "this is a good design":
Does it work?
How hard would it be to add/modify/delete data?
Is your current dataset of parts normalized?
If it were me, I'd probably a main table with part numbers and generic columns shared across all parts, then:
At the end of the day, I try to do as much work up-front so maintaining the project in the future requires the barest minimum of effort. If your current design works for you, then it works for you.
3
u/wrybreadsf Oct 22 '25
So people helped you with some concepts and then you removed your original post so their help wouldn't help anyone else?
3
u/Own-Perspective4821 Oct 22 '25
Good that you removed your initial post, now all the answers don’t male sense for anyone else.
Very selfish, thanks for nothing.
2
u/[deleted] Oct 21 '25
That's not a TLDR.
2
u/VRStocks31 Oct 21 '25
Probably you should use SQL but the way you explained it doesn't allow me to understand perfectly how to structure it
1
u/sijmen4life Oct 21 '25
Without knowing much more than that you have a part number and want to get info from it a SQL database should work just fine. Maybe something like Laravel or the standalone Illuminate ORM would make your life easier.
To my knowledge its bad practice to dump all your info that should be in a database inside an array or to use a custom built "database". I'd really recommend to look into Normalization Microsoft has some good reading material on it. If you do want to stick with the directories system take a look at (IIRC) MongoDB. Do note I have no experience with Mongo nor have I read it's docs.
If you've got any other questions I'm more than happy to try and answer them.
1
u/birdspider Oct 21 '25
this may get interesting once you might access the data concurrently (2 or more processes)
use object probably, not only arrays
how it that different from
Model\Type\Parser::class? why do outside of php what can by done in-language ? i.e.if it's already json, you might want straight up unserialize into models (see some form of jsonmapper)
1
u/latro666 Oct 21 '25 edited Oct 21 '25
Why cant all of this be in database tables? I cant see why you cant have linking tables with foreign key references modelling all of this?
Have tou looked into relational database design, joins etc?
E.g. something like
SELECT m.model_code, p.part_number, p.voltage, p.power, a.key, a.value FROM machines m JOIN parts p ON p.machine_id = m.id LEFT JOIN attributes a ON a.part_id = p.id WHERE p.part_number = 'ABC123';
1
u/Big-Dragonfly-3700 Oct 21 '25
The code to insert, search, update, and delete data like this has already been designed, written, and tested. It is called a database engine.
At the point where you had a messy database design, should have been when you were posting your data structure, queries, and code to get help with them.