Skip to content

Instantly share code, notes, and snippets.

@gpfiel
Created November 19, 2013 11:31
Show Gist options
  • Save gpfiel/7544054 to your computer and use it in GitHub Desktop.
Save gpfiel/7544054 to your computer and use it in GitHub Desktop.
Create Album has many Photos using Doctrine
<?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 $tiltle;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="main_photo", type="string", nullable=false)
*/
private $main_photo;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* @param integer
*
* @ORM\Column(name="status", type="integer")
*/
private $status;
/**
* @OneToMany(targetEntity="Photo", mappedBy="album_id")
* @var Photo[]
*/
protected $assignedPhotos = null;
public function __get($property)
{
return $this -> $property;
}
public function __set($property, $value)
{
$this -> $property = $value;
}
public function __construct()
{
$this->assignedPhotos = new ArrayCollection();
}
public function assignedToPhoto($photo)
{
$this->assignedPhotos[] = $photo;
}
/**
* Exchange array - used in ZF2 form
*
* @param array $data An array of data
*/
public function exchangeArray($data)
{
$this->id = (isset($data['id']))? $data['id'] : null;
$this->description = (isset($data['description']))? $data['description'] : null;
$this->tittle = (isset($data['tittle']))? $data['tittle'] : null;
$this->status = (isset($data['status']))? $data['status'] : null;
$this->main_photo = (isset($data['main_photo']))? $data['main_photo'] : null;
$this->date = (isset($data['date']))? $data['date'] : null;
}
/**
* Get an array copy of object
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars($this);
}
/**
* 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,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'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
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="main_photo", type="string", nullable=false)
*/
private $main_photo;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* Bidirectional - Many Photos are upload by one album (OWNING SIDE)
*
* @ManyToOne(targetEntity="Album", inversedBy="assignedPhotos")
*/
private $album_id;
public function __get($property)
{
return $this -> $property;
}
public function __set($property, $value)
{
$this -> $property = $value;
}
/**
* Exchange array - used in ZF2 form
*
* @param array $data An array of data
*/
public function exchangeArray($data)
{
$this->id = (isset($data['id']))? $data['id'] : null;
$this->description = (isset($data['description']))? $data['description'] : null;
$this->tittle = (isset($data['tittle']))? $data['tittle'] : null;
$this->main_photo = (isset($data['main_photo']))? $data['main_photo'] : null;
$this->date = (isset($data['date']))? $data['date'] : null;
$this->album_id = (isset($data['album_id']))? $data['album_id'] : null;
}
/**
* Get an array copy of object
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars($this);
}
/**
* 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