【问题标题】:Symfony2 && mongodb simple referenceSymfony2 && mongodb 简单参考
【发布时间】:2012-07-04 15:28:36
【问题描述】:

各位,

我想使用 symfony2 对 mongodb 文档做一个简单的参考。

我有这两个文档,并希望将图片引用存储到请求文档中。如果我在请求文档中只有图片 ID,它对我有用。

所以我需要以下内容:

每个人都可以更改文档文件并进行和扩展 custum 调用以从请求(图片数组)中获取所有图片作为对象吗?

我的原始文件:

文档图片:

<?php

namespace TestBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(repositoryClass="TestBundle\Repository\RequestsRepository")
 */
class Requests
{
    /**
     * @MongoDB\Id
     */
    protected $id;

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

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }
}

文档图片:

<?php

namespace TestBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(repositoryClass="TestBundle\Repository\PicturesRepository")
 */
class Pictures
{
    /**
     * @MongoDB\Id
     */
    protected $id;

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

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setFilename($filename)
    {
        $this->filename = $filename;
    }

    public function getTitle()
    {
        return $this->filename;
    }
}

我的基本通话:

$dm = $this->get('doctrine.odm.mongodb.document_manager');
$request = $dm->getRepository('TestBundle:Requests')->find($requestId);

我的测试:

我在请求文档中添加了以下内容:

/**
 * @MongoDB\ReferenceMany(targetDocument="Pictures",cascade={"persist"},simple="true")
 */
protected $pictures = array();
public function setPictures($pictures)
{
    $this->pictures[] = $pictures;
}

public function getPictures()
{
    return $this->pictures;

}

并添加了这样的图片:

$dm     = $this->get('doctrine.odm.mongodb.document_manager');
$photo   = $dm->getRepository('TestBundle:Pictures')->find($photoId);

$dm1 = $this->get('doctrine.odm.mongodb.document_manager');
$request = $dm1->getRepository('TestBundle:Requests')->find($requestId);

$request->setPictures($photo);
$dm1->flush();

这可行 - 但我无法通过加载文档来获取图片。

我要加载的代码:

$dm1 = $this->get('doctrine.odm.mongodb.document_manager');
$request = $dm1->getRepository('TestBundle:Requests')->find($requestId);

$pictures = $request->getPictures();

foreach($pictures as $picture)
{
    print $picture->getId();         
}

这行不通。我变成了以下错误:

致命错误:Doctrine\ODM\MongoDB\Proxy\ProxyFactory::getProxy(): 需要打开失败 '.../app/cache/dev/doctrine/odm/mongodb/Proxies/_CG_TestBundleDocumentPictures.php' (include_path='.:.../library:/usr/local/zend/share/pear') 在 ..../test/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Proxy/ProxyFactory.php 在第 100 行

谢谢,杨

【问题讨论】:

    标签: mongodb symfony


    【解决方案1】:

    首先,您只需要在 $dm 中调用一次教义就可以使您的资源超载,这是一种不好的做法。一个函数,一个 Doctrine 调用。其次,你需要一个 $dm->persist($request) 和 $dm->flush()。在您的 Documents 之间创建一个 OnetoOne,然后使 $pictures 成为一个 Doctrine Array 集合。然后像你尝试的那样设置一张图片,然后进行一个简单的查询并调用 $request->getPicture()->getId()。

    【讨论】:

    • 嘿,戴夫,你是对的 - 重载非常糟糕,并且持久调用在我的代码中实现。但是当我使用 recerenceOne 时,这个调用的代码很糟糕,并给出了同样的错误 $dm = $this->get('doctrine.odm.mongodb.document_manager'); $request = $dm->getRepository('WunschbildBundle:Requests')->find($requestId);
    【解决方案2】:

    好的,我发现了错误:

    在 deps 文件中,我有以下几行:

    [doctrine-common]
        git=http://github.com/doctrine/common.git
        version=2.1.4
    
    [doctrine-dbal]
        git=http://github.com/doctrine/dbal.git
        version=2.1.7
    
    [doctrine]
        git=http://github.com/doctrine/doctrine2.git
        version=2.1.7
    

    更新后:

    [doctrine-common]
        git=http://github.com/doctrine/common.git
        version=2.2.1
    
    [doctrine-dbal]
        git=http://github.com/doctrine/dbal.git
        version=2.2.1
    
    [doctrine]
        git=http://github.com/doctrine/doctrine2.git
        version=2.2.1
    

    并且执行php bin/vendors update 引用将再次起作用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多