0

On my symfony project I try to call doctrine from a class object, the idea is to put information into the database from the constructor of the object.

First, I must check if the object is already in the database, and if not, I must create the object and add it into the data base. I do this from the class object because a lot a information are done in a single loop, and for optimization, I would like to do it from the object.

My object:

/** class object */
use MainBundle\Services\UserService;
use MainBundle\Entity\Item;
use MainBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;

class MyListObject{
    function MyListObject($data,$bddUser=null){
        if ($data->getSuccess()){
            foreach($data->getIdItem() as $id){
                $item = new IntItem($id,$data,$this);

                //database stuff
                $repository = $this->get('service.item');//get the table user from the database
                $existItem = $repository->getItemById($id);//get the item

                if ($existItem == null){
                    $itemBdd = new Item();
                    $itemBdd->setIditem($item->getId());
                    $itemBdd->setIdfull($item->getClassIdFull());
                    $itemBdd->setName($item->getName());


                }

My service:

services:
    service.item:
        class: MainBundle\Services\ItemService
        arguments: ['@doctrine.orm.default_entity_manager']

My service class:

<?

class ItemService
{

    private $entityManager;

    /*
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /*
     * @param integer $blogId
     *
     * @return Blog
     */
    public function getItemById($itemId)

        return $this->entityManager
                    ->getRepository('MainBundle:Item')
                    ->find($itemId);
    }
}

Then I got this error:

Attempted to call an undefined method named "get" of class "MainBundle\Object\MyListObject".

And after that I want to call doctrine again from my object to add the item to the database. I really don't know how I can fix that. all the injections I try haven't worked, I also try to extends controller, but I get another error.

Thanks. be nice with my english please, I have try my best !

I really don't know where I go wrong

2 Answers 2

0

your class should extend class ContainerAware

use \Symfony\Component\DependencyInjection\ContainerAware;

class MyListObject extends ContainerAware
{
    public function someMethod() 
    {
        $this->container->get('service.item');
        ...
    }
}

like so you'll have access to the container and all its services.

Sign up to request clarification or add additional context in comments.

2 Comments

this time I got this error : Error: Call to a member function get() on a non-object
I already place it into a function. I am using symfony 3 so the ContainerAware is not avalable anymore. I use use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; that and also I implements ContainerAwareInterface and add a use use ContainerAwareTrait; inside the class
0

Whith symfony 3

use Symfony\Component\DependencyInjection\ContainerInterface
use Symfony\Component\DependencyInjection\ContainerAwareInterface;   

class ItemService implements ContainerAwareInterface
   {
       private $container;

       public function setcontainer(containerinterface $container = null)
       {
           $this->container = $container;
       }
   }

Comments

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.