NYCPHP Meetup

NYPHP.org

[nycphp-talk] DataObject implementation (was Re: Constructors and)

Brian Dailey bdailey at jaysec.com
Thu Oct 19 14:15:58 EDT 2006


Just as a side note, the Object->query function is pretty much exactly 
how CakePHP handles that question.

- Brian

Dell Sala wrote:
> This is where DataObjects can start to grow in complexity. What  
> you're doing here is creating a wrapper for your database, so that  
> each table maps to an class, and has a nice, clean interface. But how  
> do you keep your interface clean and still have access to the fancy  
> functionality of your database?
> 
> A simple solution to your example below: you might at an argument to  
> your find() method.
> 
> function find ($useLike = true) {
> 	...
> }
> 
> But you'll quickly come across more complex scenarios, and you will  
> either end up with too many methods or too many arguments for your  
> methods... I've heard Object Relational Mapping described as the  
> "Vietnam" of software development.
> 
> http://en.wikipedia.org/wiki/Object_Relational_Mapping
> 
> A more flexible solution is to have a query() method that allows you  
> to supply your own custom SQL. While this removes some of the  
> abstraction, it does give you lots of flexibility. And besides,  
> sometimes it is important to write your own optimized SQL for a  
> particular query. Here's an example:
> 
> $user = new DataObject('user');
> $user->query('SELECT * FROM user WHERE name LIKE '%john%' AND age <  
> 35');
> 
> The other possibility is to look into existing DataObject frameworks  
> that have already implemented the fancy stuff for you. I've used  
> PEAR's DB_DataObject. It's a bit of a pain to get up an running, but  
> it's solid. There are quite a few others -- here are a few:
> 
> http://pear.php.net/package/DB_DataObject/
> http://propel.phpdb.org/trac/
> and CakePHP implements its own ActiveRecord  http://cakephp.org/
> 
> 
> -- Dell
> 
> 
> 
> On Oct 19, 2006, at 12:42 PM, David Mintz wrote:
> 
>>> $user->insert();
>>>
>>> Here's a good article that covers the basics:
>>>
>>> http://www.onlamp.com/pub/a/php/2004/08/05/dataobjects.html
>>>
>> Quite good, thanks. Now, a question about
>> http://www.onlamp.com/pub/a/php/2004/08/05/dataobjects.html?page=3
>>
>> What if you want to find a record WHERE $object->someProp LIKE 'M 
>> %' (as
>> opposed to " = ")?
>>
>> The first crude thing that comes to mind is, if you want LIKE, say
>>
>> $user = 'Jane%'
>>
>> and have your find() method look for '%' and do LIKE if it's found.
>>
>> Thoughts?
> 
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
> 
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
> 
> 



More information about the talk mailing list