C0ol_Cod3r
C0ol_Cod3r

Reputation: 949

Symfony 2 Service How To Feed My Own Data Into My Service?

Ok, I have set up a few services in the pass but not many, so my knowledge about them are some what limited.

I have set it up like so,

upload:
    class: MyBundle\Classes\Upload
    arguments: [@doctrine.orm.entity_manager, %FormData]

So I trying to set up my own class to upload my file data. I am doing this has there are a number of controllers that will need to upload the data. I have also tried using, @FormData and $FormData, but still not passing my form data into my service?

Form data is being set as follows:

// getting just the file non mapped, form element
$FormData = $form->get('file')->getData();

This is how I called it in my Controller,

$this->get('upload', $FormData)

However FormData always seems to just dump the variable name and not pass the data into my class.

So my Upload class is set up like this:

class Upload
{
    private $Data;

    public function __construct(EntityManager $em, $Data) {
        $this->em = $em;
        $this->data = $Data; <-*Note 1
    }

    public function UploadFile() {
        // ...do something and save path/data into File Entity...
    }
} 

And yes I am loading the 'EntityManager' namespace into my class...

So why is my $this->data not contain my form data passed to it?

Note 1: This is what I was doing a var_dump() on and it was just displaying $FormData or @FormData.

All help most welcome....

Thanks.


UPDATE

What I am thinking is set it up as a normal class like,

$UploadData = new Upload($Data, $em);

And pass in my data and then the $em?

Is that the best why to set this up?

Thanks

Upvotes: 1

Views: 193

Answers (1)

Jason Roman
Jason Roman

Reputation: 8276

You were really close. The idea behind the service you created is that it is reusable - so you wouldn't want to directly inject a single form's data in the service's constructor. The only thing you want to inject is any other services you might need - in this case, the Doctrine entity manager.

That service is created before any of your controller code is executed, so you would have no way of passing your form data to the constructor anyway. Symfony uses dependency injection to do all of the heavy lifting for you, passing in the Doctrine entity manager so you don't have to manually do that, like you were trying. So your service definition is going to be simplified:

upload:
    class: MyBundle\Classes\Upload
    arguments: ["@doctrine.orm.entity_manager"]

The defiinition of your service is simplified as well:

use Doctrine\Orm\EntityManager;

class Upload
{
    private $em;

    public function __construct(EntityManager $em) {
        $this->em = $em;
    }
}

You now have the option of creating a setter for your form data, but it's probably better to just pass it to an uploadFile() function in your service, like so:

class Upload
{
    public function uploadFile($formData)
    {
        // do something with the form data here
    }
}

Then you can simply do the following in your controller:

$this->container->get('upload')->uploadFile($form->get('file')->getData());

Upvotes: 2

Related Questions