Skip to content

Instantly share code, notes, and snippets.

@fdonzello
Created October 14, 2012 08:28
Show Gist options
  • Save fdonzello/3887959 to your computer and use it in GitHub Desktop.
Save fdonzello/3887959 to your computer and use it in GitHub Desktop.
STI
/**
* Fm\DocumentBundle\Entity\Document
*
* @ORM\Table()
* @ORM\Entity
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"playerList" = "PlayerList", "playerPrice" = "PlayerPrice", "avatar" = "Avatar", "playerPhoto" = "PlayerPhoto", "teamPhoto" = "TeamPhoto"})
* @ORM\HasLifecycleCallbacks
*/
abstract class Document
{
const AVATAR = 'avatar';
const PLAYER_LIST = 'playerList';
const PLAYER_PRICE = 'playerPrice';
const PLAYER_PHOTO = 'playerPhoto';
const TEAM_PHOTO = 'teamPhoto';
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @Assert\NotBlank
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string $path
*
* @ORM\Column(name="path", type="string", length=255, nullable=true)
*/
private $path;
/**
* @Assert\File(maxSize="6000000")
*/
protected $file;
abstract public function getType();
}
/**
* Fm\DocumentBundle\Entity\PlayerPhoto
*
* @ORM\Entity
*/
class PlayerPhoto extends Document
{
/**
* @Assert\File(
* maxSize="6000000",
* mimeTypes = {"image/jpeg", "image/png", "image/gif"},
* mimeTypesMessage = "form.field.invalidFormat"
* )
*/
protected $file;
protected function getUploadDir()
{
return 'uploads/playerPhotos';
}
public function getType()
{
return 'PlayerPhoto';
}
}
/**
* Fm\DocumentBundle\Entity\TeamPhoto
*
* @ORM\Entity
*/
class TeamPhoto extends Document
{
/**
* @Assert\File(
* maxSize="6000000",
* mimeTypes = {"image/jpeg", "image/png", "image/gif"},
* mimeTypesMessage = "form.field.invalidFormat"
* )
*/
protected $file;
protected function getUploadDir()
{
return 'uploads/teamPhotos';
}
public function getType()
{
return 'TeamPhoto';
}
}
//controller Team
public function setPhotoAction($id, $document_id)
{
$documentManager = $this->get('fm.manager.document');
$teamManager = $this->get('fm.manager.team');
$document = $documentManager->findDocumentById((int) $document_id);
$team = $teamManager->findTeamById((int) $id);
$teamManager->setPhoto($team, $document);
... ecc..
}
// team manager
/**
* Set the team's photo.
*
* @param Team $team
* @param TeamPhoto $photo
*/
public function setPhoto(Team $team, TeamPhoto $photo)
{
if (null === $team) {
throw new InvalidArgumentException('Team is null');
}
if (null === $photo) {
throw new InvalidArgumentException('Photo is null');
}
$team->setPhoto($photo);
$this->em->persist($team);
$this->em->flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment