You are here: Re: Abstracting SQL Statements In An Object « PHP Programming Language « IT news, forums, messages
Re: Abstracting SQL Statements In An Object

Posted by Jerry Stuckle on 11/14/22 11:21

Scott Auge wrote:
> I am looking for comments on something that lets me abstract database
> updates in an object.
>
> Lemme explain what I am thinking:
>
> Lets say I have an object Person with...
>
> SetFirstName()
> SetLastName()
>
> etc.
>
> (Basically the goal is for the development team to use the objects and
> not try to write to the database themselves.)
>
> Well, what I am trying to avoid is when I call SetFirstName() and then
> call SetLastName() the execution of two UPDATE statements. I would like
> to combine it all into one single UPDATE or INSERT so I am not beating
> up on the database.
>
> I am thinking I would need to store the changes into variables local to
> the class and then maybe calling a SendSQL() function or something like
> that to generated SQL changes of the data to the database.
>
> Something like:
>
> $DB->query("BEGIN TRANSACTION");
>
> // Person fools around with $DB
> $Person->SetFirstName("Something");
> $Person->SetLastName ("Something");
> $Person->SendSQL();
>
> // Another object fooling around with $DB
> $AnotherOBj->SomeMethod ("Foo");
> $AnotherOBj->SendSQL();
>
> $DB->query ("COMMIT TRANSACTION");
>
>
>
> Anyhow - opinions and experiences are welcome!

Scott,

Generally, classes do not access the database unless specifically requested.
You might have four class members, for instance - fetch(), update(), insert()
and delete(). Each does its respective operations based on the data in the
current object (fetch() usually requires a key).

Another common class is the list class, which fetches an array of objects based
on select criteria.

But no, you do NOT want to update the database every time you issue a
GetFirstName() or SetFirstName(). The former needlessly requires access to the
database, and the latter may update the database when you don't want it updated
(i.e. change the name - but verify the change with the user before actually
updating the database).

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация