0

I'm creating a web app using PHP (Symfony2 framework) and I want to store some data that's created by the user, but I don't want to use a database. I think the best way to do this is JSON objects. Please look at my descriptions below and provide feedback on if this is a good way or bad way, or if it's even possible.

  • Front-end: user customizes the page, adding content and such (text, numbers, etc). Upon making any changes to the page, a front-end JS save function forms a JSON object, sends it to a php controller, then saves it into the user.json file.

  • Back-end: In addition to saving, the controller will scan the user.json file for any objects with the same id as the new one being saved. If it exists, it will delete that object, then add in the new updated object.

Q1 Is it best to delete the old object 12345 and re-add the updated 12345? Or would it be better to simply update the object somehow?

Q2 Is this way of storing data a good way and is it possible/easy in PHP?

Kenny
  • 2,124
  • 3
  • 33
  • 63
  • 2
    I wouldn't recommend using files for things that get a lot I/O. If you want something light weight that's somewhere in between "a real" database and simply writing to a file you can use [sqlite](https://www.sqlite.org/). – Andrei Jun 08 '16 at 15:00
  • Why don't you want to use a database for this? – ceejayoz Jun 08 '16 at 15:01
  • I don't want to use a database because the page will be pulling some client information, and I'm not quite authorized to use a db. – Kenny Jun 08 '16 at 15:02
  • I don't exactly need to store the information, it's only used for a short period of time. But I do want a copy of it in case I need to reference something I did in the past, for example. – Kenny Jun 08 '16 at 15:03
  • 1
    @Kenny What you're doing now(with the files I mean) is basically using a not-so-great database. If we're talking semantics here, what you're doing compared to what I'm suggesting is a stone throw away. However, if you're really hell bent on not using a database, then yeah, I suppose files is the way to go. – Andrei Jun 08 '16 at 15:04
  • @Andrew Yeah, I'd agree with you. For most normal projects, I'd totally use a db. But this is kind of a quick and dirty tool I'm creating, and in that tool I just want to store the data in case I need to recall it in the future. But I might just do away with the flat file storage, perhaps I can part the my desire to want a backup copy in case I need to reference. – Kenny Jun 08 '16 at 15:07
  • I might just use a cookie as a cheap way to keep track of the page content in case they accidentally refresh the page or something that would cause them to lose all the information they entered. – Kenny Jun 08 '16 at 15:08
  • If you just want to keep it for reference, why not just use logging and log out what's happened? – LBA Jun 08 '16 at 15:20
  • A log would have redundant information. For example, if the user changed something, it'd log that. Then they change something again, it'd log that. So the log would have multiple entries of the same object, each with just a new part. I'm looking for a single object without incomplete copies laying around. Great suggestion though, thanks. – Kenny Jun 08 '16 at 15:21
  • SQLite is a file and a DB. – cilefen Jun 08 '16 at 15:28
  • @cilefen can I simply download SQLite and toss it into my server files and run it? I thought it was a mobile device only option. – Kenny Jun 08 '16 at 15:52
  • Not at all. I use it for testing, for example. Related: http://stackoverflow.com/questions/23537549/symfony-doctrine-unittests-with-sqlite-memory-db – cilefen Jun 08 '16 at 17:02

1 Answers1

2
  1. You have to rewrite the file anyway, so it's not going to make much difference in terms of performance. From code point of views, if each time you get the full data from the client, it's probably easier to delete the object and re-create it.

  2. This is not really a good way to store your data. As @cilefen reported in the comments, if you want to use a file-based database, you may want to have a look at SQLite (https://www.sqlite.org/). Of course everything is possible but probably not worth it.

Francesco Abeni
  • 4,190
  • 1
  • 19
  • 30
  • Thanks, I'll look into SQLite. I'm not familiar with setting it up to sit within my php files, so I'll have to do some research. – Kenny Jun 08 '16 at 19:12