0

i'm new to symfony2 and getting hard time to inject services into my custom class. Following is the Scenario: In Bundle, i created a folder and a class inside it. In that bundle, services.yml has the following code

parameters:
    rc2ab_model.class: rc2ab\ShareBundle\Model\Rc2abModel
services:
    rc2ab_model:
        class: "%rc2ab_model.class%"
        arguments: ["@doctrine.orm.entity_manager"]

This is the constructor of the custom class

public function __construct($em) {

}

After this when i create Object of Rc2abModel Class in DefaultController

$myObj = new Rc2abModel();

Error comes:

Warning: Missing argument 1 for rc2ab\ShareBundle\Model\Rc2abModel::__construct(), called in Default Controller....

I am not able to fix this out, i have read the documentation on their site, so please don't give me documentation link, i have tried other solution here but nothing seems to work for me. Kindly suggest.

1 Answer 1

2

A service is called like that in the controller, you don't have to initial it yourself :

$myObj = $this->get('rc2ab_model');

It will result the object with given arguments from the service registration ^^

Try this now :

echo get_class($this->get('rc2ab_model')); exit;

// Display :
rc2ab\ShareBundle\Model\Rc2abModel
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) last time i ended up doing this thing.. but didn't checked that i'm getting any arg in constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.