我在symfony2中有一个api,其中添加和删除项目没有问题,但是,更新不会给我带来任何错误,但是我的数据库中的相关行没有得到更新!
我的控制器方法:
/*
* @Route("/complete/uid/{storeUid}",
* name = "media_complete"
* )
*
* @Method({"GET"})
*
* @param String $storeUid - the uid for the store that has completed downloading
*
* @return Response
*/
public function downloadCompleteAction($storeUid)
{
$response = $this->getNewJsonResponse();
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository("SimplySmartUHDProBundle:Machine");
try {
//get the machine from the db
$machine = $repo->findOneBy(array(
"uid" => $storeUid
));
//set it as download completed
$machine->setStatus(Machine::STATUS_DOWNLOAD_COMPLETE);
$em->persist($machine);
$em->flush();
$response->setStatusCode(200);
} catch (\Exception $e) {
//if there is an error, catch the exception set the status to 500 and return json with the exception error
$error = array("message" => $e->getMessage());
$response->setContent(json_encode($error));
$response->setStatusCode(500);
}
return $response;
}
我的实体:
namespace SimplySmart\UHDProBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use SimplySmart\UHDProBundle\Entity\Store;
/**
* Machine
*
* @ORM\Table(name="Machines", uniqueConstraints={@ORM\UniqueConstraint(name="UID", columns={"UID"})})
* @ORM\Entity
*/
class Machine
{
const STATUS_NO_FEED = 0;
const STATUS_REQUIRES_DOWNLOAD = 1;
const STATUS_DOWNLOAD_IN_PROGRESS = 2;
const STATUS_DOWNLOAD_COMPLETE = 3;
/**
* Array of available statuses
*
* @var array
*/
static public $statuses = array(
self::STATUS_NO_FEED,
self::STATUS_REQUIRES_DOWNLOAD,
self::STATUS_DOWNLOAD_IN_PROGRESS,
self::STATUS_DOWNLOAD_COMPLETE
);
/**
* @var string
*
* @ORM\Column(name="UID", type="string", length=50, nullable=false)
*/
private $uid;
/**
* @var string
*
* @ORM\Column(name="StoreCode", type="string", length=10, nullable=false)
*/
private $storecode;
/**
* @var string
*
* @ORM\Column(name="Description", type="string", length=100, nullable=true)
*/
private $description;
/**
* @var boolean
*
* @ORM\Column(name="Status", type="boolean", nullable=false)
*/
private $status;
/**
* @var boolean
*
* @ORM\Column(name="OnlineStatus", type="boolean", nullable=false)
*/
private $onlinestatus;
/**
* @var string
*
* @ORM\Column(name="Version", type="string", length=10, nullable=false)
*/
private $version;
/**
* @var \DateTime
*
* @ORM\Column(name="Timestamp", type="datetime", nullable=false)
*/
private $timestamp;
/**
* @ORM\ManyToOne(targetEntity="SimplySmart\UHDProBundle\Entity\Store", inversedBy="machines")
* @ORM\JoinColumn(name="StoreCode", referencedColumnName="Code")
*/
protected $store;
/**
* @ORM\ManyToMany(targetEntity="SimplySmart\UHDProBundle\Entity\Feed", inversedBy="machines")
* @ORM\JoinTable(joinColumns={@ORM\JoinColumn(name="machine_id", referencedColumnName="MachID")},
* inverseJoinColumns={@ORM\JoinColumn(name="feed_id", referencedColumnName="FeedID")}
* )
*
*/
protected $feeds;
/**
* @var integer
*
* @ORM\Column(name="MachID", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $machid;
/**
*
*/
public function __construct()
{
$this->feeds = new ArrayCollection();
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
*
* @return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* @return string
*/
public function getFeedtype()
{
return $this->feedtype;
}
/**
* @param string $feedtype
*
* @return $this
*/
public function setFeedtype($feedtype)
{
$this->feedtype = $feedtype;
return $this;
}
/**
* @return int
*/
public function getMachid()
{
return $this->machid;
}
/**
* @param int $machid
*
* @return $this
*/
public function setMachid($machid)
{
$this->machid = $machid;
return $this;
}
/**
* @return boolean
*/
public function isOnlinestatus()
{
return $this->onlinestatus;
}
/**
* @param boolean $onlinestatus
*
* @return $this
*/
public function setOnlinestatus($onlinestatus)
{
$this->onlinestatus = $onlinestatus;
return $this;
}
/**
* @return boolean
*/
public function isStatus()
{
return $this->status;
}
/**
* @param boolean $status
*
* @throws \Exception if invalid status is given
*
* @return $this
*/
public function setStatus($status)
{
if (in_array($status, self::$statuses)) {
$this->status = $status;
} else {
throw new \Exception("invalid status given");
}
return $this;
}
/**
* @return string
*/
public function getStorecode()
{
return $this->storecode;
}
/**
* @param string $storecode
*
* @return $this
*/
public function setStorecode($storecode)
{
$this->storecode = $storecode;
return $this;
}
/**
* @return \DateTime
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* @param \DateTime $timestamp
*
* @return $this
*/
public function setTimestamp($timestamp)
{
$this->timestamp = $timestamp;
return $this;
}
/**
* @return string
*/
public function getUid()
{
return $this->uid;
}
/**
* @param string $uid
*
* @return $this
*/
public function setUid($uid)
{
$this->uid = $uid;
return $this;
}
/**
* @return string
*/
public function getVersion()
{
return $this->version;
}
/**
* @param string $version
*
* @return $this
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get status
*
* @return boolean
*/
public function getStatus()
{
return $this->status;
}
/**
* Get onlinestatus
*
* @return boolean
*/
public function getOnlinestatus()
{
return $this->onlinestatus;
}
/**
* Set store
*
* @param \SimplySmart\UHDProBundle\Entity\Store $store
* @return Machine
*/
public function setStore(\SimplySmart\UHDProBundle\Entity\Store $store = null)
{
$this->store = $store;
return $this;
}
/**
* Get store
*
* @return \SimplySmart\UHDProBundle\Entity\Store
*/
public function getStore()
{
return $this->store;
}
/**
* Method to generate and set a new UID
*
* @return $this
*/
public function generateNewUid()
{
$date = new \DateTime("UTC");
$uid = "U";
$uid .= $date->format("U");
$uid .= 'R'.rand(0,100);
$this->setUid($uid);
return $this;
}
/**
* Add feeds
*
* @param \SimplySmart\UHDProBundle\Entity\Feed $feeds
* @return Machine
*/
public function addFeed(\SimplySmart\UHDProBundle\Entity\Feed $feeds)
{
$this->feeds[] = $feeds;
return $this;
}
/**
* Remove feeds
*
* @param \SimplySmart\UHDProBundle\Entity\Feed $feeds
*/
public function removeFeed(\SimplySmart\UHDProBundle\Entity\Feed $feeds)
{
$this->feeds->removeElement($feeds);
}
/**
* Get feeds
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getFeeds()
{
return $this->feeds;
}
}
我完全清楚在更新实体时我不需要使用$ em-> persist($ machine),但是删除它似乎没有任何区别。
真正磨练我的齿轮的部分是,当我进入探查器时,看起来它按预期运行所有查询!
答案 0 :(得分:0)
刚才意识到我是一个完全白痴,我的问题是我的实体中状态被设置为布尔字段,所以它正在更新,但始终将其设置为1!