Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Best DB Abstraction Layer to use with Smarty
Goto page Previous  1, 2
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Thu May 15, 2003 9:45 pm    Post subject: Reply with quote

For comparison sake, here is an example of a Model method retrieving data. Actually reasonably complicated because it uses bind variables as well:

Code:
/**
 *  retrieve daily catalog history
 *  @return  array
 */
function GetCatlgDailyHist($psCatlg, $psPlant, $piDays=91)
{
  $o_conn = amp_adodb_connection('prdqry');
  $a_bind = array('CATLG' => $psCatlg, 'PLANT' => $psPlant, 'DAYS' => $piDays);
  $o_rs = $o_conn->Execute(PLANT_CATLG_HIST_DAY_SQL, $a_bind);
  if ($o_rs && !$o_rs->EOF) {
    return $o_rs->GetArray();
  } else {
    trigger_error(DB_OOPS);
    return false;
  }
}


I tend to code the SQL statements themselves as constants before the class definition.

By "MVC mindset" I mean at least structuring you code so that data/file access and other business logic is handled in on portion of your code (Models). Application flow and security by another (Controller), and finally presentation logic in a final portion of your code (Views).

I have settled down on using ADOdb for db abstraction, Phrame for a base MVC framework and Smarty for HTML rendering in views. Once you get used to how the framework works, most of the code you write is in small tight blocks, and maintenance has not been an issue.

I have written a two part series documenting this setup for PHP|Architect. Part 1 was the lead story for the May issues, Part 2 will be coming out in June.
_________________
Jason
jsweat_php AT yahoo DOT com
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri May 16, 2003 2:56 am    Post subject: Reply with quote

I think that the crucial difference in sweatje's approach as compared to toma's is the idea of an abstracted data layer as opposed to the abstraction of the db handling code (the driver code).

In toma's approach, you still code your application logic directly against the database -- except that instead of using native db calls you are using abstracted calls. The principle behind sweatje's approach is that your application logic doesn't directly call the db handlers whether abstracted or direct. Instead your application calls "business" objects in the data layer that handle the necessary data retrieval. This provides a much higher degree of code separation and makes things like migration and scaling far simpler.

I prefer the latter approach. It means that the "extended functionality" (like associative keys, etc) can be removed from the db abstraction layer and put into your data abstraction layer as can your error handlers, etc.

I'm not convinced that an MVC approach is necessary for web apps, but I do like the code separation. Off-topic, that was a nice article in php|a, sweatje. I'm looking forward to the conclusion.
Back to top
View user's profile Send private message
Justin
Smarty Regular


Joined: 07 May 2003
Posts: 38
Location: Vilnius, Lithuania

PostPosted: Fri May 16, 2003 4:22 am    Post subject: Reply with quote

Well, I use my own DB abstraction class

* connect( array $config )
* disconnect()
* getAll( string $sql [, array $params] )
* getRow( string $sql )
* getOne( string $sql [, string $fieldName] )
* getCol( string $sql )
* insert( string $table, array $array )
* replace( string $table, array $array )
* update( string $table, array $array [, string $condition] )
* delete( string $table [, string $condition] )
* countQuery( [string $table [, string $extraParams]] );
* affectedRows()
* lastID()

.. and I'm realy happy Smile It works fast, It's small..
_________________
http://www.baubas.net
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Fri May 16, 2003 8:28 am    Post subject: Reply with quote

Justin wrote:
Well, I use my own DB abstraction class

* connect( array $config )
* disconnect()
* getAll( string $sql [, array $params] )
* getRow( string $sql )
* getOne( string $sql [, string $fieldName] )
* getCol( string $sql )
* insert( string $table, array $array )
* replace( string $table, array $array )
* update( string $table, array $array [, string $condition] )
* delete( string $table [, string $condition] )
* countQuery( [string $table [, string $extraParams]] );
* affectedRows()
* lastID()

.. and I'm realy happy Smile It works fast, It's small..


Me too. And my abstraction layer looks quite the same Very Happy
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Fri May 16, 2003 1:36 pm    Post subject: Reply with quote

in my opinion, web development isn't the very best field of operation for the MVC model... I just don't like it
for PHP applications, it's mostly enough to cleanly seperate your presentation and application logic along with a good structure and well designed classes etc...
Back to top
View user's profile Send private message
Justin
Smarty Regular


Joined: 07 May 2003
Posts: 38
Location: Vilnius, Lithuania

PostPosted: Fri May 16, 2003 1:39 pm    Post subject: Reply with quote

andre wrote:

Me too. And my abstraction layer looks quite the same Very Happy


huh Very Happy I've implemented this from scratch Smile
_________________
http://www.baubas.net
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon May 19, 2003 6:48 am    Post subject: Reply with quote

Justin wrote:
andre wrote:

Me too. And my abstraction layer looks quite the same Very Happy


huh Very Happy I've implemented this from scratch Smile


Me too Laughing Laughing Laughing
Back to top
View user's profile Send private message
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Tue May 20, 2003 9:55 pm    Post subject: Reply with quote

boots wrote:
@Toma: Well, Pear is still a mess on Windows.

I've read somewhere(
http://pear.php.net/weeklynews.php/en/20030413.html) that PEAR-Integration with Windows will be much better with PHP version 4.3.2!

Perhaps you should have a look again when it is released Smile
________
Dodge Venom specifications

Last edited by andreas on Fri Feb 04, 2011 9:01 am; edited 2 times in total
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue May 20, 2003 10:01 pm    Post subject: Reply with quote

Quote:
Perhaps you should have a look again when it is released


I certainly intend to check it out. On the other hand, I've been "waiting" for Pear to work on windows for a long time. To be fair, I stopped waiting awhile ago, but I still check in on the project now and then. I really do want it to be better, but as things stand, I doesn't look like it will ever be a big part of my trick-bag.

I suspect Pear will get "good" around PHP 5.1 or somewhere after that.

Still, I'd like to see more examples of solutions that use Smarty in conjunction with Pear projects.
Back to top
View user's profile Send private message
koide
Smarty Rookie


Joined: 06 May 2003
Posts: 5

PostPosted: Wed May 21, 2003 1:21 am    Post subject: What's the big problem about MVC + Web? Reply with quote

Wom.bat wrote:
in my opinion, web development isn't the very best field of operation for the MVC model... I just don't like it
for PHP applications


I agree that it was thought for 'Desktop' GUI's and there is where it really shines, but it's very nice to help structuring your code and concern separations. I just started developing a Eocene+Smarty+ADODb app and it's amazing the ease of development and maintenance compared to what i used to do in only Smarty+ADODb. The performance impact is not too big, and it really _really_ helps to implement a well-thought design. I agree you can perfectly do this without the help of actually implementing (or using an existing) MVC framework, but isn't this all about ease of development, easy maintenance and quick deployment times? Remember you can still program in Assembly and get the same functionality.

Of course you can argue that I didn't (or don't) know how to "cleanly seperate your presentation and application logic along with a good structure and well designed classes etc...", but my point is exactly that, now I can do it easily.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 21, 2003 1:33 am    Post subject: Reply with quote

Quote:
I agree you can perfectly do this without the help of actually implementing (or using an existing) MVC framework, but isn't this all about ease of development, easy maintenance and quick deployment times? Remember you can still program in Assembly and get the same functionality.


The problem with using MVC is two-fold:

1) It requires extensive use of language features that PHP simply does not support very well. PHP5 will be a much better match for class intensive frameworks.

2) MVC solves a problem (fairly elegantly) that simply does not exist for nearly every type of current web-based application: the co-ordination of data among disperate items in an interactive environment. (The key here is the "interactive")

Further, unless you actually have experience developing with an object framework as abstracted as MVC, you are likely going to make some critical implementation errors. Even if you are good at it, it is not unreasonable that your class hierarchies become increasingly obtuse leading to far more difficult maintenance. Using existing frameworks helps, but sometimes they are just more rope to hang yourself with.

Not to say that MVC isn't useful or can't lead to pleasant designs. Its just that for web apps, much simpler frameworks can achieve the same end results while being faster and easier to maintain. The point here is that if you use overkill on a problem, your solution will be much more complicated (and therefore more brittle) than it need be.

Also, I'm not saying that MVC's approach is incorrect--its all about the layers. I am saying that for web apps, MVC is likely too much of a good thing.


Last edited by boots on Wed May 21, 2003 1:51 am; edited 1 time in total
Back to top
View user's profile Send private message
koide
Smarty Rookie


Joined: 06 May 2003
Posts: 5

PostPosted: Wed May 21, 2003 1:51 am    Post subject: MVC & web Reply with quote

boots wrote:

1) It requires extensive use of language features that PHP simply does not support very well. PHP5 will be a much better match for class intensive frameworks.


I agree, but with PHP 4.2 and upwards it's _doable_, not easy, but you can at least make it work. And that's a problem with PHP, not with the model.

boots wrote:

Using existing frameworks helps, but sometimes they are just more rope to hang yourself with.


Yes, in fact I have tried Phrame, phpMVC, a few other I don't remember and finally I stumbled upon Eocene. It's lightweight, easy to work with, easy to modify to your needs (I adapted Smarty and ADODb to it in less than a day of work), and easy to understand. You should check it out if you still haven't.

boots wrote:

Not to say that MVC isn't useful or can't lead to pleasant designs. Its just that for web apps, much simpler frameworks can achieve the same end results while being faster and easier to maintain.


As I said above, I've looked at a lot of frameworks to do my coding with, and still have to find a great one. Eocene is the closest to great I've found so far. It's based on MVC, but that's not a reason by itself not to use it. I agree with the cons for MVC and web, but do you have a better alternative? Please share!
Back to top
View user's profile Send private message
eadz
Smarty Regular


Joined: 30 Apr 2003
Posts: 61
Location: Auckland, New Zealand

PostPosted: Wed May 21, 2003 3:24 am    Post subject: Reply with quote

ez_sql.php - it's lightweight, free, fast, and perfect for smarty Smile
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 21, 2003 5:51 pm    Post subject: Reply with quote

@eadz: I looked at ez and it is cute. Small and does very little (which I like) but does offer some convenience functions.

Not extremely flexible and since it is driverless, it looks like you can't have access multiple db types without extending the classes (since every db type uses the same class name). I'm not sure I'd use this long-term, but I do like its output styles Smile Has anyone patched this to be Smarty aware and use templates internally instead of embedded HTML?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP