Skip to content

Instantly share code, notes, and snippets.

Created May 19, 2014 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7b52fe7f18459bb03a51 to your computer and use it in GitHub Desktop.
Save anonymous/7b52fe7f18459bb03a51 to your computer and use it in GitHub Desktop.
Book Model
<?php
namespace Matiit\Bundle\BookBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Matiit\Bundle\BookBundle\Form\Type\BookCoverType;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Book
*
* @ORM\Table()
* @ORM\Entity
*/
class Book
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var integer
*
* @ORM\Column(name="length", type="integer")
*/
private $length;
/**
* @var integer
*
* @ORM\Column(name="category_id", type="integer")
*/
private $categoryId;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=512)
*/
private $path;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="books")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* @ORM\OneToMany(targetEntity="BookCover", mappedBy="book")
*/
protected $covers;
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/books';
}
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
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;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Book
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set length
*
* @param integer $length
* @return Book
*/
public function setLength($length)
{
$this->length = $length;
return $this;
}
/**
* Get length
*
* @return integer
*/
public function getLength()
{
return $this->length;
}
/**
* Set categoryId
*
* @param integer $categoryId
* @return Book
*/
public function setCategoryId($categoryId)
{
$this->categoryId = $categoryId;
return $this;
}
/**
* Get categoryId
*
* @return integer
*/
public function getCategoryId()
{
return $this->categoryId;
}
/**
* Set path
*
* @param string $path
* @return Book
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set category
*
* @param \Matiit\Bundle\BookBundle\Entity\Category $category
* @return Book
*/
public function setCategory(\Matiit\Bundle\BookBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return \Matiit\Bundle\BookBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
/**
* @return \Matiit\Bundle\BookBundle\Entity\BookCover
*/
public function getCovers() {
return $this->covers;
}
public function setCovers(ArrayCollection $covers) {
$this->covers = $covers;
}
public function addCover(BookCoverType $cover) {
$this->covers[] = $cover;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment