symfony2 and Doctrine2 - upload as a service - symfony

I'm learning Symfony2 and need some advice. I have two entities, the first entity "Issue" has a OneToMany relationship with the second entity "Attachment". When posting an Issue form I am successfully uploading all child attachments, everything works hunky-dory :) but now I want to turn my upload solution into a service and that's where I'm getting confused. The uploads are processed in my Attachment model and my controller simply reads as follows :
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
....
From what I understand a service can be accessed from a controller using the "get" method but because I do not process my attachments in the controller and never instantiate the "Attachment" model from the controller how do I pass it an upload service? Instantiating it within the Attachment entity construct appears to defeat the object of the exercise doesn't it?
Forgive me if I am talking rubbish. Idiot friendly guidance would be appreciated.
EDIT:: Further to the feedback, should uploads be treated as a service processed in the controller or could they be a behaviour associated to a model? Which way to jump.

I'm doing something similar in ZF2 - but I use my controller still to process and receive the upload, then use the Service to extract the uploads, register them into the database etc.
I get the name of the file where it's temporarily uploaded to and pass that through to the service (i.e /tmp/up2029398393). That then moves it to where it's supposed to be, creates the entity and does the persist & flush for that object

Related

Understanding of OData services

This is a question to improve my understanding about OData and the process of OData services. I'm not sure about the process when an OData request is sent to the server from my Fiori app. The app is added to our Fiori Launchpad. When the user wants to create a new target group in the UI, a create request is sent. What happens then in detail? What I thought so far:
OData service checks the data
If the data is valid, a new entry in the database is created (HTTP POST)
If the data is not valid, the OData service sends an error
I'm not sure about what information is delivered by the OData service and what information is delivered directly from the database? Does the OData service work like a adjustor which transfers the messages sent from the database to the application?
I hope you can understand what I'm trying to figure out. Thank you for your time.
it depends how your backend-methods are implemented. Every Entityset usually has one of these Methods:
Get Entity
Get EntitySet
Create
Update
Delete
There are some more I guess, but these are mostly used by developers. You can redefine every single method and implement your own Business Logic in there.
So, let's assume you want to send data from the Frontend to your service and insert the data into a table inside your database. You have to redefine the create method of your entity and implement own logic. This could contain an insert into a database-table. You have to consider, that your oData Service will throw an Error if the types which are sent from the frontend do not match the Entity-Types (i.e. a String into an edm.Time type).
Here you can find all EDM.Types oData could consume and the correct mapping of the types:
https://help.sap.com/saphelp_gateway20sp12/helpdata/en/76/4a837928fa4751ab6e0a50a2a4a56b/frameset.htm
Hope this helps :)

How to get self links of embeddedItems from Resources from Spring Data Rest and FeignClient

I have an application where I am using Spring Data Rest to expose my entities in one service, and then use FeignClient from another service to access and update those resources.
In examples I've seen POSTing a #OneToMany sub-resource association in Spring Data REST, the way to establish these relationships is as follows:
Create the entity
Get the "self" href of the newly created entity
Create a list of existing entities of the owning entity for that type and then add that newly created link to the list
Do a "PUT" with the list of URIs to the association URI (with a Content-Type of "text/uri-list") to create the association.
I have done this using AngularJs and it works fine. However, I really do not want my javascript controller to have such an intimate knowledge of my domain objects, I would prefer to have Spring HATEOAS do that work for me.
So what I've done is to create a service in my client application that uses the Spring Cloud FeignClient access those endpoints and do the work that the Angular is presently doing.
The problem that I am running into is that when I get my reference to my associations (e.g. "http://myapp/myobjects/3/myassociation") and then do a "GET" to that URI, it returns "Resources<MyAssociation>" which has no way of getting at the list of URIs of the association objects. All I get for "Links" is the reference to the associations from the owning entity (e.g. "http://myapp/myobjects/3/myassociation").
Is there a way that I can find that list of associations without having to make several other GETs so that I can add the newly added one to it?
Thanks in advance,
CS
URI.create(Resource.getId().getHref()).getPath()
U can try this.

Symfony2 invalid form still saves entity on flush

I currently have a form and on save I always make an API call to validate the form information against an external source (eg: is the company a VAT paying company etc.). I also want to store in the database the response I get from the external API, for which I have a post submit subscriber on the form.
In the subscriber, I call the API and set errors on the form where values do not match. That is where I call a service to handle all the business logic and somewhere in there I have
$em->persist($apiResponse);
$em->flush();
to hold onto the API response.
This ends up saving the entity values attached to the form (company) in the database even though these values are actually invalid and the user is returned to the form page saying that.
Please note this happens on edit, and I understand why it happens, because the entity is managed by Doctrine.
I also tried flushing only the entity I am interested in:
$em->persist($apiResponse);
$em->flush($apiResponse);
and this seems to work, but I read this can have an unexpected behavior.
Hence, my question is: what are the solutions for this ?
Thank you, and sorry for the lack of code samples!

Attaching an entity to a Request in Symfony

I have a symfony 2 application where I set up a kernel.request listener to perform dynamic url checking by loading a corresponding entity from db.
The entity that is loaded then gets attached to a service, so that throughout the application who ever needs that data can ask the service for it.
Reviewing the code I was tempted to simply attach the entity to the Request object. It kind of makes sense, since this entity is used everywhere in the application, and it's determined by a url component.
Is that a proper way to use the Request, or is it better to leave it incapsulated into a service?
Thanks in advance
sergio

Symfony 2 - How can I share data between controllers

I need to be able to make some request data from one controller available in another controller. I can make a service to set the data in one controller, but when the other controller fires and I get the service, a new instance of the service is created. Is there any way I can make this data static and share it between two controllers?
The same basic things you would do whenever you need information to be available in PHP from a new request:
Store it in the session. Symfony2 has a great session component for this. Ideal for fleeting data that needs to be saved only while the user is navigating
Store it in the database. Symfony2 supports Doctrine which makes this very easy. Ideal for permanent storage
Optionally:
Store it on the filesystem. Not recommended unless it's actually a file, but possible as well.
In the end, rather than using the session to store data, I created two separate routes to the same controller action. I added an optional argument in the controller action, with a default value only specified in one of the routes. I can then test for that argument's value when the controller runs. In the Twig template that calls this controller action, the path can be generated using either one of these routes, depending on a variable already available.
Bit of a work around, but problem solved!

Resources