Skip to content

Instantly share code, notes, and snippets.

@ksn135
Created July 31, 2015 18:48
Show Gist options
  • Save ksn135/78ef2d5ae149c38259d5 to your computer and use it in GitHub Desktop.
Save ksn135/78ef2d5ae149c38259d5 to your computer and use it in GitHub Desktop.
Translatable entities example for symfony2admingenerator/GeneratorBundle#188
<?php
/*
* This file is part of the XXXXXXXXXXXXXXXXXXXXXXXX informational system package.
*
* (c) Serg N. Kalachev <serg@kalachev.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Entity\Translation\NewsItemTranslation;
use AppBundle\Traits\BlameableEntity;
use AppBundle\Traits\TimestampableEntity;
use AppBundle\Traits\SoftDeleteableEntity;
/**
* NewsItem
*
* @ORM\Table(name="news_item",options={"type"="InnoDB","charset"="utf8","collate"="utf8_general_ci"})
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\NewsItemRepository")
* @Gedmo\TranslationEntity(class="AppBundle\Entity\Translation\NewsItemTranslation")
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class NewsItem
{
/**
* Hook blameable behavior
* updates createdBy, updatedBy fields
*/
use BlameableEntity;
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* Hook softdeletable behavior
* updates deletedBy, deletedAt fields
*/
use SoftDeleteableEntity;
/**
* @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)
* @Gedmo\Translatable
* @Assert\NotBlank
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
* @Gedmo\Translatable
* @Assert\NotBlank
*/
private $content;
/**
* @var boolean
*
* @ORM\Column(name="published", type="boolean", options={"default":0})
*/
private $published;
/**
* @var \DateTime
*
* @ORM\Column(name="published_at", type="datetime", nullable=true)
* @Gedmo\Timestampable(on="change", field="published", value="1")
*/
private $publishedAt;
/**
* @var Employee $publishedBy
*
* @Gedmo\Blameable(on="change", field="published", value="1")
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee")
* @ORM\JoinColumn(name="published_by", referencedColumnName="id", nullable=true)
*/
private $publishedBy;
/**
* @var NewsCategory $category
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\NewsCategory")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* @Assert\NotNull
*/
private $category;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Translation\NewsItemTranslation", mappedBy="object", cascade={"persist", "remove"})
*/
protected $translations;
/**
* @Gedmo\Locale
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
private $locale;
public function __construct()
{
$this->translations = new ArrayCollection;
}
public function getLocale()
{
return $this->locale;
}
public function setLocale($locale)
{
$this->locale = $locale;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
public function getTranslations()
{
return $this->translations;
}
public function addTranslation(NewsItemTranslation $t)
{
$this->translations->add($t);
$t->setObject($this);
}
public function removeTranslation(NewsItemTranslation $t)
{
$this->translations->removeElement($t);
}
public function setTranslations($translations)
{
foreach ($translations as $translation) {
$translation->setObject($this);
}
$this->translations = $translations;
return $this;
}
public function __toString()
{
return $this->getTitle();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return NewsItem
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set subtitle
*
* @param string $subtitle
* @return NewsItem
*/
public function setSubtitle($subtitle)
{
$this->subtitle = $subtitle;
return $this;
}
/**
* Get subtitle
*
* @return string
*/
public function getSubtitle()
{
return $this->subtitle;
}
/**
* Set content
*
* @param string $content
* @return NewsItem
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set published
*
* @param boolean $published
* @return NewsItem
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* Get published
*
* @return boolean
*/
public function getPublished()
{
return $this->published;
}
/**
* Set publishedAt
*
* @param \DateTime $publishedAt
* @return NewsItem
*/
public function setPublishedAt($publishedAt)
{
$this->publishedAt = $publishedAt;
return $this;
}
/**
* Get publishedAt
*
* @return \DateTime
*/
public function getPublishedAt()
{
return $this->publishedAt;
}
/**
* Set category
*
* @param integer $category
* @return NewsItem
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return integer
*/
public function getCategory()
{
return $this->category;
}
/**
* Sets publishedBy.
*
* @param Employee $publishedBy
* @return $this
*/
public function setPublishedBy($publishedBy)
{
$this->publishedBy = $publishedBy;
return $this;
}
/**
* Returns publishedBy.
*
* @return Employee
*/
public function getPublishedBy()
{
return $this->publishedBy;
}
}
<?php
/*
* This file is part of the XXXXXXXXXXXXXXXXXXXXXXXX informational system package.
*
* (c) Serg N. Kalachev <serg@kalachev.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Entity\Translation;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity()
* @ORM\Table(name="news_item_translations",
* options={"type"="InnoDB","charset"="utf8","collate"="utf8_general_ci"},
* uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
* "locale", "object_id", "field"
* })}
* )
*/
class NewsItemTranslation extends AbstractPersonalTranslation
{
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\NewsItem", inversedBy="translations")
* @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment