【发布时间】:2016-10-26 18:18:11
【问题描述】:
在我的 symfony 项目中,我尝试从类对象调用教义,其想法是从对象的构造函数将信息放入数据库。
首先,我必须检查对象是否已经在数据库中,如果没有,我必须创建对象并将其添加到数据库中。 我从类对象中执行此操作,因为很多信息是在一个循环中完成的,为了优化,我想从对象中执行此操作。
我的对象:
/** 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());
}
我的服务:
services:
service.item:
class: MainBundle\Services\ItemService
arguments: ['@doctrine.orm.default_entity_manager']
我的服务等级:
<?
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);
}
}
然后我得到了这个错误:
Attempted to call an undefined method named "get" of class "MainBundle\Object\MyListObject".
然后我想再次从我的对象中调用学说来将项目添加到数据库中。我真的不知道我该如何解决这个问题。我尝试的所有注入都没有奏效,我也尝试扩展控制器,但我得到另一个错误。
谢谢。 请对我的英语好点,我已经尽力了!
我真的不知道我哪里错了
【问题讨论】:
标签: php service doctrine-orm symfony