How to isolate the active record in laravel5?

Wondered how to safely isolate the AR model and to drive their application, to services, to return from repositories etc. ?

I recently realized how not much screwed up the application architecture. The fact that I sprayed the model AR for the entire project, despite the fact that I used the repositories (was not correct).

For example we have a simple code:
$types = Type::with('fields')->get();

We take some types of storage. Now suppose that AR and tomorrow is a REST API or a Query Builder, in General the result will be different. Ie in the first case AR, the second array into a third collection. Not good.

I wanted this whole thing to lead to one format:
- are not you afraid that someone intentionally or accidentally make $type->save() anywhere in your project
- return a single data type from the repository
- to get away from the logic of the AR, and pass in the types and between services just data (such as using DTO)

Come on revenue collection:
$type = Type::findOrFail($id);
 collect($type);


With a single entity that works fine, however when there is say communiques:
$types = Type::with('fields')->get();
collect($types->toArray());


It turns out that all nested reports is just an array, ie not every entry in the reports is collections, and something like this:
Illuminate\Support\Collection Object
(
 [items:protected] => Array
(
 [0] => Array
(
 [id] => 1
 [alias] => test1
 [name] => news
 [description] => 
 [fields] => Array
(
 [0] => Array
(
 [id] => 1
 [type_id] => 1
 [type] => test
 [is_required] => 1
 [validation] => 1
)

)

)

 [1] => Array
(
 [id] => 2
 [alias] => test2
 [name] => Article
 [description] => 
 [fields] => Array
(
 [0] => Array
(
 [id] => 2
 [type_id] => 2
 [type] => test
 [is_required] => 1
 [validation] => 2
)

)

)

 [2] => Array
(
 [id] => 3
 [alias] => test3
 [name] => Jobs
 [description] => 
 [fields] => Array
(
)

)

)

)


Ie record inside relational fields, just an array and not a nested collection.

Interested in the following 2 questions:
- Would it be correct to bother with insulation AR using DOT or collection ?
- You need to write your handler collections that will convert the model with the correlations recursively in a collection with nested collections, or is there another option ?
July 2nd 19 at 14:26
2 answers
July 2nd 19 at 14:28
The interesting thing is, what I'm trying to implement. Once I read about it, but did not understand what I mean. Right now more clear after a bit of the patient experience. That's just the Fowler usually mentioned in passing in 2 words. There is somewhere a more or less "smooth" implementation of this patteran ? - Justice commented on July 2nd 19 at 14:31
: Yes seeker shall find. - Allene_Crona78 commented on July 2nd 19 at 14:34
: =), then the main guano is not found. Since when are you interested in the Bible ? - Justice commented on July 2nd 19 at 14:37
: it's just an expression which seemed to me more pleasant than "go Google". - Allene_Crona78 commented on July 2nd 19 at 14:40
July 2nd 19 at 14:30
We take some types of storage. Now suppose that AR and tomorrow is a REST API or a Query Builder, in General the result will be different. Ie in the first case AR, the second array into a third collection. Not good.
Create a service layer, enter the incoming and return types (you can create a new non-persistent types model) to methods. And the implementation within the service may change, no one affected.

not afraid that someone intentionally or accidentally make $type->save() anywhere in your project
Why? AR so used.
Question looks like you just want to create a wrapper around AR, creating a makeshift semblance of a DM, but why don't you take DM?

Would it be correct to bother with insulation AR
This (anti)pattern was created and popular with the rapid development, initially disturbing the SOLID. With him usually don't bother and rivet small projects.
Yes, that's right I want the wrapper. There is rather a question of abstraction. If you implement a repository for ideas no matter where we take the data from AR or from AR, but the return format is important to padkontrol.

I entered the repository, but hemorrhoids got nothing. As the model is anyway run on the project. KPD - 0.

Here I want to somehow bother on the wrapper AR or at least to drive data outside of AR, and collections. What would you advise me ? - Justice commented on July 2nd 19 at 14:33
and you'll read: www.martinfowler.com/eaaCatalog/rowDataGateway.html

That is the active record in the schema is when we all three object of these mergim one. And Yes, it is a violation of the principle of single liability, but for very simple projects this is enough.

But we can use active record only as the data model for our object model (attributes of entities). Full control of the data management of the wrapper, dividing the data storage and business logic. So we like active record but as not.

On the other hand there are problems with connections, etc. - Allene_Crona78 commented on July 2nd 19 at 14:36
: I read this book in translation. What exactly do you want to pay attention to?
Apparently the author of the question came from the status of "very simple project" and is now suffering from AR. And will suffer, because AR is a pain for long-term projects. - Justice commented on July 2nd 19 at 14:39
: Yes, there is. We need to develop further. - Allene_Crona78 commented on July 2nd 19 at 14:42

Find more questions by tags LaravelOOP