<?php
namespace Raltech\WarehouseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Entity
* @ORM\Table(name="warehouse_item")
* @ORM\HasLifecycleCallbacks
*/
class Item
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $codepath;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $filepath;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="items")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id",onDelete="SET NULL")
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity="Raltech\UserBundle\Entity\User", inversedBy="itemreserved")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id",onDelete="SET NULL")
*/
private $reservedby;
/**
* @Assert\File(maxSize="51200k",
* maxSizeMessage = "Plik ma więcej niż 50 megabajtów",
* mimeTypes = {
* "image/png",
* "image/jpeg",
* "image/jpg",
* "image/gif",
* "application/pdf",
* "application/x-pdf",
* "application/msword",
* "application/zip",
* "application/x-rar-compressed",
* "application/excel"
* },
* mimeTypesMessage = "Zły format!"
* )
*/
private $file;
/**
* @var $removefile if set, remove file
*/
private $removefile;
private $temp;
/**
* Constructor
*/
public function __construct()
{
$this->category = new \Doctrine\Common\Collections\ArrayCollection();
$this->resservedby = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Item
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return Item
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set codepath
*
* @param string $codepath
* @return Item
*/
public function setCodepath($codepath)
{
$this->codepath = $codepath;
return $this;
}
/**
* Get codepath
*
* @return string
*/
public function getCodepath()
{
return $this->codepath;
}
/**
* Set filepath
*
* @param string $filepath
* @return Item
*/
public function setFilepath($filepath)
{
$this->filepath = $filepath;
return $this;
}
/**
* Get filepath
*
* @return string
*/
public function getFilepath()
{
return $this->filepath;
}
/**
* Add category
*
* @param \Raltech\WarehouseBundle\Entity\Category $category
* @return Item
*/
public function addCategory(\Raltech\WarehouseBundle\Entity\Category $category)
{
$this->category[] = $category;
return $this;
}
/**
* Remove category
*
* @param \Raltech\WarehouseBundle\Entity\Category $category
*/
public function removeCategory(\Raltech\WarehouseBundle\Entity\Category $category)
{
$this->category->removeElement($category);
}
/**
* Get category
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategory()
{
return $this->category;
}
/**
* Add resservedby
*
* @param \Raltech\UserBundle\Entity\User $resservedby
* @return Item
*/
public function addResservedby(\Raltech\UserBundle\Entity\User $resservedby)
{
$this->resservedby[] = $resservedby;
return $this;
}
/**
* Remove resservedby
*
* @param \Raltech\UserBundle\Entity\User $resservedby
*/
public function removeResservedby(\Raltech\UserBundle\Entity\User $resservedby)
{
$this->resservedby->removeElement($resservedby);
}
/**
* Get resservedby
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getResservedby()
{
return $this->resservedby;
}
/**
* Set category
*
* @param \Raltech\WarehouseBundle\Entity\Category $category
* @return Item
*/
public function setCategory(\Raltech\WarehouseBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Set resservedby
*
* @param \Raltech\UserBundle\Entity\User $resservedby
* @return Item
*/
public function setResservedby(\Raltech\UserBundle\Entity\User $resservedby = null)
{
$this->resservedby = $resservedby;
return $this;
}
/**
* Add reservedby
*
* @param \Raltech\UserBundle\Entity\User $reservedby
* @return Item
*/
public function addReservedby(\Raltech\UserBundle\Entity\User $reservedby)
{
$this->reservedby[] = $reservedby;
return $this;
}
/**
* Remove reservedby
*
* @param \Raltech\UserBundle\Entity\User $reservedby
*/
public function removeReservedby(\Raltech\UserBundle\Entity\User $reservedby)
{
$this->reservedby->removeElement($reservedby);
}
/**
* Get reservedby
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getReservedby()
{
return $this->reservedby;
}
public function getAbsolutePath()
{
return null === $this->filepath
? null
: $this->getUploadRootDir().'/'.$this->filepath;
}
public function getWebPath()
{
return null === $this->filepath
? null
: $this->getUploadDir().''.$this->filepath;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/itemsdata/';
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
if (isset($this->file)) {
// store the old name to delete after the update
$this->temp = $this->filepath;
$this->filepath = null;
} else {
$this->filepath = 'initial';
}
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// set the path property to the filename where you've saved the file
$this->filepath = sha1(uniqid(mt_rand(), true)).'-'.$this->getFile()->getClientOriginalName();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// move takes the target directory and then the
// target filename to move to
$this->getFile()->move(
$this->getUploadRootDir(),
$this->filepath
);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir().'/'.$this->temp);
// clear the temp image path
$this->temp = null;
}
// clean up the file property as you won't need it anymore
$this->file = null;
}
/**
* Get removefile
*
* @return string
*/
public function getRemovefile()
{
return $this->removefile;
}
/**
* set removefile (remove file)
*
* @return string
*/
public function setRemovefile($removefile = null)
{
$this->removefile = $removefile;
if($removefile) {
$path = $this->getAbsolutePath();
if (file_exists($path)) {
unlink($path);
$this->filepath = null;
}
}
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
$file = $this->getAbsolutePath();
if ($file) {
unlink($file);
}
}
/**
* Set reservedby
*
* @param \Raltech\UserBundle\Entity\User $reservedby
* @return Item
*/
public function setReservedby(\Raltech\UserBundle\Entity\User $reservedby = null)
{
$this->reservedby = $reservedby;
return $this;
}
}
我的实体, 函数上传应该在PostCreate上触发(这很好)和PostUpdate但是在更新时不会触发。我做错了什么?更新是coockbook(获得项目,持久,同花顺)。