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 Scott Auge on 07/30/05 18:49

In article <slrndembnk.4ej.richard@localhost.localdomain>,
richard <richard@localhost.localdomain> wrote:

> On 2005-07-15, Scott Auge <scott_auge@yahoo.com> wrote:
> >
> > I am looking for comments on something that lets me abstract database
> > updates in an object.
>
> > 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");
>
>
> I work for a company called zedcore.com, and we code php for money. We
> have solved this issue. I'll explain what we have done.
>
> For sql we use arrays rather than objects which are then acted upon by
> a database object.
>
> I'ts probably easiest if I give you some examples.
>
> $oRst=new Recordset('RstUsers',DATASOURCE);
>
> $oRst->SetDefinitionFromDatasource(array('Users'=>''));
>
> //All fields from Users table added to a recordset.
>
> Example 1 - Basic select
> ------------------------
>
> $aSQL=array('Command'=>'Select');
> $oRst->ExecuteSQLArr($aSQL);
>
> Example 2 - Add a where
> -----------------------
>
> $aSQL=array();
> $aSQL['Command']='Select';
> $aSQL['Where']="username='bob'";
>
> $oRst->ExecuteSQLArr($aSQL);
>
>
> Example3 - More than one where
> --------------------------------
>
> $aSQL=array();
> $aSQL['Where']=array();
> $aSQL['Where'][]="username='bob'";
> $aSQL['Where'][]="accountactive='t'";
>
>
> Example4 - ordering
> --------------------
>
> $aSQL=array();
> $aSQL['Command']='Select';
> $aSQL['OrderBy']='UserName';
>
> Example5 - Offset and Limit
> ---------------------------
> $aSQL=array();
> $aSQL['Command']='Select';
> $aSQL['Limit']=2;
> $aSQL['Offset']=10;
>
> Now when we have a recordset we can put tags into our html to refer to
> the recordset. Guess what this does:-
>
> <$TEST.RST.RstTest.ANYRECORDS>
> <table>
> <tr><td><$RST.RstTest.CAPTION.username></td></tr>
> <$ITERATE.RST.RstTest>
> <tr><td><$RST.RstTest.FIELD.username></td></tr>
> <$/ITERATE>
> </table>
> <$TEST.ELSE>
> <p> No records</p>
> <$/TEST>
>
>
> But we do more as well. Guess what this does.
>
> <RST.RstTest.PAGER>
>
> <$TEST.RST.RstTest.ANYRECORDS>
> <table>
> <$RST.RstTest.ALLCAPTIONSORTER>
> <$ITERATE.RST.RstTest>
> <tr><$RST.RstTest.ALLFIELDS></tr>
> <$/ITERATE>
> </table>
> <$TEST.ELSE>
> <p> No records</p>
> <$/TEST>
>
> It does the same as the first template example unless you populate the
> recordset with
>
> $oRst->SetSQLArr(array('Command'=>'Select','Limit'=>2));
> $oRst->SetSorterPagerFromURL();
> $oRst->Execute();
>
> To be clear, it allows us to add table headers which will sort the
> data and then move to the correct 'page'.
>
> Neat huh?
>
> We are going to gpl our code in the future, but we are busy with paying
> work, so it may take some time. I have had a look at all the other
> application frameworks for PHP, and excepting ezPublish which I was
> half asleep when I was checking I can say our code is superior. Although
> in places, less polished. Which is another reason why it has not been
> opensourced yet.
>
> zedcore.com will have the news when it comes...
>
> Hope some of this helps! Integration of template engines to a source of
> data directly is so convienient..


That is certainly a way to do it and peaks my interest...

But the whole idea was to abstract away from the developer the need to
know the database relations, field names, etc.

I want them to know the objects to perform a certain piece of
functionality. The people who maintain the database (a more advanced
group of developers) will maintain these objects so the publicly
available attributes and methods are the same or added to.

If I hire someone, I can make them more productive faster by saying "If
you want to create a sales order, use the object SalesOrder, "call this
method/fill these attributes with" with these values and then call
Create(). In short, it is easier to learn three lines of code than it
is to craft an SQL statement with knowledge of the database (and it's
denormalized quirks), etc.

I have been getting some ideas rolling around in my head and will make
them public when they are better formed.




--
Available for Hire! http://amduus.com/Resumes/

 

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

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