NYCPHP Meetup

NYPHP.org

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

Dell Sala dell at sala.ca
Thu Oct 19 13:37:47 EDT 2006


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?




More information about the talk mailing list