Symfony2 + Doctrine2在我的数据库中没有更新 [英] Symfony2 + Doctrine2 not updating in my database

查看:155
本文介绍了Symfony2 + Doctrine2在我的数据库中没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在symfony2中有一个api,其中添加和删除项目不是问题,但是更新不会给我任何错误,但是我的数据库中的相关行没有被更新!



我的控制器方法:

  / * 
* @Route(/ complete / uid / {storeUid},
* name =media_complete
*)
*
* @Method({GET})
*
* @param String $ storeUid - 已完成下载的商店的uid
*
* @return响应
* /
public function downloadCompleteAction($ storeUid)
{

$ response = $ this-> getNewJsonResponse();
$ em = $ this-> getDoctrine() - > getManager();
$ repo = $ em-> getRepository(SimplySmartUHDProBundle:Machine);

尝试{
//从db获取机器
$ machine = $ repo-> findOneBy(array(
uid=> $ storeUid
));

//将其设置为下载完成
$ machine-> setStatus(Machine :: STATUS_DOWNLOAD_COMPLETE);

$ em-> persist($ machine);
$ em-> flush();

$ response-> setStatusCode(200);
} catch(\Exception $ e){
//如果有错误,捕获异常将状态设置为500,并返回具有异常错误的json
$ error = array message=> $ e-> getMessage());
$ response-> setContent(json_encode($ error));
$ response-> setStatusCode(500);
}

return $ response;
}

我的实体:


 命名空间SimplySmart\UHDProBundle\Entity; 

使用Doctrine\Common\Collections\ArrayCollection;
使用Doctrine\ORM\Mapping作为ORM;
使用SimplySmart\UHDProBundle\Entity\Store;

/ **
*机器
*
* @ 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;

/ **
*可用状态数组
*
* @var数组
* /
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,可空= 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整数
*
* @ 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 $这 - >描述;
}

/ **
* @param string $ description
*
* @return $ this
* /
public function setDescription($ description)
{
$ this-> description = $ description;

return $ this;
}

/ **
* @return string
* /
public function getFeedtype()
{
return $这 - >的FeedType;
}

/ **
* @param string $ feedtype
*
* @return $ this
* /
public function setFeedtype($ feedtype)
{
$ this-> feedtype = $ feedtype;

return $ this;
}

/ **
* @return int
* /
public function getMachid()
{
return $这 - > machid;
}

/ **
* @param int $ machid
*
* @return $ this
* /
public function setMachid($ machid)
{
$ this-> machid = $ machid;

return $ this;
}

/ **
* @return boolean
* /
public function isOnlinestatus()
{
return $这 - > onlinestatus;
}

/ **
* @param boolean $ onlinestatus
*
* @return $ this
* /
public function setOnlinestatus($ onlinestatus)
{
$ this-> onlinestatus = $ onlinestatus;

return $ this;
}

/ **
* @return boolean
* /
public function isStatus()
{
return $这 - >状态;
}

/ **
* @param boolean $ status
*
* @throws \Exception如果无效状态为
*
* @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
* /
公共函数getStorecode()
{
return $这 - >商店代码;
}

/ **
* @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 $这 - > UID;
}

/ **
* @param string $ uid
*
* @return $ this
* /
public function setUid($ uid)
{
$ this-> uid = $ uid;

return $ this;
}

/ **
* @return string
* /
public function getVersion()
{
return $这 - >版本;
}

/ **
* @param string $ version
*
* @return $ this
* /
public function setVersion($ version)
{
$ this-> version = $ version;

return $ this;
}


/ **
*获取状态
*
* @return boolean
* /
public function getStatus()
{
return $ this-> status;
}

/ **
*获取onlinestatus
*
* @return boolean
* /
public function getOnlinestatus )
{
return $ this-> onlinestatus;
}

/ **
*设置商店
*
* @param \SimplySmart\UHDProBundle\Entity\Store $ store
* @return Machine
* /
public function setStore(\SimplySmart\UHDProBundle\Entity\Store $ store = null)
{
$ this- > store = $ store;

return $ this;
}

/ **
*获取商店
*
* @return \SimplySmart\UHDProBundle\Entity\Store
* /
public function getStore()
{
return $ this-> store;
}

/ **
*生成和设置新的UID的方法
*
* @return $ this
* /
public function generateNewUid()
{
$ date = new \DateTime(UTC);

$ uid =U;
$ uid。= $ date->格式(U);
$ uid。='R'.rand(0,100);

$ this-> setUid($ uid);

return $ this;
}

/ **
*添加Feed
*
* @param \SimplySmart\UHDProBundle\Entity\Feed $ feeds
* @return Machine
* /
public function addFeed(\SimplySmart\UHDProBundle\Entity\Feed $ feeds)
{
$ this-> feeds [] = $ feeds;

return $ this;
}

/ **
*删除Feed
*
* @param \SimplySmart\UHDProBundle\Entity\Feed $ feeds
* /
public function removeFeed(\SimplySmart\UHDProBundle\Entity\Feed $ feeds)
{
$ this-> feeds-> removeElement($ feeds );
}

/ **
*获取Feed
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getFeeds()
{
return $ this-> feeds;
}
}

我完全意识到,更新一个实体时,我应该不需要使用$ em-> persist($ machine),但是删除它似乎没有任何区别。



真正磨削我的齿轮的部分是,当我进行分析器时,看起来它像预期的那样运行所有的查询!

解决方案

只是意识到我一直是一个完整的白痴,我的问题是,状态被设置为一个布尔字段在我的实体,所以它正在更新,但总是将其设置为1!


I have an api in symfony2, in which adding and deleting items is not issue, however, updating doesn't throw me any errors however the relevent row in my database does not get updated!

My Controller method:

/*
 * @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;
}

My Entity:

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;
    }
}

I am fully aware that when updating an entity I shouldn't need to use $em->persist($machine), however removing this doesn't seem to make any difference.

The part that is really grinding my gears, is that when I go on the profiler, it appears as it is ran all the queries as expected!

解决方案

Just realised that I've been a complete idiot and my issue is that status is set as a boolean field in my entity, so it was updating, but always setting it as 1!

这篇关于Symfony2 + Doctrine2在我的数据库中没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆