Created
May 19, 2014 19:59
-
-
Save anonymous/72282c8467a9c1232d2f to your computer and use it in GitHub Desktop.
BookCover Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Matiit\Bundle\BookBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\HttpFoundation\File\UploadedFile; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* BookCover | |
* | |
* @ORM\Table() | |
* @ORM\Entity(repositoryClass="Matiit\Bundle\BookBundle\Entity\BookCoverRepository") | |
* @ORM\HasLifecycleCallbacks | |
*/ | |
class BookCover | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="path", type="string", length=512) | |
*/ | |
private $path; | |
/** | |
* @Assert\File(maxSize="6000000") | |
*/ | |
private $file; | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="book_id", type="integer") | |
*/ | |
private $bookId; | |
/** | |
* @ORM\ManyToOne(targetEntity="Book", inversedBy="covers") | |
* @ORM\JoinColumn(name="book_id", referencedColumnName="id") | |
*/ | |
private $book; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set path | |
* | |
* @param string $path | |
* @return BookCover | |
*/ | |
public function setPath($path) | |
{ | |
$this->path = $path; | |
return $this; | |
} | |
/** | |
* Get path | |
* | |
* @return string | |
*/ | |
public function getPath() | |
{ | |
return $this->path; | |
} | |
/** | |
* Set bookId | |
* | |
* @param integer $bookId | |
* @return BookCover | |
*/ | |
public function setBookId($bookId) | |
{ | |
$this->bookId = $bookId; | |
return $this; | |
} | |
/** | |
* Get bookId | |
* | |
* @return integer | |
*/ | |
public function getBookId() | |
{ | |
return $this->bookId; | |
} | |
/** | |
* Get file | |
* | |
* @return string | |
*/ | |
public function getFile() | |
{ | |
return $this->file; | |
} | |
/** | |
* Sets file. | |
* | |
* @param UploadedFile $file | |
*/ | |
public function setFile(UploadedFile $file = null) | |
{ | |
$this->file = $file; | |
} | |
/** | |
* @return \Matiit\Bundle\BookBundle\Book | |
*/ | |
public function getBook() { | |
return $this->book; | |
} | |
/** | |
* @param \Matiit\Bundle\BookBundle\Entity\Book $book | |
*/ | |
public function setBook(Book $book) { | |
$this->book = $book; | |
} | |
/** | |
* Called before entity removal | |
* | |
* @ORM\PostRemove() | |
*/ | |
public function removeUpload() | |
{ | |
if ($file = $this->getAbsolutePath()) { | |
unlink($file); | |
} | |
} | |
/** | |
* Called after entity persistence | |
* | |
* @ORM\PostPersist() | |
* @ORM\PostUpdate() | |
*/ | |
public function upload() | |
{ | |
// the file property can be empty if the field is not required | |
if (null === $this->getFile()) { | |
return; | |
} | |
// use the original file name here but you should | |
// sanitize it at least to avoid any security issues | |
// move takes the target directory and then the | |
// target filename to move to | |
$this->getFile()->move( | |
$this->getUploadRootDir(), | |
$this->getFile()->getClientOriginalName() | |
); | |
// set the path property to the filename where you've saved the file | |
$this->path = $this->getFile()->getClientOriginalName(); | |
// clean up the file property as you won't need it anymore | |
$this->file = null; | |
} | |
public function getAbsolutePath() | |
{ | |
return null === $this->path | |
? null | |
: $this->getUploadRootDir().'/'.$this->path; | |
} | |
public function getWebPath() | |
{ | |
return null === $this->path | |
? null | |
: $this->getUploadDir().'/'.$this->path; | |
} | |
protected function getUploadRootDir() | |
{ | |
// the absolute directory path where uploaded | |
// documents should be saved | |
return __DIR__.'/../../../../../web/'.$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/covers'; | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment