Skip to content

Instantly share code, notes, and snippets.

@Koc
Created October 22, 2011 17:53
Show Gist options
  • Save Koc/1306277 to your computer and use it in GitHub Desktop.
Save Koc/1306277 to your computer and use it in GitHub Desktop.
<?php
namespace Ololo\Bundle\BadgesBundle\Entity;
use Doctrine\ORM\EntityRepository;
class BadgeRepository extends EntityRepository
{
public function getInfoAboutBadge($id, $locale)
{
$badge = $this->_em->createQuery('
SELECT b, bt
FROM OloloBadgesBundle:Badge b
LEFT JOIN b.translations bt WITH bt.locale = :locale
WHERE b = :id
')
->setParameter('locale', $locale)
->setParameter('id', $id)
->getOneOrNullResult();
$badge->setActiveLocale($locale);
return $badge;
}
public function getBadgesByUser($user, $locale, $perPage = null, $skip = null)
{
$owners = $this->_em->createQuery('
SELECT o
FROM OloloBadgesBundle:BadgeOwner o
JOIN o.badge ob
LEFT JOIN ob.translations bt WITH bt.locale = :locale
WHERE o.user = :user
ORDER BY o.ownedAt DESC
')
->setParameter('locale', $locale)
->setParameter('user', $user)
->setFirstResult($skip)
->setMaxResults($perPage)
->getResult();
$badges = array();
foreach ($owners as $owner) {
$badge = $owner->getBadge();
$badge->setActiveLocale($locale);
$badges[] = $badge;
}
return $badges;
}
public function getBadges($locale, $perPage = null, $skip = null)
{
$badges = $this->_em->createQuery('
SELECT b, bt
FROM OloloBadgesBundle:Badge b
LEFT JOIN b.translations bt WITH bt.locale = :locale
ORDER BY b.id ASC
')
->setParameter('locale', $locale)
->setFirstResult($skip)
->setMaxResults($perPage)
->getResult();
foreach ($badges as $badge) {
$badge->setActiveLocale($locale);
}
return $badges;
}
}
<?php
namespace Ololo\Bundle\BadgesBundle\Entity;
use Doctrine\ORM\Mapping as Orm;
/**
* @Orm\Entity
* @Orm\Table(name="badge_translation")
*/
class BadgeTranslation
{
/**
* @Orm\Id
* @Orm\GeneratedValue
* @Orm\Column(type="integer")
*/
protected $id;
/**
* @Orm\ManyToOne(targetEntity="Badge", inversedBy="translations")
* @Orm\JoinColumn(name="badge_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
protected $badge;
/**
* @Orm\Column(length=5)
*/
protected $locale;
/**
* @Orm\Column(length=50, nullable=true)
*/
protected $title;
/**
* @Orm\Column(type="text", nullable=true)
*/
protected $description;
/**
* @Orm\Column(type="text", nullable=true, name="description_present")
*/
protected $descriptionPresent;
public function getId()
{
return $this->id;
}
public function getBadge()
{
return $this->badge;
}
public function setBadge(Badge $badge)
{
$this->badge = $badge;
}
public function getLocale()
{
return $this->locale;
}
public function setLocale($locale)
{
$this->locale = $locale;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getDescriptionPresent()
{
return $this->descriptionPresent;
}
public function setDescriptionPresent($descriptionPresent)
{
$this->descriptionPresent = $descriptionPresent;
}
}
<?php
namespace Ololo\Bundle\BadgesBundle\Entity;
use Doctrine\ORM\Mapping as Orm;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Orm\Entity(repositoryClass="Ololo\Bundle\BadgesBundle\Entity\BadgeRepository")
* @Orm\Table(name="badge")
* @Orm\HasLifecycleCallbacks
*/
class Badge
{
/**
* @Orm\Id
* @Orm\GeneratedValue
* @Orm\Column(type="integer")
*/
protected $id;
/**
* @Orm\Column(length=50)
* @Assert\NotBlank()
*/
private $title;
/**
* @Orm\Column(type="text")
* @Assert\NotBlank()
*/
private $description;
/**
* @Orm\Column(type="text", name="description_present")
*/
private $descriptionPresent;
/**
* @Orm\OneToMany(targetEntity="BadgeTranslation", cascade={"persist"}, mappedBy="badge", indexBy="locale")
*/
protected $translations;
/**
* @Orm\Column(length=50)
*/
protected $slug = '';
/**
* @Orm\Column(type="text")
* @Assert\NotBlank()
*/
protected $conditions;
/**
* @Orm\OneToMany(targetEntity="BadgeTerm", mappedBy="badge", cascade={"persist"})
*/
protected $terms;
/**
* @Orm\Column(type="datetime", name="created_at", nullable=false)
*/
protected $createdAt;
/**
* @Orm\Column(type="datetime", name="updated_at", nullable=false)
*/
protected $updatedAt;
/**
* @Assert\Image()
*/
protected $imageColor;
/**
* @Assert\Image()
*/
protected $imageGrey;
/**
* @Assert\Image()
*/
protected $imagePresent;
/**
* @Orm\Column(type="string", nullable=true, name="image_color_name")
*/
protected $imageColorName;
/**
* @Orm\Column(type="string", nullable=true, name="image_grey_name")
*/
protected $imageGreyName;
/**
* @Orm\Column(type="string", nullable=true, name="image_present_name")
*/
protected $imagePresentName;
protected $activeLocale;
public function __construct()
{
$this->terms = new ArrayCollection();
$this->translations = new ArrayCollection();
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
/**
* @Orm\PreUpdate
* Не отрабатывает при редактировании, при котором изменяется только изображение.
*/
public function updateUpdatedAt()
{
$this->updatedAt = new \DateTime();
}
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function getTitleTranslated()
{
if (isset($this->translations[$this->activeLocale])) {
$title = $this->translations[$this->activeLocale]->getTitle();
return isset($title) ? $title : $this->title;
}
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getDescription()
{
return $this->description;
}
public function getDescriptionTranslated()
{
if (isset($this->translations[$this->activeLocale])) {
$description = $this->translations[$this->activeLocale]->getDescription();
return isset($description) ? $description : $this->description;
}
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getDescriptionPresent()
{
return $this->descriptionPresent;
}
public function getDescriptionPresentTranslated()
{
if (isset($this->translations[$this->activeLocale])) {
$descriptionPresent = $this->translations[$this->activeLocale]->getDescriptionPresent();
return isset($descriptionPresent) ? $descriptionPresent : $this->descriptionPresent;
}
return $this->description;
}
public function setDescriptionPresent($descriptionPresent)
{
$this->descriptionPresent = $descriptionPresent;
}
public function getTranslations()
{
return $this->translations;
}
public function addTranslation(BadgeTranslation $translation)
{
$translation->setBadge($this);
$this->translations[$translation->getLocale()] = $translation;
}
public function removeTranslation(BadgeTranslation $translation)
{
$this->translations->removeElement($translation);
}
public function getSlug()
{
return $this->slug;
}
public function setSlug($slug)
{
$this->slug = $slug;
}
public function getConditions()
{
return $this->conditions;
}
public function setConditions($conditions)
{
$this->conditions = $conditions;
}
public function getImageGrey()
{
return $this->imageGrey;
}
public function setImageGrey($imageGrey)
{
$this->imageGrey = $imageGrey;
}
public function getImageColor()
{
return $this->imageColor;
}
public function setImageColor($imageColor)
{
$this->imageColor = $imageColor;
}
public function getImageColorName()
{
return $this->imageColorName;
}
public function getImageGreyName()
{
return $this->imageGreyName;
}
public function setImageColorName($imageColorName)
{
$this->imageColorName = $imageColorName;
}
public function setImageGreyName($imageGreyName)
{
$this->imageGreyName = $imageGreyName;
}
public function getImagePresent()
{
return $this->imagePresent;
}
public function setImagePresent($imagePresent)
{
$this->imagePresent = $imagePresent;
}
public function getImagePresentName()
{
return $this->imagePresentName;
}
public function setImagePresentName($imagePresentName)
{
$this->imagePresentName = $imagePresentName;
}
/**
*
* @return collection
*/
public function getTerms()
{
return $this->terms;
}
public function addTerm(BadgeTerm $term)
{
$term->setBadge($this);
$this->terms->add($term);
}
public function removeTerm(BadgeTerm $term)
{
$this->terms->removeElement($term);
}
public function getActiveLocale()
{
return $this->activeLocale;
}
public function setActiveLocale($activeLocale)
{
$this->activeLocale = $activeLocale;
}
/**
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment