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