【问题标题】:Add refrences to MongoDB schema within Symfony project在 Symfony 项目中添加对 MongoDB 模式的引用
【发布时间】:2017-09-14 10:30:31
【问题描述】:

我正在尝试使用 Symfony 中的一些引用创建 MongoDB 数据库。 在我的上下文中,我有 2 个文档 CustomerMeetingOne Customer 可以有 Many Meeting 以便我所做的:

Meeting.php

<?php

namespace FrontOfficeBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class Meeting
{
    /**
     * @MongoDB\Id
     * @MongoDB\ReferenceOne(targetDocument="Customer")
     */
    protected $id;

    /**
     * @MongoDB\Field(type="timestamp")
     */
    protected $creationDate;

...

Customer.php

    <?php

namespace FrontOfficeBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class Customer
{
    /**
     * @MongoDB\Id()
     * @MongoDB\ReferenceMany(targetDocument="Meeting")
     */

    protected $id;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $username;

...

然后当我运行命令行时:

php bin/console 学说:mongodb:schema:update

我明白了:

没有为文档“FrontOfficeBundle\Document\Meeting”指定标识符/主键。每个文档都必须有一个标识符/主键。

我尝试使用 @MongoDB\UniqueIndex() 但没办法。 我认为 @MongoDB\Id 应该是一个标识符!!!

版本

  • Symfony 3.2
  • MongoDB 3.4.4

有什么想法吗?

谢谢。

【问题讨论】:

    标签: mongodb symfony bidirectional


    【解决方案1】:

    最后我找到了解决方案,首先我在文档 Customer 中添加了一个名为 $meetings 的字段,并在文档会议中添加了另一个 $customer ,如下所示:

    Customer.php

    /**
     * @MongoDB\ReferenceMany(targetDocument="meeting", mappedBy="customer")
     */
    protected $meetings;
    

    Meeting.php

    /**
     * @MongoDB\ReferenceOne(targetDocument="customer", inversedBy="meetings")
     */
    protected $customer;
    

    然后运行命令行生成setter和getter:

    php bin/console 学说:mongodb:generate:documents mybundleBundle

    当我运行一个固定装置(创建一个客户,然后是它的会议)时,一切正常,您会注意到在您的文档中定义了 1 到多个 的关系(我正在使用罗盘显示 mongoDB)作为 One-to-Squillions 模型(有关 1-to-N 关系的更多详细信息:(https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1) 这意味着子文档(会议)包含参考父级如下图:

    客户

    会议

    在 MongoDB 中,@MongDB/Id 是主键和外键,从不尝试定义对这个唯一字段的引用。

    感谢您的关注。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      相关资源
      最近更新 更多