Re: PHP MySQL object question



William Gill wrote:
Jerry Stuckle wrote:
That's one way to do it, and I use it quite a bit for low level operations on the database. In more complicated applications I layer a business object on top of the database objects.
Not sure what you mean by "business object?"


Business objects are another OO layer on top of the database. They server to further decouple the application code from the data storage.

For instance, a business object may get information from several different tables, and when you update the data in the business object, it updates the underlying tables.


Don't try to store large objects in your session, though. Better to store id's in the session rather than the entire object.
Since I am only dealing with one composite record at a time (editing) I was planning to store the original field data for before and after comparison to determine when and where updates are needed. (12 - 20 fields, from 4 or 5 tables) I could do it as separate session variables, but since each table record will have an associated object, why not just store them that way? Or are you suggesting save the id in a session variable, then re-select the db record and compare fields (with incoming data) to see which need updating?



Just store the id and later just update the data. MySQL is smart enough not to update data which doesn't need to be changed. Or, if you don't have all of the data in the object, reselect the row, update the object then update the database. Unless your system is heavily used, chances are the original row will still be in the database's cache and retrieval will be very quick.

Of course, this is assuming no one else will be changing the data in the meantime. If they are, you have to save the old data (the session would work) and the second time around, fetch the data again. Compare the saved data with that in the object; if there is a difference you need to determine if it's important or not (i.e. if someone else updated column 'a' and you're updating column 'b', it's probably not a problem).

But just beware that when you save the object to the $_SESSION array, PHP has to serialize it, then unserialize it later. This can be a bit of overhead. And it can affect performance if your system is busy.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================

.



Relevant Pages

  • Re: Session or...?
    ... Typically you want to minimize the number of database trips, as well as minimize the amount of information you hold in memory. ... User Credentials, Shopping Cart Contents, Misc Preferences. ... The session would be a good place to store the ID and Qty. ...
    (microsoft.public.dotnet.general)
  • Re: PHP authentication for rookies
    ... >> username and password which you store however you will. ... >> using the php session functions. ... > Are there any advantages to using a database to store user credentials? ... Flat files don't scale or have the ability to do dynamic updates. ...
    (comp.lang.php)
  • Re: Caching often-used SQL queries
    ... The problem is that neither one of those are user (session) specific. ... need to enable sessions and store the data in the session context. ... Second - you'd be caching so much data that it would ... And if you tune your database server you'll spend much more time processing ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Session sharing between ASP and ASP.NET
    ... In my opinion, and though it may vary depending on the nature of your application, a database to store temporary information based on GUID or some other form of session key generated when the user hits the site and persists during the duration of their visit, would be easier than Yuen's example. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Session data and Control-N issue
    ... if a postback happens for an existing guid, you redirect to a new session. ... the request guid is an index into the session. ... Our application was using Session variables to store business object ...
    (microsoft.public.dotnet.framework.aspnet)