为了从 Web 服务中删除游戏会话,我们定义了以下控制器。
我希望删除一个 Session 实体,而不是从数据库中删除所有记录。
SessionController.php
/**
* @Route("/{id}/")
* @Method({"DELETE"})
*/
public function deleteAction($id)
{
$repository = $this->getSessionRepository();
$session = $repository->find($id);
$em = $this->getEntityManager();
$em->remove($session);
$em->flush();
return new Response('', 200);
}
services.yml 包含:
sh.repository.session:
class: ScavengerWebserviceBundle\Entity\SessionRepository
factory_service: doctrine.orm.default_entity_manager
factory_method: getRepository
arguments:
- 'ScavengerWebserviceBundle:Session'
/**
* @return \Scavenger\WebserviceBundle\Entity\SessionRepository
*/
private function getSessionRepository()
{
return $this->container->get('sh.repository.session');
}
该实体包含:
<?php
namespace Scavenger\WebserviceBundle\Entity;
use Scavenger\WebserviceBundle\Entity\Battlezone;
use Doctrine\ORM\Mapping as ORM;
use \Doctrine\ORM\Mapping\OneToOne;
use \Doctrine\ORM\Mapping\ManyToMany;
use \Doctrine\ORM\Mapping\JoinTable;
use \Doctrine\ORM\Mapping\JoinColumn;
/**
* Scavenger\WebserviceBundle\Entity\Session
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Scavenger\WebserviceBundle\Entity\SessionRepository")
*/
class Session
{
const SESSION_ACTIVE = 0; //lol
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var integer $mrX
*
* @ORM\Column(name="mrX", type="integer")
*/
private $mrX;
/**
* @var integer $causer
*
* @ORM\Column(name="causer", type="integer")
*/
private $causer = 0;
/**
* @var integer $statusCode
*
* @ORM\Column(name="status_code", type="integer", nullable=false)
*/
private $statusCode = self::SESSION_ACTIVE;
/**
* @ManyToMany(targetEntity="Scavenger\WebserviceBundle\Entity\User", mappedBy="sessions", cascade={"all"})
*/
private $users;
/**
* @OneToOne(targetEntity="Scavenger\WebserviceBundle\Entity\Battlezone", cascade={"all"})
* @JoinColumn(name="battlezone_id", referencedColumnName="id")
*/
private $battlezone;
/**
* @var integer
* @ORM\Column(name="start", type="integer")
*/
private $start = 0;
/**
* @var integer
* @ORM\Column(name="end", type="integer")
*/
private $end = 0;
//a bunch of setters and getters ...
}