简体   繁体   中英

ZF2/PHP - Model Factory

I just want to ask, how I should create a model class instance in ZF2? I create all my services, forms, etc... with factories in the ServiceManager/FormElementManager/etc... to inject the dependencies of the different classes, but I think that's not the right way to create a model instance like a product for example, isn't it?

Should I just create it with $product = new Product() ? But what if Product does have dependencies? I don't want to initialize the dependencies at all the different places where I need them.

Thanks for any hints.

Think of me. I did not follow any standard rules while developing an application that you are developing now. I am sure! You are now coming across various problems. Because I did not take care of dependency injection, scattered object creation etc. in building that application. So guess how difficult it is now! Dark! Dark!

All the efforts for building software/web application is only for Good/Standard Design . This really makes building software/web application faster and keeps everything neat and clean. In more general, this is for us if we are a developer/programmer.

Now in ZF2 :

Make everything serviceable where possible. Do your needs folder-wise, like below

src/ModuleName/Model
src/ModuleName/Service
src/ModuleName/Form

Create closure or factory (for attaching services to the ServiceManager) to make them available around your application. Any dependency you can inject there. Keep the Module.php thin, make the module.config.php fatty.

Managing the relationship of the domain model (entities/model classes) is not something that should be attempted using dependency injection yourself. This would be considered reinventing the wheel.

This is because when we talk about the domain model we are really talking about the data in your application. As a developer you have already made the decision to model your data as objects (ie new Product(); is a class that represents the product data in your application). By doing so you have given the data all the fantastic abilities that we love in OOP. This is great, until you release that this quickly becomes very complex, especially in large application. A product 'model' has relationship, relationships to other models etc. A large application can end up with the need to manage multiple instances of these classes and their relationships in memory.

When you try to persist this data into common storage (a relational database) you encounter many issues of complexity. This problem has been coined as the Object Relational Impedance Missmatch and really is a technical problem that is generic to most OOP languages using a database back-end.

A solution that is very popular is to use an Object Relational Mapper (ORM), such as Doctrine. This abstracts the database access and allows you to write your code using 'entities' while ignoring the complexity of having to manage dependencies.

While very powerful, introducing a ORM can cause other issues. This is the reason we have object databases such as MongoDB and probably the reason why, out of the box, ZF2 doesn't ship with any database abstraction or 'domain layer'. It really will depend on your needs for any given project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM