cakephp update modified time on another table or model - php

Let's say that I have a Post table and an Update table. What I would like to know is how could I modify the 'modified' time field in the Post table when an update is added to the update table. I have searched - but can't seem to figure it out.
I hope this makes sense...
Thanks!

Yes you can do so, for example you could do this in the model. Implement a afterSave() method in the Update table. There you can update the related Post.
http://book.cakephp.org/2.0/en/models/callback-methods.html
Other way around would be removing modified in the Post model and creating an afterFind() method in the model. In most instances you should not do this because of data load but might be useful when using lots of inserts and a very low amount of reads.
The most nice way is to put those methods into a Behaviour which creates a separation of concerns and also allows re-use:
http://book.cakephp.org/2.0/en/models/behaviors.html
Addition while thinking about it: You could of course update the Post.modified field but it would be more clear and better to create a separate field for it. So Post.latestcomment datetime field. Then also edits to the post will be registered as an update. Also it allows more flexible scheduling. Next to that you know whether there are any posts also.

Related

Loading/saving PHP objects to MySQL database

I have an object for a „listing“, which has attributes like id, name, description, etc. which can be loaded via load($id) method.
It has some advanced attributes, too. For example multiple products (which is a separate object). To improve performance, I load them when you try to access them with listing->getProducts().
Now I want a save method in listing, to save the values back to the MySQL db. But I‘m not sure how to do that product saving in a clean way.
The best way would be adding a save method to products object and call it from the listing object, but that only saves the values. How do I handle added/removed products? That has to be done by the listing object I think. But how do I know if all products were removed or if they weren't even loaded (because they didn’t get accessed)?
All ways, like storing a boolean loaded products seem like a stupid solution. Do you have a good idea how to handle that? I‘m not inexperienced in coding but I want to improve my structure since I always end in a lot of code mess if projects get bigger.
I assume we are talking about adding, editing, and deleting products in your database. So you are probably going to need an 'insert' method and a 'delete' method (invoked from your list view). as well as the 'save' (update) method.
But the problem, or so I understand, is that you don't know which of these to use on a particular product line.
If you are just allowing the user to type willy nilly into input fields, so that you can't tell whether this is an edit, an insert, or a delete (or indeed which product you are editing or deleting, if you allow /every/ field to be changed) then you are making your life really complicated - is this necessary, from a business requirement/UI design perspective?
Consider something like listing the existing information read only and providing edit/insert/delete buttons.
I think I found a more or less good way on my own.
For the first problem (where to save what):
Storing of basic attributes of listing in the listing->save() method,
calling listing->products[]->save() to store products details,
syncing the listing<->product associations in listing->save().
Second problem (Deleting products, just because they didn't got load):
That was pretty easy, listing->products = null -> not loaded, listing->products = array() -> loaded, but empty. (I should got this earlier, haha)

To what extent is duplicate code OK in a model with CodeIgniter?

This is a long running question that gets me every time I am developing.
I suppose it is not specific to CodeIgniter, but as I am using it, I will consider it in my example.
Firstly, which is better:
function add_entry($data_array)
{
//code to add entry
}
function edit_entry($data_array)
{
//code to update entry
}
OR
function save_changes($what,$data_array)
{
//if what == update update
//otherwise insert
}
Both produce the same action, but does it really matter which one you use?
Getting onto more complicated things.
I have a page where I need to get ONE entry from the database.
I also have a page where I need to get all the entries from the same database ordered by a user specified column.
My resultant method is a function similar to
function($data_array,$order_by='',$limit='')
{
//get where $data_array
//if order_by!='' add order by
//if limit !='' add limit
}
As I develop my application and realise new places where I need 'similar' database functionality I am what feels like hacking previous methods so they work with all my case scenarios. The methods end up containing lots of conditional statements, and getting quite complex with in some cases 4 or 5 input parameters.
Have I missed the point? I don't want duplicate code, and when for the most part the functions are very similar I feel like this 'hacking' methodology works best.
Could someone advise?
Finally my admin functionality is part of the same application in an admin controller. I have an admin model which contains specific methods for admin db interaction. I however use some model functionality from 'user' models.
FOr example if on an admin page I need to get details of a db entry I may load the user model to access this function. There is nothing wrong/insecure about this..? right?
In addition to that within my admin model itself I need to get data about a user database entry so I call my user model directly from my admin model. This is strictly OK, but why? If i need data and there is already a method in my user model which gets it.. it seems a little pointless to rewrite the code in the admin model BUT each time that function is called does it load the whole user model again?
Thanks a lot all.
In order, add edit in the model vs save. Personally I have a save built in MY_Model that chooses whether it is a save or an edit depending on the existence of a primary key in the data being passed, so obviously I prefer that method it means a lot less duplication of code since I can use the save for any table without having functions in the model at all.
As to the second question I think it depends on situation. I also have a number of functions that have a ton of conditionals on them depending on where they're used and for what. In some cases I'm finding this makes the legibility of the code a little rough. If you're running them all through if statements it also could be impacting performance. DRY is a concept, not a rule and like other design concepts there are times when they just don't make sense, it's like database normalization, it's my personal opinion it's VERY easy to over normalize a database and destroy performance and usability.
Finally, using user functions in the admin code. I don't see an issue here at all, the reverse probably isn't true, but rewriting a function just because it's an "admin" function, when it's identical to a user function is utterly pointless. So you're correct there, it's a waste of time and space.

Detecting field changes - cakePHP

What I want
I want to see which fields on a table was changed and save that name into the database under the edit column.
What I have
Currently, not much. Just the standard cakePHP baked edit view and controller. I have done it previously, but not with cakePHP. What I did was retrieve the record, and if it's different to what the user entered, save the name of the column that was edited in the edit column corresponding to the row.
My Question
Could someone tell me how I would compare user input with what is on the database?
Behaviors like the "Logable" Behavior already do that and store the information separately.
I advice you to do the same. the "changes" do not necessarily need to be put into the same table. If you feel they do, though, you could make your own "modified" Logable behavior that only creates the "diff" and stores it into a field of your choice on the same record.
PS: You might also want to take a look at the RevisionBehavior.
It also contains some diff algorithm.
Then there is the WhoDidIt behavior which stores the user that last modified the record. In the same table, though. So this combined with the above should do the trick.
Either way:
use callbacks (beforeSave/afterSave) on model itself or (cleaner) as behavior
calculate diff
store the diff in a separate table or as in your case in an extra table field.
Actually writing something up here that does the job is pretty straight-forward.
The voluntary exercise here would be to write it more "generic". Maybe you want to reuse the same functionality again for other models in the future? Copy-and-paste would be pretty bad style then. The goal here would be to create some generic piece of code you can easily reuse. If your initial code works, try to rewrite it into a generic ChangesBehavior that you can attach to as many models you like. You can take the linked examples or take a look at other behaviors out there to get an idea how to do that.
Also you can publish your behavior in github/plugins.cakephp and give the community something back again. Maybe others find it useful, too.

DB Transaction in Controllers

I have created a crud system. Here I have a main model and then dependent details model. So their are multiple rows (some times > 100 ) entered into details model related to parent model. All operations handled through grid ( inline edit ). So create, update, delete operations are performed through single request (logical checking via DB).
Now I am considering to use DB transaction to whole operations. But I am confused, how I can implement this using same structure. I already have suggestions to move my all code to one model, So transaction can be applied there. But I am thinking, if any other approach can be used for retain separation of main and details model code.
Are you using AJAX for the changes or does it rely on manual form submissions?
You can use what's called a UnitOfWork pattern.
Save any changes that the user makes to each line of the grid but don't actually commit them to the DB. Then, place a geneic save button on the page that will cause your server to actually commit all the changes through a transaction.
You can keep a list server side of each row that the user changes. You don't need to track all the rows because if they don't change anything you don't need to save anything.
What sort of approach are you using in your Models? Do your models know about their own persistence (do you do things like $user->save()), or are you doing a more data-mapper approach ($userManager->save($userEntity))? The latter makes transaction handling a lot easier.
If you're doing the active-record type of patter ($user->save()), your best bet is probably just to manually grab your db connection and manage the transaction in your controller.
If you're doing data-mapper stuff, you have more options, up to and including doing a whole unit-of-work implementation.
As for my last comment, Moving code to parent model is my solution for now. So I think I should mark this thread answered.

Simple versioning (in PHP/Zend Framework)?

i am wondering how can i have simple versioning, say i want to be able to undo past edits (just up to 3 will do) to a post. maybe because the app allows other user to edit a post. but i may want the owner to restore the post for example.
do i just store the full post in another field?
One suggestion could be something like this:
you propably have each post as a separate row in the table and it has indexes that point to right threat and you retain their order by those indexes.
Add a "revision" to each post row. Everytime user edits the post, you store a new row into table but with higher revision. And when showing the posts, you just fetch the one with highest revision..
I've done this previously by creating a table which contains the following columns:
id, serialisation value of entire row you want backed up, the table it is for, datetime, why the backup was created.
That way you have a complete listing of all the versions of that table. I use Doctrine ORM so that way I can setup a postSave hook to create a new backup version in that table.
Doctrine Versionable Bebaviour may be the simplest solution for you:
Doctrine - Doctrine ORM for PHP - Versionable

Categories