Permalink
Browse files

[translatable] started implementation f personal translations for mon…

…go ODM
  • Loading branch information...
1 parent 6ac6ffc commit 7052ee35760c27dc9960ca7c7b593aadf6f5508b @l3pp4rd l3pp4rd committed Jan 24, 2012
@@ -0,0 +1,147 @@
+<?php
+
+namespace Gedmo\Translatable\Document;
+
+use Doctrine\ODM\MongoDB\Mapping\Annotations\MappedSuperclass;
+use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
+use Doctrine\ODM\MongoDB\Mapping\Annotations\String;
+
+/**
+ * Gedmo\Translatable\Document\AbstractPersonalTranslation
+ *
+ * @MappedSuperclass
+ */
+abstract class AbstractPersonalTranslation
+{
+ /**
+ * @var integer $id
+ *
+ * @Id
+ */
+ protected $id;
+
+ /**
+ * @var string $locale
+ *
+ * @String
+ */
+ protected $locale;
+
+ /**
+ * Related entity with ManyToOne relation
+ * must be mapped by user
+ */
+ protected $object;
+
+ /**
+ * @var string $field
+ *
+ * @String
+ */
+ protected $field;
+
+ /**
+ * @var text $content
+ *
+ * @String
+ */
+ protected $content;
+
+ /**
+ * Get id
+ *
+ * @return integer $id
+ */
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ /**
+ * Set locale
+ *
+ * @param string $locale
+ * @return AbstractPersonalTranslation
+ */
+ public function setLocale($locale)
+ {
+ $this->locale = $locale;
+ return $this;
+ }
+
+ /**
+ * Get locale
+ *
+ * @return string $locale
+ */
+ public function getLocale()
+ {
+ return $this->locale;
+ }
+
+ /**
+ * Set field
+ *
+ * @param string $field
+ * @return AbstractPersonalTranslation
+ */
+ public function setField($field)
+ {
+ $this->field = $field;
+ return $this;
+ }
+
+ /**
+ * Get field
+ *
+ * @return string $field
+ */
+ public function getField()
+ {
+ return $this->field;
+ }
+
+ /**
+ * Set object related
+ *
+ * @param object $object
+ * @return AbstractPersonalTranslation
+ */
+ public function setObject($object)
+ {
+ $this->object = $object;
+ return $this;
+ }
+
+ /**
+ * Get object related
+ *
+ * @return string $object
+ */
+ public function getObject()
+ {
+ return $this->object;
+ }
+
+ /**
+ * Set content
+ *
+ * @param string $content
+ * @return AbstractPersonalTranslation
+ */
+ public function setContent($content)
+ {
+ $this->content = $content;
+ return $this;
+ }
+
+ /**
+ * Get content
+ *
+ * @return string $content
+ */
+ public function getContent()
+ {
+ return $this->content;
+ }
+}
@@ -129,7 +129,7 @@ public function getObject()
/**
* Set content
*
- * @param text $content
+ * @param string $content
* @return AbstractTranslation
*/
public function setContent($content)
@@ -141,7 +141,7 @@ public function setContent($content)
/**
* Get content
*
- * @return text $content
+ * @return string $content
*/
public function getContent()
{
@@ -22,6 +22,19 @@
*/
final class ODM extends BaseAdapterODM implements TranslatableAdapter
{
+ /**
+ * {@inheritDoc}
+ */
+ public function usesPersonalTranslation($translationClassName)
+ {
+ return $this
+ ->getObjectManager()
+ ->getClassMetadata($translationClassName)
+ ->getReflectionClass()
+ ->isSubclassOf('Gedmo\Translatable\Document\AbstractPersonalTranslation')
+ ;
+ }
+
/**
* {@inheritDoc}
*/
@@ -37,57 +50,104 @@ public function loadTranslations($object, $translationClass, $locale)
{
$dm = $this->getObjectManager();
$wrapped = AbstractWrapper::wrapp($object, $dm);
+ $result = array();
- // load translated content for all translatable fields
- $identifier = $wrapped->getIdentifier();
- // construct query
- $qb = $dm->createQueryBuilder($translationClass);
- $q = $qb->field('foreignKey')->equals($identifier)
- ->field('locale')->equals($locale)
- ->field('objectClass')->equals($wrapped->getMetadata()->name)
- ->getQuery();
+ if ($this->usesPersonalTranslation($translationClass)) {
+ // first try to load it using collection
+ die(print_r($wrapped->getMetadata()));
+ $found = false;
+ foreach ($wrapped->getMetadata()->associationMappings as $assoc) {
+ $isRightCollection = $assoc['targetEntity'] === $translationClass
+ && $assoc['mappedBy'] === 'object'
+ && $assoc['type'] === ClassMetadataInfo::ONE_TO_MANY
+ ;
+ if ($isRightCollection) {
+ $collection = $wrapped->getPropertyValue($assoc['fieldName']);
+ foreach ($collection as $trans) {
+ if ($trans->getLocale() === $locale) {
+ $result[] = array(
+ 'field' => $trans->getField(),
+ 'content' => $trans->getContent()
+ );
+ }
+ }
+ $found = true;
+ break;
+ }
+ }
+ // if collection is not set, fetch it through relation
+ if (!$found) {
+ $dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
+ $dql .= ' WHERE t.locale = :locale';
+ $dql .= ' AND t.object = :object';
- $q->setHydrate(false);
- $result = $q->execute();
- if ($result instanceof Cursor) {
- $result = $result->toArray();
+ $q = $em->createQuery($dql);
+ $q->setParameters(compact('object', 'locale'));
+ $result = $q->getArrayResult();
+ }
+ } else {
+ // load translated content for all translatable fields
+ $identifier = $wrapped->getIdentifier();
+ // construct query
+ $qb = $dm->createQueryBuilder($translationClass);
+ $q = $qb->field('foreignKey')->equals($identifier)
+ ->field('locale')->equals($locale)
+ ->field('objectClass')->equals($wrapped->getMetadata()->name)
+ ->getQuery();
+
+ $q->setHydrate(false);
+ $result = $q->execute();
+ if ($result instanceof Cursor) {
+ $result = $result->toArray();
+ }
}
return $result;
}
/**
* {@inheritDoc}
*/
- public function findTranslation($objectId, $objectClass, $locale, $field, $translationClass)
+ public function findTranslation(AbstractWrapper $wrapped, $locale, $field, $translationClass)
{
$dm = $this->getObjectManager();
- $qb = $dm->createQueryBuilder($translationClass);
- $q = $qb->field('foreignKey')->equals($objectId)
+ $qb = $dm
+ ->createQueryBuilder($translationClass)
->field('locale')->equals($locale)
->field('field')->equals($field)
- ->field('objectClass')->equals($objectClass)
- ->getQuery();
-
+ ->limit(1)
+ ;
+ if ($this->usesPersonalTranslation($translationClass)) {
+ $qb->field('object')->equals($wrapped->getObject());
+ } else {
+ $qb->field('foreignKey')->equals($wrapped->getIdentifier());
+ $qb->field('objectClass')->equals($wrapped->getMetadata()->name);
+ }
+ $q = $qb->getQuery();
+ $q->setHydrate(false);
$result = $q->execute();
if ($result instanceof Cursor) {
$result = current($result->toArray());
}
- $q->setHydrate(false);
return $result;
}
/**
* {@inheritDoc}
*/
- public function removeAssociatedTranslations($objectId, $transClass, $targetClass)
+ public function removeAssociatedTranslations(AbstractWrapper $wrapped, $transClass)
{
$dm = $this->getObjectManager();
- $qb = $dm->createQueryBuilder($transClass);
- $q = $qb->remove()
- ->field('foreignKey')->equals($objectId)
- ->field('objectClass')->equals($targetClass)
- ->getQuery()
+ $qb = $dm
+ ->createQueryBuilder($transClass)
+ ->remove()
;
+ if ($this->usesPersonalTranslation($transClass)) {
+ $qb->field('object')->equals($wrapped->getObject());
+ } else {
+ $qb->field('foreignKey')->equals($wrapped->getIdentifier());
+ $qb->field('objectClass')->equals($wrapped->getMetadata()->name);
+ }
+ $q = $qb->getQuery();
return $q->execute();
}
@@ -4,6 +4,7 @@
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Doctrine\ORM\Query;
+use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Gedmo\Translatable\Mapping\Event\TranslatableAdapter;
use Gedmo\Tool\Wrapper\AbstractWrapper;
use Doctrine\DBAL\Types\Type;
@@ -48,18 +49,54 @@ public function loadTranslations($object, $translationClass, $locale)
{
$em = $this->getObjectManager();
$wrapped = AbstractWrapper::wrapp($object, $em);
- // load translated content for all translatable fields
- $objectId = $wrapped->getIdentifier();
- // construct query
- $dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
- $dql .= ' WHERE t.foreignKey = :objectId';
- $dql .= ' AND t.locale = :locale';
- $dql .= ' AND t.objectClass = :objectClass';
- // fetch results
- $objectClass = $wrapped->getMetadata()->name;
- $q = $em->createQuery($dql);
- $q->setParameters(compact('objectId', 'locale', 'objectClass'));
- return $q->getArrayResult();
+ $result = array();
+ if ($this->usesPersonalTranslation($translationClass)) {
+ // first try to load it using collection
+ $found = false;
+ foreach ($wrapped->getMetadata()->associationMappings as $assoc) {
+ $isRightCollection = $assoc['targetEntity'] === $translationClass
+ && $assoc['mappedBy'] === 'object'
+ && $assoc['type'] === ClassMetadataInfo::ONE_TO_MANY
+ ;
+ if ($isRightCollection) {
+ $collection = $wrapped->getPropertyValue($assoc['fieldName']);
+ foreach ($collection as $trans) {
+ if ($trans->getLocale() === $locale) {
+ $result[] = array(
+ 'field' => $trans->getField(),
+ 'content' => $trans->getContent()
+ );
+ }
+ }
+ $found = true;
+ break;
+ }
+ }
+ // if collection is not set, fetch it through relation
+ if (!$found) {
+ $dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
+ $dql .= ' WHERE t.locale = :locale';
+ $dql .= ' AND t.object = :object';
+
+ $q = $em->createQuery($dql);
+ $q->setParameters(compact('object', 'locale'));
+ $result = $q->getArrayResult();
+ }
+ } else {
+ // load translated content for all translatable fields
+ $objectId = $wrapped->getIdentifier();
+ // construct query
+ $dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
+ $dql .= ' WHERE t.foreignKey = :objectId';
+ $dql .= ' AND t.locale = :locale';
+ $dql .= ' AND t.objectClass = :objectClass';
+ // fetch results
+ $objectClass = $wrapped->getMetadata()->name;
+ $q = $em->createQuery($dql);
+ $q->setParameters(compact('objectId', 'locale', 'objectClass'));
+ $result = $q->getArrayResult();
+ }
+ return $result;
}
/**
@@ -87,6 +124,7 @@ public function findTranslation(AbstractWrapper $wrapped, $locale, $field, $tran
$qb->setParameter('objectClass', $wrapped->getMetadata()->name);
}
$q = $qb->getQuery();
+ $q->setMaxResults(1);
$result = $q->getResult();
if ($result) {
Oops, something went wrong.

0 comments on commit 7052ee3

Please sign in to comment.