我可以在我的普通 php 类中使用 Doctrine Mongodb ODM 吗?

发布于 2024-12-01 09:15:13 字数 575 浏览 1 评论 0原文

我想在我的单独项目中使用 Doctrine mongodb ODM,我想使用这个 ODM 执行所有 mongodb 相关的数据库调用。

当我的应用程序(不在交响乐中)想要添加用户对象时,我想调用用 Mongodb-ODM 编写的用户文档类

<?php

namespace Documents;

/** @Document */
class User
{
    // ...
    /** @Id(strategy="AUTO") */
    private $id;

    /** @Field(type="string") */
    private $username;
}

现在我想从我单独的 php 类中调用这个类,如下所示,我们在文档中显示......

$document = new User();
$document->setUsername('abc');
$dm->persist($document);
$dm->flush();

什么在我单独的 php 类中使用这个 $dm= documentmapper 需要什么步骤?

I want to use Doctrine mongodb ODM for my separate project, I want to do all mongodb related database-calls using this ODM.

When My application(not in symphony) want to add user object, I want to call User Document Class written in Mongodb-ODM

<?php

namespace Documents;

/** @Document */
class User
{
    // ...
    /** @Id(strategy="AUTO") */
    private $id;

    /** @Field(type="string") */
    private $username;
}

Now I want to call this class from my separate php classes as below we are shown in documentation....

$document = new User();
$document->setUsername('abc');
$dm->persist($document);
$dm->flush();

What steps are needed to use this $dm= documentmapper in my separate php classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2024-12-08 09:15:13

你绝对可以做到!不过,如何取决于你自己。

当您创建 DocumentManager 实例时,您需要保留它。例如,如果您将其与 Zend Framework 一起使用,则可以使用 Zend_Registry 保留它。这只是一个示例,但您可以通过多种方式保存 DocumentManager。

  • 存储在静态访问的类中
  • 依赖注入
  • 全局var(不真正建议)

You can absolutely do that! How you do it is up to you, though.

When you create your instance of DocumentManager, you need to persist it. For example, If you're using it with Zend Framework, you can persist it with Zend_Registry. This is just an example, but there are many ways you can persist your DocumentManager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文