簡體   English   中英

Symfony2 + Doctrine2在我的數據庫中未更新

[英]Symfony2 + Doctrine2 not updating in my database

我在symfony2中有一個api,在其中沒有添加和刪除項的問題,但是,更新不會引發任何錯誤,但是數據庫中的relevent行不會得到更新!

我的控制器方法:

/*
 * @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),但是刪除它似乎沒有任何區別。

真正磨合我的齒輪的部分是,當我進行探查器時,它按預期運行了所有查詢!

剛剛意識到我一直是個白痴,我的問題是狀態在我的實體中設置為布爾值字段,因此它正在更新,但始終將其設置為1!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM