简体   繁体   中英

Symfony2 + Doctrine2 not updating in my database

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!

刚刚意识到我一直是个白痴,我的问题是状态在我的实体中设置为布尔值字段,因此它正在更新,但始终将其设置为1!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM