在 Symfony 项目中添加对 MongoDB 模式的引用
Add refrences to MongoDB schema within Symfony project
我正在尝试使用 Symfony 中的一些引用创建 MongoDB 数据库。
在我的上下文中,我有 2 个文档 Customer 和 Meeting,One Customer can have 许多 会议所以我做了:
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 doctrine:mongodb:schema:update
我得到了:
没有为文档 'FrontOfficeBundle\Document\Meeting' 指定 identifier/primary 键。每个文档必须有一个 identifier/primary 键。
我尝试使用 @MongoDB\UniqueIndex() 但没有办法。
我认为 @MongoDB\Id 应该是一个标识符 !!!
版本
- Symfony 3.2
- MongoDB 3.4.4
有什么想法吗?
谢谢。
最后我找到了一个解决方案,首先我在文档 Customer 中添加了一个名为 $meetings 的字段,并且在文档 meeting 中添加了另一个 $customer ,如下所示:
Customer.php
/**
* @MongoDB\ReferenceMany(targetDocument="meeting", mappedBy="customer")
*/
protected $meetings;
Meeting.php
/**
* @MongoDB\ReferenceOne(targetDocument="customer", inversedBy="meetings")
*/
protected $customer;
然后运行命令行生成setters和getters:
php bin/console doctrine:mongodb:generate:documents mybundleBundle
当我 运行 固定装置(创建一个客户然后它的会议)时,一切正常,你会注意到 1 到 many 的关系已经在您的文档中定义(我正在使用指南针显示 mongoDB)作为 One-to-Squillions 模型(有关 1-to-N 关系的更多详细信息: (https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1) 这意味着子文档(会议)包含 parent 的引用,如下所示:
客户
会议
在 MongoDB 中,@MongDB/Id 是主键和外键,永远不要尝试定义对此唯一字段的引用。
感谢您的关注。
我正在尝试使用 Symfony 中的一些引用创建 MongoDB 数据库。 在我的上下文中,我有 2 个文档 Customer 和 Meeting,One Customer can have 许多 会议所以我做了:
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 doctrine:mongodb:schema:update
我得到了:
没有为文档 'FrontOfficeBundle\Document\Meeting' 指定 identifier/primary 键。每个文档必须有一个 identifier/primary 键。
我尝试使用 @MongoDB\UniqueIndex() 但没有办法。 我认为 @MongoDB\Id 应该是一个标识符 !!!
版本
- Symfony 3.2
- MongoDB 3.4.4
有什么想法吗?
谢谢。
最后我找到了一个解决方案,首先我在文档 Customer 中添加了一个名为 $meetings 的字段,并且在文档 meeting 中添加了另一个 $customer ,如下所示:
Customer.php
/**
* @MongoDB\ReferenceMany(targetDocument="meeting", mappedBy="customer")
*/
protected $meetings;
Meeting.php
/**
* @MongoDB\ReferenceOne(targetDocument="customer", inversedBy="meetings")
*/
protected $customer;
然后运行命令行生成setters和getters:
php bin/console doctrine:mongodb:generate:documents mybundleBundle
当我 运行 固定装置(创建一个客户然后它的会议)时,一切正常,你会注意到 1 到 many 的关系已经在您的文档中定义(我正在使用指南针显示 mongoDB)作为 One-to-Squillions 模型(有关 1-to-N 关系的更多详细信息: (https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1) 这意味着子文档(会议)包含 parent 的引用,如下所示:
客户
会议
在 MongoDB 中,@MongDB/Id 是主键和外键,永远不要尝试定义对此唯一字段的引用。
感谢您的关注。