【发布时间】: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