Doctrine State management - php

Is it possible to save the state of the database at given time in doctrine?
Why? well for example lets say I seed a database with Products table and Customer Table and then we have 1 Products record and 1 Customer record. Then I do some crud activity on the Products table that I now have 3 Products and changed the name of the original first Product. But now for the next test or time I want to reset to state back to where I was done seeding So then when I start doing the Customer testing I start with a Fresh database with out having to reseed the database?
Can I for example hook into the even system and see which products gets created and then fix the database after or some kind of back up and reset it towards that state?
Any advice will be appreciated
Quick note I dont have access towards PHPunit

Related

How to save the modifications of a column in sql

I'm coding a website where users can add concerts/events and other users can modify the informations provided for every concert/event added on the website.
I want to save every modification that has been made and the user that made the modification. For instance there is this functionnality on Wikipedia where every article has saves of every modification made and the user who made it.
For now, I just save the name of the user who made the last modification(s), and the modifications aren't saved: when a modification is made it will just overwrite the previous value of the corresponding column.
You can see under the current state of the tables I'm talking about. One for concerts with different informations: artist, date of the event, hour, links related to the event etc...
and another one for users.
Current sql tables
I think a first step would to create separate tables for every element of a concert that can be modified, with 2 foreign keys (one linked to user, one linked to concert) plus the value of the element. But still, if a modification is made I still don't know how to save the previous value and previous user.
I've thought of puting values inside an array but I don't know if this is possible in SQL and if this is the best solution.
Thanks for reading/helping
You should add a column that mark the create/update time to the concert table.
Then create a new history table (same schema of concert) for storing concert changes.
When a user edit a concert:
Save the current state of concert content to the history table.
Update the new change to the concert table with the user_id of the user who perform this action
That's it! When you want to list the changes history, just query the history table!
If you want to log the delete operations as well, add another column and mark the end period of each version (a little bit more complex)

Laravel - DB structure change - Is this a good practice or not?

Hello how are you? I wanted to ask you who have more experience than me if what I want to do is a good or bad practice in Laravel.
I am making an ordering app and in my database I have that a product has a certain price (one more field in the product table). But then I realized that when the price of the product one day changes, the price will also change in an old order, that is, the old order will adopt the new price of the product at this moment and not on the date when the order was placed, thus generating an information problem.
For that, I decided to create a new table that has the temporary prices, that is, if the price changes, that a new row is created in the database with the price on a certain date, but that the price it had in another is recorded. date.
Now, my question is ... How can I bring the current price of a product to that new table that I am creating, without the user intervening ... that is, migrate all the prices of each product to that new table.
My idea was to create a function that goes through each product and that creates a new row in the prices table with the product id, the price and the current date. Once this function is created, run it through Tinker and that the user does not notice absolutely nothing, but that the system adopts the new price structure.
This is good?
Is it a good practice or is there a better way to do it?
Thank you.
welcome to SO! This is one possible way to do it to have a table with the price history incl. start and end date of validity. To not overcomplicate the queries for showing the user his order history I would recommend you also to save the price of the product in the order. For example in the many-to-many mapping table between order and product, where you can also save the amount of the purchased products.
You could make a CLI command (e.g. php artisan make:command InsertOrderPrices) then to fill these fields for the old orders after you created them.

Database logic for online shop orders magament

I am designing as a project a web store (using PHP Laravel and MySQL FYI) and I am at the part where I have to create the logic behind the production system, which goes like this:
-On my Database,
I have 1 ORDER table where I have all the information regarding the shipping, customer, etc.
I have another table called ITEM where are listed all the Items in an order (so if an order has 3 items, there will be 3 lines in the ITEM table with a Foreign Key pointing to the ORDER).
Now I'm creating the PRODUCTION DASHBOARD. Right now I'm able to scan the item ID and get the shipment information on the Dashboard.
After that, for orders with multiple items what I want to do is for the system to tell the user to deposit the item in a numbered box to wait for the rest of the items from the order. That way the user can keep scanning items from other orders and once another item from the ordered stored in X box is produced, he can scan it and the system will then tell him that the other items from the same Order and placed on X box and he can do that until the order is complete.
My question is what would be the best way and logic Database wise (and also Laravel wise if you want to further expand your answer hehe) to implement this BOX system.
I hope my question is clear enough and thank you very much :)
I had a similar system for a project that I was working on. What I did was, was create a database table called temp_orders with a column called items that each item was separated by a line break. Until the order was finalized (100% processed), the order would remain in temp_orders.
Once finalized, it would get deleted from temp_orders and moved over to the orders table. If I needed to check items, I would explode() the data from the items column in temp_orders table using a line break, thus putting them into an array and then using the data however I needed to.
You need to determine when you want to finalize the order. It could be upon credit card payment, or upon user order confirmation, for example.

News feed using redis

I have the following table structure where I am storing activity of a user like 'product creation'
id
user_id
type_id
type
verb
data
created_at
updated_at
Whenever an event happens on a product like ( an order has come for the product), I need to update the updated time, so that the record appears in the first as the user will be seeing the 'sorted using updated_at' row data. The feed which I am referring here will be consumed by a single user and there is no concept of following here. So whoever created a feed will be seeing his own feeds. If there is an update (order placed by someone on that product) coming on the feed, it should go up.
So when an order is placed, I will update the updated time of the entry so that it appears in the first.
I am planning to use redis for the reads, but I am pretty confused on the update part. How will I handle this case.
What I have tried ?
Created the table structure as following
id
user_id - User who created the event
event - created_product
event_id - product_id of products table
data - json object of the product details
created_at
updated_at
When an order is received
The updated_at timestamp is updated. So that the record comes on the top of the user feed.
But this doesn't seem to be a proper solution as frequent updates can come for a row which can lead to row locking and more waits. How do I solve this?
Well, you could create sorted set in redis:
FEED:USER_ID containing values:
PRODUCT_ID - last update time as score
And then get products from database by ids which you got from redis.
But if your main concern is row locking, then maybe you should just queue updates? Create redis sorted set "PRODUCT_UPDATED" and add row whenever you are updating product:
USER_ID#PRODUCT_ID - update time as score
Then create some background CRON job which will update products found in that sorted set. You can do it every second, or every 30 seconds, you can throttle number of updates etc...

magic records being deleted

i have customised oscommerce to pull in a csv file of products, delete anything thats not with an image/proper description/proper title gets removed.
The import runs on a cron job basis pulling information from a supplier, it hasnt run since yesterday but a product has disappeared-
Anyone who has used oscommerce will know that, product information is stored over multiple tables. example is-
products
product_description
and so on. the thing that has got me that the information is deleted from the product table but not from the product_description table.
The product that is being deleted is a manually input one which carries a special tag/prefix on the model item of the product table. Therefore shouldn't be touched at all. Am clueles as what is going on.
Is there mysql integrity checks deleting records? could there be another plugin working on oscommerce?
If you can identify 'magic' rows in your table you could create a BEFORE DELETE trigger on that table to check the row someone is planning to delete isn't one of them, and raising an error if it is. I haven't created a trigger for quite a while so I forget the syntax, but there's more about triggers here... http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

Categories