ynot
ynot

Reputation: 86

Store data in MongoDB and MySQL via Doctrine using the same Entity/Document-Class

I need help. Here is the situation. I am using Symfony2 + FOSRestBundle, I created my Entity-Classes to store my data in MySQL via Doctrine. I also wrote all the Controllers to get the data and directly translate it to my database. That works fine.

namespace Stat\ContentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/** 
 * @ORM\Entity
 * @ORM\Table(name="agegroups")
 */
 class Table
 {
 * @ORM\Column(name="id", type="smallint")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
 protected $id;

 /**
 * @ORM\Column(name="description", type="string", length=50)
 * @Type("string")
 */
 protected $description;

Now I do want to use the same Entity declaration and extend it with some more information for usage with MongoDB. I want to store my data in MySQL and additionally in MongoDB. To reuse the code with the mongodb-odm-bundle I have to use the namespace Document - and that's just the beginning of the problems. If I would want to reuse my Controllers I would have to rewrite that code for MongoDB as well.

namespace Stat\ContentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/** 
 * @MongoDB\Document
 * @ORM\Entity
 * @ORM\Table(name="agegroups")
 */

 class Table
 {
 * @ORM\Column(name="id", type="smallint")
 * @ORM\Id
 * @MongoDB\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
 protected $id;

 /**
 * @ORM\Column(name="description", type="string", length=50)
 * @MongoDB\String
 * @Type("string")
 */
 protected $description;

 /**
 * @ORM\Column(name="mySqlOnly", type="string", length=50)
 * @Type("string")
 */
 protected $mySqlOnly;

 /**
 * @MongoDB\String
 */
 protected $mongoDbOnly;    

Is there an easy way to use a Doctrine-database schema for both document-based and relational databases?

Upvotes: 4

Views: 5904

Answers (1)

webDEVILopers
webDEVILopers

Reputation: 1916

Maybe this helps: Blending the ORM and MongoDB ODM

It is quote easy to use a database nybrid with Doctrine2 since you have an entity- plus an documentmanager.

Upvotes: 5

Related Questions