jojovem
4 years ago
144
10
Eloquent

Entities tightly coupled with Eloquent

Posted 4 years ago by jojovem

Greetings to all!

I am using Laravel since 2011 now. After a few months of studying design patterns and specially DDD (Domain Drivern Design), i came across serious doubts of how Eloquent handles its relation with Entities.

First thing that bugs me is the necessity of making my Entity extend Eloquent, for me, this is telling my Entity how it interacts with its persistence, being far away from a plain php class. I know that Doctrine uses different approaches like annotations, which doesn´t make the Class totally unaware from the database layer, but i think is a cleaner way to handle with it. Is this a genuine concern? Im really lost at trying to explain if this is a good or a bad thing. I need more information on this!

The second concern is the way we are acessing an Entity relation. If i want to get the books related to a user, the common practice in Eloquent would be to add the relation into the entity (a method, not a property) and then access it like $user->books (Collection) or $user->books()->get() (more intuitive to me).

I think $user->books()->get() is more intuitive because i can actually see that there is a method called books() and it returns a Collection. I really dont like the idea of $user->books as books is not a property, rather a magic way to do the same thing as $user->books()->get(). I mean, $user->books is kinda beautiful, but as its not defined as a property, i feel kinda weird about it.

Are those genuine concerns? If so, is there a way to leverage Eloquent as a decoupled ORM solution? Because the repository pattern doesn´t do that, the Entity still extends Eloquent in a way that it know how its persistence work (even if all persistence methods are in the repository).

I want to build complex applications with Laravel, and to do so, im trying to understand how this issues would affect the testability and other things. I am kinda lost in these conceptions.

Thanks in advance!

Please sign in or create an account to participate in this conversation.