Skip to content

Commit

Permalink
MonogDB storage support
Browse files Browse the repository at this point in the history
  • Loading branch information
pasinter committed Jul 28, 2014
1 parent 4137409 commit d97b9dd
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Document/Entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ServerGrove\Bundle\TranslationEditorBundle\Document;

use ServerGrove\Bundle\TranslationEditorBundle\Model\Entry as AbstractEntry;

/**
* Entry entity for Doctrine MongoDB
*
* @author Ken Golovin <[email protected]>
*/
class Entry extends AbstractEntry
{

}
15 changes: 15 additions & 0 deletions Document/Locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ServerGrove\Bundle\TranslationEditorBundle\Document;

use ServerGrove\Bundle\TranslationEditorBundle\Model\Locale as AbstractLocale;

/**
* Locale entity for Doctrine MongoDB
*
* @author Ken Golovin <[email protected]>
*/
class Locale extends AbstractLocale
{

}
15 changes: 15 additions & 0 deletions Document/Translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ServerGrove\Bundle\TranslationEditorBundle\Document;

use ServerGrove\Bundle\TranslationEditorBundle\Model\Translation as AbstractTranslation;

/**
* Translation entity for Doctrine MongoDB
*
* @author Ken Golovin <[email protected]>
*/
class Translation extends AbstractTranslation
{

}
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@ We recommend that you only enable this bundle for the development environments,

The collection parameter allows you to define the collection that will contain the translations for the project, so you can have multiple Symfony2 projects in the same mongodb server.

A sample configuration (in your config_dev.yml):
A sample Doctrine ORM configuration (in your config_dev.yml):

server_grove_translation_editor:
storage:
type: server_grove_translation_editor.storage.orm
manager: doctrine.orm.entity_manager

Doctrine MongoDB configuration (in your config_dev.yml):

server_grove_translation_editor:
storage:
type: server_grove_translation_editor.storage.mongodb
manager: doctrine_mongodb.odm.document_manager

Add the routing configuration to app/config/routing_dev.yml

SGTranslationEditorBundle:
Expand Down
42 changes: 42 additions & 0 deletions Resources/config/doctrine/Entry.mongodb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="ServerGrove\Bundle\TranslationEditorBundle\Document\Entry"
table="sg_translation_entry">

<field fieldName="id"
name="id"
id="true"
strategy="AUTO">
</field>

<field fieldName="domain"
name="domain"
type="string"/>

<field name="fileName"
column="file_name"
type="string"/>

<field fieldName="format"
name="format"
type="string"/>

<field fieldName="alias"
name="alias"
type="string"/>

<reference-many target-document="ServerGrove\Bundle\TranslationEditorBundle\Document\Translation"
field="translations"
mapped-by="entry">
<cascade>
<all />
</cascade>
</reference-many>

</document>

</doctrine-mongo-mapping>
41 changes: 41 additions & 0 deletions Resources/config/doctrine/Locale.mongodb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="ServerGrove\Bundle\TranslationEditorBundle\Document\Locale"
table="sg_translation_entry">

<field fieldName="id"
name="id"
id="true"
strategy="AUTO">
</field>


<field fieldName="language"
name="language"
type="string"/>

<field fieldName="country"
name="country"
type="string"
nullable="true"/>

<field fieldName="active"
column="active"
type="boolean"/>


<reference-many target-document="ServerGrove\Bundle\TranslationEditorBundle\Document\Translation"
field="translations"
mapped-by="locale">
<cascade>
<all />
</cascade>
</reference-many>

</document>

</doctrine-mongo-mapping>
39 changes: 39 additions & 0 deletions Resources/config/doctrine/Translation.mongodb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="ServerGrove\Bundle\TranslationEditorBundle\Document\Translation"
table="sg_translation_entry">

<field fieldName="id"
name="id"
id="true"
strategy="AUTO">
</field>

<field fieldName="value"
name="value"
type="string"/>


<reference-one target-document="ServerGrove\Bundle\TranslationEditorBundle\Document\Entry"
field="entry">
<cascade>
<persist />
<refresh />
</cascade>
</reference-one>

<reference-one target-document="ServerGrove\Bundle\TranslationEditorBundle\Document\Locale"
field="locale">
<cascade>
<persist />
<refresh />
</cascade>
</reference-one>

</document>

</doctrine-mongo-mapping>
4 changes: 4 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<service id="server_grove_translation_editor.storage.orm" class="ServerGrove\Bundle\TranslationEditorBundle\Storage\ORMStorage">
<argument type="service" id="server_grove_translation_editor.storage.manager"/>
</service>

<service id="server_grove_translation_editor.storage.mongodb" class="ServerGrove\Bundle\TranslationEditorBundle\Storage\MongoDBStorage">
<argument type="service" id="server_grove_translation_editor.storage.manager"/>
</service>

<service id="server_grove_translation_editor.storage.manager" factory-method="get" factory-service="service_container" class="Doctrine\Common\Persistence\ObjectManager" public="false">
<argument>%server_grove_translation_editor.storage.manager%</argument>
Expand Down
95 changes: 95 additions & 0 deletions Storage/MongoDBStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace ServerGrove\Bundle\TranslationEditorBundle\Storage;

/**
* Doctrine MongoDB Storage
*
* @author Ken Golovin <[email protected]>
*/
class MongoDBStorage extends AbstractStorage implements StorageInterface
{
const CLASS_LOCALE = 'ServerGrove\Bundle\TranslationEditorBundle\Document\Locale';
const CLASS_ENTRY = 'ServerGrove\Bundle\TranslationEditorBundle\Document\Entry';
const CLASS_TRANSLATION = 'ServerGrove\Bundle\TranslationEditorBundle\Document\Translation';

/**
* {@inheritdoc}
*/
protected function getLocaleClassName()
{
return self::CLASS_LOCALE;
}

/**
* {@inheritdoc}
*/
protected function getEntryClassName()
{
return self::CLASS_ENTRY;
}

/**
* {@inheritdoc}
*/
protected function getTranslationClassName()
{
return self::CLASS_TRANSLATION;
}

/**
* {@inheritdoc}
*/
public function findLocaleList(array $criteria = array())
{
$builder = $this->manager->createQueryBuilder($this->getLocaleClassName());

$this->hydrateCriteria($builder, $criteria);

return iterator_to_array($builder->getQuery()->execute());
}

/**
* {@inheritdoc}
*/
public function findEntryList(array $criteria = array())
{
$builder = $this->manager->createQueryBuilder($this->getEntryClassName());

$this->hydrateCriteria($builder, $criteria);

return iterator_to_array($builder->getQuery()->execute());
}

/**
* {@inheritdoc}
*/
public function findTranslationList(array $criteria = array())
{
$builder = $this->manager->createQueryBuilder($this->getTranslationClassName());

if(isset($criteria['locale']) && $criteria['locale'] instanceof \ServerGrove\Bundle\TranslationEditorBundle\Document\Locale) {
$criteria['locale'] = $criteria['locale']->getId();
}
if(isset($criteria['entry']) && $criteria['entry'] instanceof \ServerGrove\Bundle\TranslationEditorBundle\Document\Entry) {
$criteria['entry'] = $criteria['entry']->getId();
}

$this->hydrateCriteria($builder, $criteria);

return iterator_to_array($builder->getQuery()->execute());
}

/**
* Populate a criteria builder
*
* @param \Doctrine\ODM\MongoDB\Query\Builder $builder
* @param array $criteria
*/
protected function hydrateCriteria($builder, array $criteria = array())
{
foreach ($criteria as $fieldName => $fieldValue) {
$builder->addOr($builder->expr()->field($fieldName)->equals($fieldValue));
}
}
}

0 comments on commit d97b9dd

Please sign in to comment.