Zend Framework, Architecture with Doctrine - doctrine-orm

im learning Zend Framework (3) ... I installed Doctrine because i do not want to write sql queries to learn all other stuff faster....
First Question:
So know i configured a factory that loads me the doctrine entity manager to my Controllers (with DI).
So its really simple to get my entities to my controller ... e.g in my Project Controller createAction i can easy get my user entities to show them in my project form (project <--> user many-to-many).
But know im struggling, would it be better to create repository classes and inject this to my controllers instead of the doctrine entity manager, so i can filtering etc all my entities?
Second question:
When i want to filter my projects (e.g by user)
where should i do this ... in simpler slim projects i created Collection classes and injected them all of my entities and after that i called a filter method in my collection ... but the problem there is, i just loaded all entities from my database to the collection. In larger application i think there are to many entities loaded from the Database?
Third Question (Conclusion):
When i want to load data from the db to my Controllers whats best Practice here?
Load it from the entity manager ?
Load it from a Repository (the Repository load it from the entity manager)?
Load it from a Collection (the Collection loads data from the repository class and the repository loads data from the entity manager)?
I did not thought about Pagination #all... thats what i have to do next ... but there are many questions similar to my other questions.
(I know there is a zend module for this .. but have no idea how this works.. have too learn this too)
Im thankful for every hint, meaning etc.

Answers to this question are possibly all opinion based. I would say it is all about personal preference. There is no such thing as best practice here it all depends on what you will do in your controller. On top of that you can easily get a repository from your entity-manager if you need it:
$userRepository = $entityManager->getRepository('Application\Entity\User');
A more common might be to make a custom UserService (a wrapper class) around your repository/entity-manager that you populate with the custom methods you would like to use with your User resources.
I hope this helps you a bit...

Related

Import model from inside ember-engine

I have an Ember.js application with an ember-engine added as an external addon, and both the parent application and the engine share the store.
This creates an issue because when fetching a model in a route from the engine, the fact of having the store shared makes the parent application to crash saying that it does not know the model.
Copying the model from inside the engine into the parent's models fixes it, but it goes against the principle of isolation.
I wonder if there is any way of importing an engine's model into the parent application, because even if that also makes the parent aware of the engine's model, at least it won't be replicating code.
Since it is an addon, I supposed this would work:
`import DockerComposeModel from 'ember-stack-builder-engine/models/docker-compose'`
But throws an error
Error: Could not find module `ember-stack-builder-engine/models/docker-compose` imported from `bde-integrator-user-interface/application/controller`
Can you please show us some code How you are configuring the engine.
normally engine and app shares the model and you do not have to use import.
You can directly use this.findRecord(ModelName) but if it does not work then please share your code.

Ember 2 best practices

I'm trying to find best practices for ember 2 related to class defining. I have a few questions...
Are we supposed to be using the ECMA6 "class", or does ember strictly rely on Ember.Object.extend?
If my class is not supposed to be an ember service does that auto make it a Utility?
Do all custom classes belong in Utility, or is it okay to create
another folder for my classes?
More specifically my class is a wrapper for server sent events.
Are we supposed to be using the ECMA6 "class", or does ember strictly
rely on Ember.Object.extend?
I find Ember.js to be a strict framework. But it does not strictly rely on Ember.Object.extend. You can use whatever you want that is valid Javascript.
However, in my experience I would not try to re-implement too much stuff myself. Also, please note not inheriting from Ember.Object will prevent you from using these methods : http://emberjs.com/api/classes/Ember.Object.html.
Do all custom classes belong in Utility, or is it okay to create
another folder for my classes
Why would you create a new folder ? Ember-cli already provides you with Services and Utilities.

ember.js: prepare code to move from ember 1.x to ember 2.0

My question is: Can someone direct me in moving from controller based application to component base application?
I am building a map application for my dog training club. We are specialized in helping finding missing people.
In order to organize our training, we need an application to draw trails and add items on them etc.
I've started an app using Ember-Cli and OpenLayers-3. The application is working nicely, but I would like to move the code from controller based to component base approach.
I would like also to use more routing as at the moment, I have only one route and all user interactions are handled using actions.
I've setup a repository on github for those who would be kind enough to help me:
https://github.com/mylen/mantrailling
if you want to checkout the code and test the app localy, you'll need to modify the referer using a header mod in your navigator to use http://demo.melard.fr
You can see a beta of the website at that page :
http://recherche.utilitaire.melard.fr/beta/map
Thank you in advance,
First, we should clarify the intended uses of components, controllers and routes in ember.js.
Components are similar to Views, but they are isolated and therefore used to create reusable pieces of code, that handle the visual representation of your models.
Controllers are mainly used to decorate your models, but also to hold application state.
Routes represent you current application state. They are responsible for loading your models and to initialize your controllers.
To solve your first problem (controllers -> components), you only need to move all view related stuff, and only this, into components. Your code that decorate your model, for example the active flag of a way-point, remains in the controller. You only need to bind the data of your models/controllers to the components, via embers data binding. (http://guides.emberjs.com/v1.11.0/components/passing-properties-to-a-component)
Your second problem (use routes) is a bit harder to solve, I think. First you need to find all of the states your app currently have. After that, you should move your model loading and saving stuff into this routes.
Edit
Some references describing the problem.
https://github.com/ef4/rfcs/blob/routeable-components/active/0000-routeable-components.md
https://www.youtube.com/watch?v=QgycDZjOnIg
Edit 2
Your question is highly related to How to move ember from 1.x to 2.0, because the changes you mentioned will come along with ember 2.0.
Here are some additional links that describe how to prepare best for this upgrade.
https://gist.github.com/samselikoff/1d7300ce59d216fdaf97
https://speakerdeck.com/tomdale/ember-2-dot-0-in-practice
http://discuss.emberjs.com/t/what-is-a-good-way-to-do-this-in-ember-2-0-no-itemcontroller-no-arraycontroller/6649
You can find a lot of resources if you search for ember 2.0.
Edit 3
Here is I think the most informative source for keeping up with new Ember releases:
https://www.youtube.com/watch?v=wsydQzQF4Ww

Best place/time to create a dynamic model in ember.js

I'm writing a simple software as a service like app where each customer defines a set of configuration for a specific component. Then at runtime I need to construct a range of models based on this configuration.
My question is when should I lookup the configuration and build the array of dynamic ember objects to represent these models?
Currently I'm playing around with this inside the setupControllers function of the ApplicationRoute itself (as it's only loaded 1x when the app is loaded and then I'll have the array of models at any moment instead of waiting for them just in time later on.)
This set of configuration is minimal and the performance gains from this approach are worth it for this example. But is this the right component to load these up / create dynamic models?
My question is when should I lookup the configuration and build the array of dynamic ember objects to represent these models?
It depends of course ;-)
If you need ember to be in a loading state while the lookup happens, then setupControllers is too late. In that case consider using the model hook instead.
Do you need to do all of this configuration upfront? If not maybe push it down to setupControllers callback on a subroute.
But is this the right component to load these up / create dynamic models?
Yes - in this case I'd say setupControllers on application route is a good choice.

Service objects with Doctrine2 and Symfony2

I'm working on a Symfony2/Doctrine2 project which handles 2 databases on MSSqlServer.
The first database A_db has a table forms and the second one B_db has people. All my entities are defined with annotations.
I need to get all forms from forms related to people as I will explain in following lines.
I've spent some time reading related answered questions:
Most likely what I need Using EntityManager inside Doctrine 2.0
entities!
Of course, a service! Doctrine2 Best Practice, Should Entities use Services?
Services/Repositories/Whatever Using the Data Mapper Pattern, Should the Entities (Domain Objects) know about the Mapper?
So I decided that a service might be the best way to handle my needs. But it makes no clear to me how actually get this done. I mean, where to put my service class, how to define it in config.yml, how into people entity...
My wish is to set up a full service (assuming its the best implementation) to perform something like:
foreach($onePeple->getForms() as $form) {/* some code with form */}
In case service implementation for doctrine is not the best practice, then what would it be and how can I make it work?
What you're asking there is possible using the entities alone - so long as you define a relationship such as this on the Form entity:
/**
* #OneToMany(targetEntity="Form", mappedBy="User")
*/
protected $Forms;
And on the User entity:
/**
* #ManyToOne(targetEntity="User", inversedBy="Forms")
*/
protected $User;
Then you can simply load a User entity (through a service, a repository or however you wish) and then access all the forms belonging to that user through $userObj->Forms (if using magic __get on your entities, if not then using a getter method, again down to your preference). Forms is an instance of an object implementing the Doctrine\Common\Collections\Collection interface (since it is a to-many relationship) which is iterable using a foreach.
In projects I have worked on using Doctrine2, we typically use our services for getting, saving and deleting entities, along with listing entities of a type (we call them index methods). This way you can tie in extra functionality required in saving, such as updating other entities which are closely associated and so on. These services are tied to the persistence layer and contain a reference to the entity manager, leaving the entities themselves isolated and testable.
We've also had the argument of whether to put this kind of logic in repositories or services, but that is more a case of personal preference and/or project requirements.
One thing to note - services you build are not going to be linked into Doctrine. It is unaware of your service layer (it's just some userland code) so it is up to you to link it in a meaningful and clean manner. Essentially you'd want something where you could pass the entity manager in through the constructor of the service and have some kind of base service class capable of operating on all entities, which could then be extended with special logic on a per-entity basis. Repositories, however, are intrinsically linked into Doctrine, so if that is what you want then it might be the best solution. We tended to use services as they are then purely related to implementing the business rules and so on, leaving the repositories to be used as a component of the persistence layer.