How can I implement model modifications in Laravel?

This question for my pastebin application is written in PHP.

I learned a little, although I could not find a solution that fits my needs. I have a table with this structure:

+-----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+------------------+------+-----+---------+----------------+ | id | int(12) unsigned | NO | PRI | NULL | auto_increment | | author | varchar(50) | YES | | | | | authorid | int(12) unsigned | YES | | NULL | | | project | varchar(50) | YES | | | | | timestamp | int(11) unsigned | NO | | NULL | | | expire | int(11) unsigned | NO | | NULL | | | title | varchar(25) | YES | | | | | data | longtext | NO | | NULL | | | language | varchar(50) | NO | | php | | | password | varchar(60) | NO | | NULL | | | salt | varchar(5) | NO | | NULL | | | private | tinyint(1) | NO | | 0 | | | hash | varchar(12) | NO | | NULL | | | ip | varchar(50) | NO | | NULL | | | urlkey | varchar(8) | YES | MUL | | | | hits | int(11) | NO | | 0 | | +-----------+------------------+------+-----+---------+----------------+ 

This is for pastebine application. Basically I want the paste changes so that if you open paste # 1234, it will show all previous versions of this paste.

I thought of three ways:

Method 1

There is a revision table with id and old_id or something else, and for each ID I would insert all the old revisions, so if my structure looks like this:

 rev3: 1234 rev2: 1233 rev1: 1232 

The table will show the following data:

 +-------+----------+ | id | old_id | +-------+----------+ | 1234 | 1233 | | 1234 | 1232 | | 1233 | 1232 | +-------+----------+ 

The problem I am facing is that it enters a lot of duplicate data. And the more changes, the more data, but I need to make N inserts for each new paste in the revision table, which is not very good for large N.

Method 2

I can add child_id to the insert table at the top and just update this. And then, extracting the paste, I will continue to request db for each child_id and their child_id, etc. But the problem is that too many DB reads will be introduced every time a paste with many revisions is opened.

Method 3

It also includes a separate revision table, but for the same scenario as method 1, it will store the data as follows:

 +-------+-----------------+ | id | old_id | +-------+-----------------+ | 1234 | 1233,1232 | | 1233 | 1232 | +-------+-----------------+ 

And when someone opens paste 1234, I will use the IN clause to get all the data of the child insert.

What is the best approach? Or is there a better approach? I am using Laravel 4 framework with Eloquent ORM.

EDIT . Can I make method 1 with oneToMany relationships? I understand that I can use

+4
source share
3 answers

So here is what I do:

Say this is a stream of changes:

 1232 -> 1233 -> 1234 1232 -> 1235 

So here is what my revision table will look like:

 +----+--------+--------+ | id | new_id | old_id | +----+--------+--------+ | 1 | 1233 | 1232 | | 2 | 1234 | 1233 | | 3 | 1234 | 1232 | | 4 | 1235 | 1232 | +----+--------+--------+ 

Identifiers 2 and 3 show that when opening 1234 it should display both 1233 and 1232 as revisions in the list.

Now a bit of implementation: I will have a Paste model with a one-to-many relationship with the Revision model.

  • When I create a new revision for an existing paste, I will start a batch insert to add not only the current new_id and old_id pair, but also the current new_id pair with all the changes that were associated with old_id.
  • When I open the insert that I will do with the new_id query, I essentially get all the related rows in the revision table (using the function in the Paste model that defines hasMany ('Revision', 'new_id')) and will be displayed to the user.

I also think about showing the author of each revision in the Change History section of the Insert View page, so I think I will also add the author column to the revision table so that I don’t need to go back and query the main insert table so that get the author.

So what about that!

+2
source

If you're on Laravel 4, try Revisionable . It can satisfy your needs.

+8
source

There are some great packages to help you keep the model modifications:

  • If you want to save model modifications, you can use:

Revisionable

  • If you also want to register any other actions when you want, using user data, you can use:

Laravel activity logger

Honorable mentions:

+1
source

Source: https://habr.com/ru/post/1499537/


All Articles