DDD Laravel. Repository pattern. How to retrieve an object from persistency and convert it into a not Laravel Entity model? - laravel

I'm aplying DDD in Laravel.
In this architecture, the entity (conformed by the corresponding value objects) is not a Laravel Model extended class (because the domain layer needs to be agnostic to the infrastructure)
So... when I retrieve some data inside the repository implementation, the result is a stdclass object, and I need to return it as the entity object.
Anybody knows the best approach to do this?
Thanks!
To get this, I tried to convert from stdclass to entity by manual, but it's look hacky and dirty.

Ok, got it.
I found two different approaches, just in case others are fighting with the same problem.
Option 1: Embracing the Eloquent Active Record.
Inside the infrastructure layer, I created a Eloquent model to represent the Entity, and I use it as a vehicule for eloquent queries. Like this, all the conection with the framework stay contained in the infrastructure, without polluting other layers.
Option 2: Apply Doctrine in Laravel.
Doctrine has a package for laravel. Doctrine, as occurs in Synfony, is using data mapping, so no worries with that.
Thanks anyway!

Related

Laravel Modeling System

I have been using Laravel for a couple weeks now and I love the framework. However, with models is there any actual robust system? Creating models, like the user model, just seems to be string arrays of what you do and do not want modified in queries. The place I see validation of the models is in the controller (through $this->validate()) and/or migrations creating the tables.
I just wanted to know if there is a certain place I am missing or not implementing. My brain is referencing something like ASP.NET's decorators that add validation to data models. Thank you!

My relationships are missing in strapi model afterCreate

I have created a model with a relationship to another model.
I want to send an email with the entry information, afterCreate seems like the place to do so. But it doesn't contain my relationship fields on the model object.
We have an issue about that on GitHub, there is currently not workaround about that.
Alex is re-writing the logic behind the life cycle function, so lot of issue should be fixed on that after the release of the new system.
As of Strapi 3.0.0, the model lifecycles have changed and now afterCreate does contain relationships.

Models responsability

I have a doub about Laravel. The models are used to define the relationships between the models like hasMany, belongsTo, etc. Also the models are used to define the fillable fields. But he models are only for that? Because I already check some examples that it seems that some queries are executed in the models instead of the controller so Im not understanding if the models should also have the querying of the relationships or not. Can you give a help to understand better what is the correct use of models (what should be placed in the models)?
Its same way to execute queries on model or controller. Written queries in model make your controller more clean. We can write mutator, accessor or query scope in eloquent model. Correct me if I'm wrong.
Visit https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Model.html
You can refer this documentation, it's quite helpful if you want to know deep basics and responsibilities about particular part in Laravel.
Models are mostly used to make an outlook of the data i.e what fields are going to be saved in the database and we also use it to associate the relationships with other related data as you already know but we also use it to alter the values that are either going in/out of the data base which you can check in the documentation in link bellow https://laravel.com/docs/5.6/eloquent-mutators
the main purpose is to divide the code between controller and model (were it best fit to be)

How to store log of all changes to Eloquent model in Laravel 5.4?

I want to store changes to all fields in Eloquent model into database.
I can do it using created and updated events but there is a problem with multiple foreign relations (described as separate tables).
Example:
User
login
Roles -> hasMany
When I update login field it is easy to write old and new value into database, but when I update Roles relation nothing happens.
How can I track all foreign relations (like hasMany, hasManyThrough, ManyToMany)?
Owen-it has a very nice Library called laravel-auditing, which keeps an easy to query list of all changes that are made to a model, and I think it does quite an awesome piece of work. Have used it and it is worth it to try out.
There is no embedded and simple method to do it.
Eloquent doesn't provide any method to implement observers on related models by design. Many proposal in this way have been rejected by Taylor (just one example).
The only thing you can do, is to create your own methods to do it.
You have many possibilities, here are some of them in order of complexity (some of them are "dirty" :-)
add a created and updated observer on each related model
override the save() or create your own saveAndFire() method on your eloquent instances, and from that method retrieve the parent and call its log methods before saving. (this is a little bit "dirty" imho)
encapsulate all your persistence layer and fire events yourself on saving objects (look at the repository pattern, for example)

how to use codeigniter database models

I am wondering how the models in code ignitor are suposed to be used.
Lets say I have a couple of tables in menu items database, and I want to query information for each table in different controllers. Do I make different model classes for each of the tables and layout the functions within them?
Thanks!
Models should contain all the functionality for retrieving and inserting data into your database. A controller will load a model:
$this->load->model('model_name');
The controller then fetches any data needed by the view through the abstract functions defined in your model.
It would be best to create a different model for each table although its is not essential.
You should read up about the MVC design pattern, it is used by codeigniter and many other frameworks because it is efficient and allows code reuse. More info about models can be found in the Codeigniter docs:
http://codeigniter.com/user_guide/general/models.html
CodeIgniter is flexible, and leaves this decision up to you. The user's guide does not say one way or the other how you should organize your code.
That said, to keep your code clean and easy to maintain I would recommend an approach where you try to limit each model to dealing with an individual table, or at least a single database entity. You certainly want to avoid having a single model to handle all of your database tables.
For my taste, CodeIgniter is too flexible here - I'd rather call it vague. A CI "model" has no spec, no interface, it can be things as different as:
An entity domain object, where each instance represents basically a record of a table. Sometimes it's an "anemic" domain object, each property maps directly to a DB column, little behaviour and little or no understanding of objects relationships and "graphs" (say, foreign keys in the DB are just integer ids in PHP). Or it can also be a "rich (or true) domain object", with all the business intelligence, and also knows about relations: say instead of $person->getAccountId() (returns int) we have $person->getAccount(); perhaps also knows how to persist itself (and perhaps also the full graph or related object - perhaps some notion of "dirtiness").
A service object, related to objects persistence and/or general DB querying: be a DataMapper, a DAO, etc. In this case we have typically one single instance (singleton) of the object (little or no state), typically one per DB table or per domain class.
When you read, in CI docs or forums, about , say, the Person model you can never know what kind of patter we are dealing with. Worse: frequently it's a ungly mix of those fundamentally different patterns.
This informality/vagueness is not specific to CI, rather to PHP frameworks, in my experience.

Resources