Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Paul Jones:
Atlas: a persistence-model data mapper
Dec 30, 2015 @ 09:48:50

Paul Jones has a new post to his site about a library he's worked up to provide "persistence-model data mapper" functionality for you to use in your PHP applications in accessing your database.

Atlas is a data mapper implementation for your persistence model (not your domain model).

As such, Atlas uses the term "record" to indicate that its objects are not domain entities. Note that an Atlas record is a passive record, not an active record; it is disconnected from the database. Use Atlas records indirectly to populate your domain entities, or directly for simple data source interactions.

The library is mostly an experiment on his part to create a tool that allows switching from the Active Record pattern to Data Mapper pattern for accessing your database without much hassle. The README on the library shows some of the basic usage of the tool, including the usual CRUD (create, read, write, execute) functionality.

tagged: atlas persistence model datamapper activerecord designpattern database library

Link: http://paul-m-jones.com/archives/6210

Jon LeMaitre:
Separation of Concerns with Laravel’s Eloquent (Series)
Nov 10, 2015 @ 09:10:47

Jon LeMaitre has written up a series of posts talking about effective separation of concerns using Eloquent, the database access component of the Laravel framework. His goal is to show you how makes use of the library and keep your application both maintainable and readable.

Eloquent (and by extension, Laravel) is often criticized because of its ActiveRecord ORM implementation, and that DataMapper produces a cleaner, more maintainable architecture in the long run. [...] This combination of behaviors crammed into a single object in itself is not the problem (in fact, it’s a wonderful asset), the problem is in how you make use of those behaviors throughout your application. You can be sloppy with them, or you can be tidy with them.

He starts with the "sloppy approach" where different methods of accessing the objects (and their data) are spread all across the application. He counters this with the tidy approach that makes use of "persistence-agnostic 'POPOs'" to hide the logic behind repositories. In the second part of the series he starts in and puts this idea into practice, using a simple "Member" table and model and repository classes to abstract the logic. Finally, in part three he gets into some more extended functionality for the repositories approach: handling collections, relations, eager loading and schema changes.

tagged: laravel eloquent designpattern repository orm datamapper activerecord abstraction tutorial series

Link: https://medium.com/laravel-news/separation-of-concerns-with-laravel-s-eloquent-part-1-an-introduction-c9a6dc6b4a65#.o0qsccqos

SitePoint PHP Blog:
Yii 2.0 ActiveRecord Explained
Nov 20, 2014 @ 09:08:31

The SitePoint PHP blog has a new tutorial posted introducing you to using ActiveRecord in the Yii2 framework to access the information in your databases. The Active Record design pattern where a single object corresponds to a record in the database (and can be manipulated as such).

The ActiveRecord class in Yii provides an object oriented interface (aka ORM) for accessing database stored data. Similar structures can be found in most modern frameworks like Laravel, CodeIgniter, Symfony and Ruby. Today, we’ll go over the implementation in Yii 2.0 and I’ll show you some of the more advanced features of it.

He introduces the "Model" class first, the based of the ActiveRecord handling, and its parts: attributes, validation and scenarios. He then gets into the creation of the a model instance based off of a table (SQL structure provided) around authors and articles. He includes the code showing how to create a simple model, add in relations and putting it to use. He also shows how to use the built in "find" handling to locate records. Finally he gets into some of the more advanced topics including checking if attributes are "dirty", the "arrayable" functionality and using events/behaviors/transactions on the models.

tagged: yii2 framework activerecord tutorial introduction

Link: http://www.sitepoint.com/yii-2-0-activerecord-explained/

Code Reuse By Inheritance
Jan 20, 2014 @ 10:55:18

On the Qafoo blog today Kore Nordmann has a new post talking about code reuse through inheritance. He talks about base classes, sharing code and abstraction.

To me, inheritance has two properties: Defining an is-a relationship [and] making it possible to share code between classes by extending from a common base class. The is-a relationship between classes is one thing I like to use inheritance for. [...] The other thing you can use inheritance for is to share code between multiple classes. [...] I personally think that the latter is one of the worst things you can do in object oriented design. Bear with me for a moment and let me try to explain why I think that.

His example of doing it the wrong way is using the Active Record design pattern and how it's usually implemented - storage logic in the base class and business/table logic in the extending class. He then gets into an example that's a bit "smaller" creating diff display functionality and how the "code reuse by inheritance" creeps in a lot in helper methods. He also briefly looks at testing (or not testing) private methods and and the "Depth of Inheritance Tree" metric's recommended value.

tagged: code reuse inheritance helper activerecord testing depth

Link: http://qafoo.com/blog/063_code_reuse_by_inheritance.html

SitePoint PHP Blog:
Yii Routing, Active Record and Caching
Nov 07, 2013 @ 09:37:31

On the SitePoint PHP blog today Sandeep Panda has a new article looking at routing, using Active Record and caching with the Yii Framework. The Yii framework is a " a high-performance PHP framework best for developing Web 2.0 applications".

Almost all modern web apps have 3 major concerns: Retrieving data from a database easily and effectively, caching the web content and URL rewriting to create user friendly URLs. Yii, like any other good framework, offers simple and easy solutions to all of the above. [...] In this tutorial we will look at how Yii greatly simplifies the development of database driven websites with its Active Record support. Additionally, Yii lets you further improve your websites by implementing user friendly URLs and powerful caching techniques.

He creates a simple application based on the Yii framework skeleton called "gadgetstore" that works with fictional phone data. He shows how to set up a few sample routes and adding the "phone" table to the database. He then uses the framework's tools to auto-generate the model and shows the save/delete operations. He then creates the "Phone" controller and an add action to handle the create submissions. Finally, he gets into caching the response data and uses the built-in "cache" function for the models to store the results of a "findAll" request.

tagged: yii framework caching activerecord routing

Link: http://www.sitepoint.com/yii-routing-active-record-caching/

Russell Walker:
Active Record vs Data Mapper for Persistence
Oct 18, 2013 @ 10:19:13

Russell Walker has a new post today comparing two popular methods for abstracting out database access and working with your data - the Active Record and Data Mapper patterns for data persistence.

These two design patterns are explained in Martin Fowler's book 'Patterns of Enterprise Application Architecture', and represent ways of handling data persistence in object oriented programming.

He gives simple code examples of both - one showing a basic "save" call with Active Record and the other showing the saving of a "Foo" entity using similar logic. Along with these examples, he also includes a few points about the major advantages and disadvantages related to the pattern. He also talks some about "service objects", the go-between that the data mapper pattern uses to combine business logic and the mapper object. He ends the post by making some suggestions about which to use, depending on the need of course.

tagged: activerecord datamapper persistence database interface designpattern

Link: http://russellscottwalker.blogspot.co.uk/2013/10/active-record-vs-data-mapper.html

Simon Holywell:
Idiorm and Paris 1.3.0 released - the minimalist ORM and fluent query builder for PH
Feb 27, 2013 @ 10:33:33

Simon Holywell has a new post to his site about a project that aims to be a minimalist ORM library and make it easier to built queries on the fly for your applications (and is installable via Composer) - the Idorm + Paris combination.

Idiorm is a PHP ORM that eschews complexity and deliberately remains lightweight with support for PHP5.2+. [...] However, having said this, Idiorm is very powerful and it makes most of the queries PHP applications require pain free. Some of these features include fluent query building, multiple connection support and result sets for easy record manipulation. Paris sits on top of Idiorm to provide a simplified active record implementation based upon the same minimalist philosophy.

He includes examples in the post of both queries with Idiorm - simple things like creating and finding records - and using Paris to make models out of PHP objects. He also talks some about the current state of the project, recent advancements and some of the things they're looking to do with it in the future (including dropping PHP 5.2 support and use late static binding).

tagged: library project orm idiorm paris activerecord simplicity


Zend Developer Zone:
Design Patterns I Hate
May 18, 2011 @ 10:18:21

On the Zend Developer Zone today there's a new post from Keith Casey about some of the design patterns he hates - ones that are misused too often and have become more of a "worst practice" at times than others.

To be clear, I think Design Patterns have their place. When used properly, they can make our lives easier and our code better. When they're misused - "If the only tool you have is a hammer.." - then they can become the bane of our existence. Unfortunately, some Design Patterns seem more susceptible to this than others. So maybe this is less "design patterns I hate" and more of "design patterns that have been abused beyond all sense of reason"... I'll let you decide.

He has three of the most commonly misused design patterns in his list. Anyone who has looked into using patterns in their applications will recognize them:

  • Active Record
  • Factory
  • Singleton

For each, he describes some of the bad implementations he's seen and notes that, while these are commonly abused, they can still be redeemed by being used appropriately.

tagged: designpattern opinion misuse activerecord factory singleton


Zend Developer Zone:
New PHP5.3+ Framework: Jolt
Aug 13, 2010 @ 10:50:05

On the Zend Developer Zone today there's a new post from Vic Cherubini about a new PHP 5.3 only framework - Jolt.

Since [I wrote a previous framework], PHP5.3 was released and added a whole slew of features. Wanting to take advantage of them, I rewrote (and renamed) the entire framework from Artisan System to Jolt. The interesting thing about Jolt is that it intentionally doesn't come with any type of ORM or ActiveRecord classes. Because I believe that Models should be fat, and controllers skinny, the model framework is left entirely up to the developer.

The README on the github repository talks about why he decided to make his own framework, some of the features and functionality it includes and an example of it in action (a boostrap file).

tagged: framework jolt orm activerecord model example


Community News:
A PHP 5.3 ActiveRecord
May 23, 2009 @ 23:58:19

Jacques Fuentes has submitted some information about an ActiveRecord tool he's developed that's similar to the pattern found in Ruby On Rails and can be found here.

It is extremely similar to ActiveRecord for Ruby on Rails. We believe our project is offering a better programming experience than propel/doctrine since it does not require effort to get started. Propel/doctrine both require that you use yaml/xml files or column mapping in your model classes. We use show fields to 'magically' load this in the background eliminating the tedious requirement of mapping your schema to a file.

The code makes use of some of the features of the upcoming PHP 5.3 release (but won't work with versions prior to that). If you'd like to try it out, you can grab the code here.

tagged: database activerecord php5