【问题标题】:Symfony call database from class object来自类对象的 Symfony 调用数据库
【发布时间】: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


    【解决方案1】:

    你的类应该扩展类 ContainerAware

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

    这样您就可以访问容器及其所有服务。

    【讨论】:

    • 这次我得到了这个错误:Error: Call to a member function get() on a non-object
    • 我已经把它放到了一个函数中。我正在使用 symfony 3,因此 ContainerAware 不再可用。我使用 use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; 并且我实现了 ContainerAwareInterface 并在类中添加了使用 use ContainerAwareTrait;
    【解决方案2】:

    使用 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;
           }
       }
    

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2017-10-22
      • 2012-09-02
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      相关资源
      最近更新 更多