1

I'm researching ways to create a OO-model/repository-layer using PHP's PDO. My idea was to create model classes that represent a domain object and a repository class for each model that has the responsibility of:

  • Saving to the database by taking a model as argument and mapping the objects properties to the VALUES of the SQL-query.
  • Reading from the database by return an instance of a model in a similar fashion.

I was inspired from a course in OOP in which we used .NET, EF and LINQ for the Labs.

I asked a question on SA about how this could be achived on SA. I got flamed because is is supposedly bad practise.

I don't see why this is the case.

2

1 Answer 1

2

There are already libraries that do what you want available for PHP. It's called O/RM or Object/Relational Mapping. Not being a PHP programmer myself, I'll relegate suggestions to the experts

Funny, that's not the first time I've seen people arguing against using objects to represent database records in dynamic Object-Oriented languages. I recently read a Python thread arguing to just access the data record directly. Frightening to say the least.

5
  • To be fair, ORM's are meant to be used for programming in the large. Sure, you can use them for small projects, but sometimes you just need a SQL statement, and you still need to know how to write a SQL statement even if you use an ORM. Mar 22, 2013 at 15:41
  • @RobertHarvey I agree wholeheartedly. But it appears that Gabriel wants to create a generic solution for converting query results into objects. Mar 22, 2013 at 15:50
  • As I said prevously I've done a bit of .NET-development. I have no problem with ASP.NET MVC and EF. But their counterparts in PHP (like Symfony and Doctrine) seem very complicated in comparision. And the more agile frameworks like laravel and codeginiter really seem to come and go over the years, and fast too. Mar 22, 2013 at 16:11
  • @GabrielSmoljár - Codeigniter, CakePHP, Symfony, and Zend have been around since circa 2006 and have large communities backing them. They're not going anywhere any time soon. They also have a large selection of ORM choices that have been around nearly as long. (For perspective, the mentioned frameworks all precede Entity Framework and LINQ.)
    – Shauna
    Mar 22, 2013 at 17:08
  • Apple's CoreData seems to do exactly that. (In a slightly clever way. You can decide what to use for database storage. You can create objects that don't read from the database until the data is needed. Especially good for data with references to other tables).
    – gnasher729
    Oct 26, 2017 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.