我正在尝试构建一个简单的应用程序我有一个数据库,其中的表“匹配”表结构
我将这段代码写为实体
<?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;
}
}
Run Code Online (Sandbox Code Playgroud)
并将其作为控制器:
<?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());
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我找不到
我缺少什么?我是超级n00b:(谢谢你的帮助
你必须扩展基本的 symfony 控制器:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AddMatch extends Controller
{
...
}
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因您无法扩展控制器,您仍然可以使用 Doctrine 实体管理器。在这种情况下,您需要注入服务容器,然后使用以下命令获取实体管理器
$container->get('doctrine')->getManager();
Run Code Online (Sandbox Code Playgroud)
您应该仔细阅读有关 Service Container 的 Symfony 指南。