I am creating a kind of "Model" by which I'd like to have the ability to choose couple of rows (or simply one) through the constructor after which have the ability to treat them being an object so for instance, choosing all of the posts which has "admin" as author having a limit of 10 could be:
$posts = new Post(array( 'where' => array('author' => 'admin'), 'limit' => array(0 => 10), # optional: 'order' => array($by => 'desc/asc') )); # get a field $id = $posts->row['id']; # editing fields $posts->row['body'] = 'new body'; $posts->row['body'] = 'new body for post # 3'; $posts->update(); # delete all the selected posts $posts->delete(); ... et cetera.
But things i don't enjoy it the actual way it choose the posts. It appears too auto technician and filled with arrays in arrays which i don't enjoy. I additionally considered "One object being one row" but this could go ahead and take require of 10 posts perform 10 different queries. And that is not capable.
Question #1 It is possible to similar pattern or similar script that may inspire me? Shall We Be Held the first that believe that database rows might be viewed as PHP object (with the pros which come together with this idea).
Question #2 Might be there one other way of choosing multiple rows? Might be a means to fix pressure the developer to specify just the primary key of the table (like the publish id) as selector? (<= I do not think this can be a solution whatsoever).
Most ORMs (Object-Relational Mappers) attempt to give solutions for your question in one or two way.
Myself, I've lately released an ORM for PHP that is known as Solid Oxygen. Take a look in the documentation, if you would like. In my opinion that things i did is not far from that which you attempt to develop.
Yes, many people have believed of this before. You've got a plethora of PHP frameworks that handle database records as objects. You can have a look to Code Igniter, CakePHP or Symfony. That's an individual opinion however i believe that Code Igniter source code is simpler to know which it might give a good base to implement all of your object layer, should you still wish to implement one once you realize just how much an inconvenience it may be :)