duanoucuo7045 2017-06-07 20:18
浏览 26
已采纳

Symfony2.8错误在不使用doctrine的对象上下文中时使用$ this

I am tryin to call a function from my controller and return the $todolist to my controller. But i get this error, when i leave this function inside the controller it works fine, but i don't want it to be in the controller.

This is my controller

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configurationoute;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use AppBundle\Entity\Todo;
use AppBundle\Model\TodoModel;

class TodoController extends Controller
{
    /**
    * @Route("/", name="homepage")
    */
    public function ShowList()
    {
        $todolist = TodoModel::getTodolist();

        $html = $this->container->get('templating')->render(
            'todolist/todolist.html.twig',
            array('todolist' => $todolist)
        );

        return new response($html);
    }
}

This is my model

namespace AppBundle\Model;

use AppBundle\Entity\Todo;

class TodoModel 
{

     public function getTodolist()
     {
       $repository = $this->getDoctrine()
           ->getRepository('AppBundle:Todo');
       $todolist = $repository->findAll();
       return $todolist;
    }

}
  • 写回答

2条回答 默认 最新

  • dqjmq28248 2017-06-08 11:55
    关注

    Right way to do it is:

    public function ShowList()
    {
        $entityManager = $this->get('doctrine.orm.entity_manager');
        $repository = $entityManager->getRepository(Todo::class);
        $todolist = $repository->findAll();
    
        $html = $this->container->get('templating')->render(
            'todolist/todolist.html.twig',
            array('todolist' => $todolist)
        );
    
        return new Response($html);
    }
    

    If you want to do some kind of abstraction with "TODO provider", then better naming for it is TodoProvider instead of TodoModel.

    And it should look like:

    namespace AppBundle\Todo;
    
    use Doctrine\ORM\EntityManager;
    use AppBundle\Entity\Todo;
    
    class TodoProvider
    {
        /**
         * @var EntityManager
         */
        private $entityManager;
    
        /**
         * TodoProvider constructor.
         * @param EntityManager $entityManager
         */
        public function __construct(EntityManager $entityManager)
        {
            $this->entityManager = $entityManager;
        }
    
        /**
         * @return Todo[]
         */
        public function getAllTodos()
        {
            return $this->entityManager->getRepository(Todo::class)->findAll();
        }
    }
    

    Then register TodoProvider as service (http://symfony.com/doc/current/service_container.html) and get in controller with $this->get('my_todo_provider')

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计