Question

i'm using doctrine/mongodb-odm-bundle and i have a problem: I can't get referenced rows from document (or i just don't know how to do this..) i have 2 documents with one-to-many reference like this: first

/**
 * @MongoDB\Document(collection="categories")
 */
class Category
{
    /**
     * @var integer $id
     *
     * @MongoDB\Id(strategy="auto")
     */
    private $id;

    /**
     * @var string $name
     *
     * @MongoDB\String
     * @Assert\NotBlank()
     * @Assert\MinLength(3)
     */
    private $name;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Application\Bundle\DefaultBundle\Document\Wallpaper", mappedBy="category")
     */
    private $files;
.................
    /**
     * Set files
     *
     * @param array $files
     */
    public function setFiles($files)
    {
        $this->files = $files;
    }

    /**
     * Get files
     *
     * @return array $files
     */
    public function getFiles()
    {
        return $this->files;
    }

.................
second

/**
 * @MongoDB\Document(collection="wallpapers")
 */
class Wallpaper
{
    /**
     * @var string $id
     * @MongoDB\Id(strategy="auto")
     */
    protected $id;
    /**
     * @MongoDB\ReferenceOne(targetDocument="Application\Bundle\DefaultBundle\Document\Category", inversedBy="files")
     */
    private $category;

    /**
     * Get category
     *
     * @return Application\Bundle\DefaultBundle\Document\Category $category
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Set category
     *
     * @param Application\Bundle\DefaultBundle\Document\Category $category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }

}

here is code from controller:

$category = $dm->getRepository('ApplicationDefaultBundle:Category')->findOneBy(...);
$wallpapers = $category->getFiles();

$wallpapers and $document->files are NULL. how i can retrieve records related to category? and how can i get category from concrete wallpaper object? is there any "JOIN" analog like in standard ORM?

Was it helpful?

Solution

The mapping looks correct. I think your issue could be with the query. I'd also check that the wallpapers collection has correct documents with a category field populated with the correct DBRef object data.

$category = $dm->getRepository('Application\Bundle\DefaultBundle\Document\Wallpaper')->findOneById($id);
$wallpapers = $category->getFiles(); // Will return a cursor to the wallpaper objects
foreach ($wallpapers as $wallpaper) {
     do stuff
}

If this isn't the issue, can you paste the full query you are trying and a sample of data from the two collections.

OTHER TIPS

Are you sure that DoctrineORM removed out your project? I had this problem. I removed out my project DoctrineORM and it is worked.

There is no "join" like in SQL, the ODM will make separate queries and combine them into the object. By default doctrine does this lazily when accessing that portion.

As Jamie said the query and data are key parts to helping here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top