Skip to content

Instantly share code, notes, and snippets.

@gpfiel
Created November 21, 2013 16:18
Show Gist options
  • Save gpfiel/7584714 to your computer and use it in GitHub Desktop.
Save gpfiel/7584714 to your computer and use it in GitHub Desktop.
<?php
namespace Admin\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\Common\Collections\ArrayCollection;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
* Album
*
* @ORM\Table(name="bnc_album")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Album implements InputFilterAwareInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", nullable=false)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="mainPhoto", type="string", nullable=true)
*/
private $mainPhoto;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* @param integer
*
* @ORM\Column(name="status", type="integer")
*/
private $status;
/**
* @ORM\OneToMany(targetEntity="Photo", mappedBy="album")
* @var Photo[]
*/
protected $assignedPhotos = null;
protected $inputFilter;
public function __construct()
{
$this->assignedPhotos = new ArrayCollection();
}
###################
# GET AND SETTERS #
###################
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
return $this;
}
public function getMainPhoto()
{
return $this->mainPhoto;
}
public function setMainPhoto($mainPhoto)
{
$this->mainPhoto = $mainPhoto;
return $this;
}
public function getDate()
{
return $this->date;
}
public function setDate($date)
{
$this->date = $date;
return $this;
}
public function getStatus()
{
return $this->status;
}
public function setStatus($status)
{
$this->status = $status;
return $this;
}
public function assignedToPhoto($photo)
{
$this->assignedPhotos[] = $photo;
}
public function getAssignedPhotos()
{
return $this->assignedPhotos;
}
/**
* Set input method
*
* @param InputFilterInterface $inputFilter
*/
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
/**
* Get input filter
*
* @return InputFilterInterface
*/
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => false,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'status',
'required' => false,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'description',
'required' => false,
'filters' => array(
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 255,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'date',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 255,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'mainPhoto',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 255,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
<?php
$album = new Album;
$photo = new Photo;
if(isset($_POST['id']))
$album = $this->getEntityManager()->getRepository('Admin\Entity\Album')->find($this -> params() -> fromPost('id'));
$photo -> setDate(date_create($_POST['date_photo']));
$photo -> setDescription($_POST['description_photo']);
$photo -> setMainPhoto($novo_nome);
$album -> assignedToPhoto($photo);
if(!isset($_POST['id']))
{
$album -> setTitle("Album incompleto.");
$album -> setStatus(2);
}
$em = $this->getEntityManager();
$em -> persist($album);
$em -> persist($photo);
$em -> flush();
$id_album = (isset($_POST['id']) ? $_POST['id'] : $album -> getId());
$id_photo = $photo -> getId();
<?php
namespace Admin\Entity;
use Doctrine\ORM\Mapping as ORM;
// Doctrine\Common\Collections\ArrayCollection;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
* Photo
*
* @ORM\Table(name="bnc_photo")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Photo implements InputFilterAwareInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="mainPhoto", type="string", nullable=false)
*/
private $mainPhoto;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* Bidirectional - Many Photos are upload by one album (OWNING SIDE)
*
* @ORM\ManyToOne(targetEntity="Album", inversedBy="assignedPhotos")
*/
private $album;
public function __get($property)
{
return $this -> $property;
}
public function __set($property, $value)
{
$this -> $property = $value;
}
//GET AND SETTERS
public function getId()
{
return $this->id;
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getMainPhoto()
{
return $this->mainPhoto;
}
public function setMainPhoto($mainPhoto)
{
$this->mainPhoto = $mainPhoto;
}
public function getDate()
{
return $this->date;
}
public function setDate($date)
{
$this->date = $date;
}
public function setAlbum($album)
{
$album->assignedToPhoto($this);
$this->album = $album;
}
public function getAlbum()
{
return $this->album;
}
/**
* Set input method
*
* @param InputFilterInterface $inputFilter
*/
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
/**
* Get input filter
*
* @return InputFilterInterface
*/
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => false,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'description',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment