【问题标题】:Symfony and Doctrine - getDoctrine not foundSymfony 和 Doctrine - 找不到 getDoctrine
【发布时间】:2017-05-12 21:15:36
【问题描述】:

我正在尝试构建一个简单的应用程序 我有一个带有“匹配”表的数据库the table structure

我把这段代码写成实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="matches")
 */

class Match
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="text", length=512)
     */
    private $descr;
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $team_a;
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $team_b;
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $location;
    /**
     * @ORM\Column(type="datetime")
     */
    private $datetime;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set descr
     *
     * @param string $descr
     *
     * @return Match
     */
    public function setDescr($descr)
    {
        $this->descr = $descr;

        return $this;
    }

    /**
     * Get descr
     *
     * @return string
     */
    public function getDescr()
    {
        return $this->descr;
    }

    /**
     * Set teamA
     *
     * @param string $teamA
     *
     * @return Match
     */
    public function setTeamA($teamA)
    {
        $this->team_a = $teamA;

        return $this;
    }

    /**
     * Get teamA
     *
     * @return string
     */
    public function getTeamA()
    {
        return $this->team_a;
    }

    /**
     * Set teamB
     *
     * @param string $teamB
     *
     * @return Match
     */
    public function setTeamB($teamB)
    {
        $this->team_b = $teamB;

        return $this;
    }

    /**
     * Get teamB
     *
     * @return string
     */
    public function getTeamB()
    {
        return $this->team_b;
    }

    /**
     * Set location
     *
     * @param string $location
     *
     * @return Match
     */
    public function setLocation($location)
    {
        $this->location = $location;

        return $this;
    }

    /**
     * Get location
     *
     * @return string
     */
    public function getLocation()
    {
        return $this->location;
    }

    /**
     * Set datetime
     *
     * @param \DateTime $datetime
     *
     * @return Match
     */
    public function setDatetime($datetime)
    {
        $this->datetime = $datetime;

        return $this;
    }

    /**
     * Get datetime
     *
     * @return \DateTime
     */
    public function getDatetime()
    {
        return $this->datetime;
    }
}

这个作为控制器:

<?php

namespace AppBundle\Controller;


use AppBundle\Entity\Match;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;


class AddMatch
{
    /**
     * @Route("/addmatch")
     */
    public function createAction()
    {
        $match = new Match();
        $match->setDescr('Descrizione Partita');
        $match->setTeamA('Squadra A');
        $match->setTeamB('Squadra B');
        $match->setLocation('a nice Gym');
        $match->setLocation('12/12/2012');
        $em = $this->getDoctrine()->getManager();


        // tells Doctrine you want to (eventually) save the Product (no queries yet)
        $em->persist($match);

        // actually executes the queries (i.e. the INSERT query)
        $em->flush();

        return new Response('Saved new match with id '.$match->getId());
    }

}

但它不起作用,我得到Not Found

我错过了什么? 我是超级n00b :( 感谢您的帮助

【问题讨论】:

  • 您需要扩展基本的 symfony 控制器才能使用 getDoctrine 方法
  • 你应该扩展Symfony\Bundle\FrameworkBundle\Controller\Controller
  • 我该怎么做?
  • 按照文档中的示例进行操作:symfony.com/doc/current/controller.html 除了扩展 symfony 基本控制器类之外,您还需要做几件事。

标签: php mysql symfony doctrine


【解决方案1】:

你必须扩展基本的 symfony 控制器:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class AddMatch extends Controller
{
    ...
}

如果由于某种原因,您无法扩展控制器,您仍然可以使用 Doctrine 实体管理器。在这种情况下,您需要注入服务容器,然后获取实体管理器

$container->get('doctrine')->getManager();

你应该仔细阅读the Symfony guide on Service Container

【讨论】:

    猜你喜欢
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多