mongodb - 使用相同的实体/文档类通过 Doctrine 在 MongoDB 和 MySQL 中存储数据

标签 mongodb symfony doctrine

我需要帮助。这是情况。我正在使用 Symfony2 + FOSRestBundle,我创建了我的实体类以通过 Doctrine 将我的数据存储在 MySQL 中。我还写了所有的 Controller 来获取数据并直接将其翻译到我的数据库中。这很好用。

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;

现在我确实想使用相同的实体声明并使用更多信息扩展它以用于 MongoDB。我想将我的数据存储在 MySQL 中,另外还想存储在 MongoDB 中。要通过 mongodb-odm-bundle 重用代码,我必须使用命名空间文档——这只是问题的开始。如果我想重用我的 Controller ,我也必须为 MongoDB 重写该代码。

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;    

有没有一种简单的方法可以将 Doctrine 数据库模式用于基于文档的数据库和关系数据库?

最佳答案

也许这有帮助: Blending the ORM and MongoDB ODM

在 Doctrine2 中使用数据库 nybrid 很容易,因为你有一个实体加上一个文档管理器。

关于mongodb - 使用相同的实体/文档类通过 Doctrine 在 MongoDB 和 MySQL 中存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14315593/

相关文章:

Symfony 4 翻译资源最佳实践

symfony - 是否可以在 postPersist 中刷新?

node.js - mongoose 无法使用默认值空对象填充 ref id

node.js - Mongoose 更新有限制

symfony - 预期的 Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS,得到 ','

php - 通过 PersistentCollection 对结果进行排序

symfony - 如何在 Symfony 中设置 Doctrine 的类继承?

php - Apigility - 如何从 ZF2 命令行使用 Doctrine ORM 模块

javascript - 使用 Node.js 和 Handlebars 循环访问 Mongo 数据库

mongodb - 如何用Go驱动程序替换MongoDB中的文档?