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

Toptal.com:
Maintain Slim PHP MVC Frameworks with a Layered Structure
Apr 07, 2017 @ 16:17:53

The Toptal.com blog has a tutorial posted by Elvira Sheina showing you how to keep a framework project "slim" and manageable in a MVC pattern using a "layered" structure. This structure adds a few extra components to the traditional MVC design to keep functionality cleaner and easier to maintain.

Fat controllers and models: an inevitable problem for most large-scale projects based on MVC frameworks such as Yii and Laravel. The primary thing that fattens controllers and models is the Active Record, a powerful and essential component of such frameworks.

She starts by talking about one of the main issues in MVC applications - "fat" controllers. In this example the controllers contain the bulk of the logic for the application making it difficult to modify and potentially reuse in other places. This is particularly bad when the Active Record pattern is used and the problem of it violating the SRP (Single Responsibility Principle of SOLID development). Instead she promotes the idea of the "layered" design using controllers, a service layer, DTOs, view decorators and a repository layer. She then shows how to implement this kind of structure and tie each of the pieces together with code examples for each piece.

tagged: tutorial mvc framework structure layer dto repository activerecord decorator service

Link: https://www.toptal.com/php/maintain-slim-php-mvc-frameworks-with-a-layered-structure

TutsPlus.com:
How to Program With Yii2: ActiveRecord
Mar 09, 2017 @ 18:07:53

In the latest tutorial in their "Programming with Yii2" series the TutsPlus.com site shows you how to work with the ActiveRecord functionality included with the framework.

In this Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In today's tutorial, I'll walk you through using Yii's object-relational mapping, known as ORM, for working with databases. It's called Active Record and is a key aspect of programming database applications efficiently in Yii.

Yii offers different ways to work with your database programmatically, such as direct queries and a query builder, but using Active Record offers a complete set of benefits for object-oriented database programming.

The article goes on from there and defines some of the basics around what Active Record is and how it works. It then starts on the code, showing how to create an ActiveRecord model and execute queries to:

  • locate single or multiple records
  • build queries
  • counting records
  • and accessing the records returned

They also talk about mass assignment, saving data via model instances, deleting records and creating relationships between the models.

tagged: yii2 framework series activerecord database tutorial introduction

Link: https://code.tutsplus.com/tutorials/how-to-program-with-yii2-active-record--cms-27434

Laravel Podcast:
Episode #42 - Shots Fired
Feb 25, 2016 @ 18:18:28

The Laravel Podcast, hosted by Matt Stauffer (with guest hosts Taylor Otwell and Jeffrey Way) has posted its latest episode - Episode #42: ActiveRecord & The School Of Zonda:

In this episode, the crew is joined by Adam Wathan to discuss ActiveRecord and the Eloquent ORM.

Lots of talk around ActiveRecord vs DataMapper and discussing the positives and negatives to each.

You can listen to this latest episode either through the in-page audio player or by downloading the mp3. If you enjoy the show, be sure to subscribe to their feed or follow them on Twitter of the latest show updates.

tagged: eloquent activerecord mattstauffer ep42 podcast laravel orm

Link: http://www.laravelpodcast.com/episodes/27610-episode-42-activerecord-the-school-of-zonda

Paul Jones:
Atlas: a persistence-model data mapper
Dec 30, 2015 @ 15: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 @ 15: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 @ 15: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/

QaFoo.com:
Code Reuse By Inheritance
Jan 20, 2014 @ 16: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 @ 15: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 @ 15: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 @ 16: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

Link:


Trending Topics: