0
votes

he guys,

i want to make a simple reference on mongodb documents using symfony2.

i have this two documents and want to store picture references into the requests document. it works for me, if i have only the picture ids in the requests document.

so i need the follow:

can everyone change the document files and make and extends the custum call to get all pictures as object from the requests (picture array)?

my original files:

Document Pictures:

<?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;
    }
}

Document Pictures:

<?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;
    }
}

My Basic Calls:

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

To my tests:

i added in the requests document the follow:

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

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

}

and added pictures like this:

$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();

this works - but i cannot get the pictures by loading the document.

my code to load:

$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();         
}

THIS WILL NOT WORK. i become the follow error:

Fatal error: Doctrine\ODM\MongoDB\Proxy\ProxyFactory::getProxy(): Failed opening required '.../app/cache/dev/doctrine/odm/mongodb/Proxies/_CG_TestBundleDocumentPictures.php' (include_path='.:.../library:/usr/local/zend/share/pear') in ..../test/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Proxy/ProxyFactory.php on line 100

thanks, jan

2

2 Answers

0
votes

First off you only need to call doctrine one time in $dm your overloading your resources and thats bad practice. One function, one Doctrine call. Secondly, you need a $dm->persist($request) and then $dm->flush(). Create a OnetoOne between your Documents and then make $pictures an Doctrine Array Collection. Then set a picture like you tried, then make a smiple query and call $request->getPicture()->getId().

0
votes

Ok i found the error:

In the deps file i have the following lines:

[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

After updating them to:

[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

And doing php bin/vendors update the references will work again